+ mandelbrot visualisation
This commit is contained in:
parent
21564bd54a
commit
bffb0aae8a
@ -1 +1 @@
|
||||
#include "internal.hpp"
#include "vkvm.hpp"
#include "../src/Bitmap.hpp"
#include <unistd.h>
//time to sleep for in seconds
constexpr int sleepTime = 10;
void displayImage(const std::string &file, vkvm::GraphicMode graphicMode) {
vkvm::setMode(graphicMode);
Bitmap image(file);
vkvm::setWidth(image.getWidth());
vkvm::setHeight(image.getHeight());
for (int x = 0; x < vkvm::getWidth(); x++) {
for (int y = 0; y < vkvm::getHeight(); y++) {
unsigned int hex = image.getPixel(x, y);
vkvm::Color color = vkvm::Color(hex);
vkvm::setPixel(x, y, color);
}
}
<unistd.h>
Bitmap image(file);
}
void displayImage(const std::string &file) {
displayImage(file, vkvm::GraphicMode::RGB);
sleep(sleepTime);
vkvm::setMode(vkvm::GraphicMode::Gray_256);
vkvm::callEvent(vkvm::EventType::Redraw);
sleep(sleepTime);
vkvm::setMode(vkvm::GraphicMode::TwoColors);
vkvm::callEvent(vkvm::EventType::Redraw);
sleep(sleepTime);
}
int main() {
bool running = true;
vkvm::initialize(0);
//vkvm::setLogLevel(vkvm::LogLevel::DEBUG);
while(running) {
displayImage("../res/P6.bmp");
sleep(sleepTime);
vkvm::setBackgroundColor(vkvm::red);
vkvm::setForegroundColor(vkvm::blue);
vkvm::callEvent(vkvm::EventType::Redraw);
vkvm::setBackgroundColor(vkvm::black);
vkvm::setForegroundColor(vkvm::white);
//time to sleep for in seconds
displayImage("../res/P8.bmp");
displayImage("../res/P9.bmp");
displayImage("../res/P10.bmp");
vkvm::setText("Hello World");
sleep(sleepTime);
vkvm::setFont(vkvm::FontType::FuturisticBlack);
vkvm::callEvent(vkvm::EventType::RenderText);
sleep(sleepTime);
vkvm::setText("Hello World, lorem ipsum dolor sit amet");
sleep(sleepTime);
vkvm::setFont(vkvm::FontType::ProFontIIX);
vkvm::callEvent(vkvm::EventType::RenderText);
sleep(sleepTime);
}
}
|
||||
#include "internal.hpp"
#include "vkvm.hpp"
#include "../src/Bitmap.hpp"
#include "../src/MathFunctions.hpp"
#include <unistd.h>
//time to sleep for in seconds
constexpr int sleepTime = 10;
void displayImage(const std::string &file, vkvm::GraphicMode graphicMode) {
vkvm::setMode(graphicMode);
Bitmap image(file);
vkvm::setWidth(image.getWidth());
vkvm::setHeight(image.getHeight());
for (int x = 0; x < vkvm::getWidth(); x++) {
for (int y = 0; y < vkvm::getHeight(); y++) {
unsigned int hex = image.getPixel(x, y);
vkvm::Color color = vkvm::Color(hex);
vkvm::setPixel(x, y, color);
}
}
<unistd.h>
vkvm::setWidth(image.getWidth());
constexpr int sleepTime = 10;
Bitmap image(file);
for (int x = 0; x < vkvm::getWidth(); ++x) {
for (int y = 0; y < vkvm::getHeight(); ++y) {
vkvm::setPixel(x, y, color);
}
}
}
void displayImage(const std::string &file) {
displayImage(file, vkvm::GraphicMode::RGB);
vkvm::callEvent(vkvm::EventType::Redraw);
sleep(sleepTime);
vkvm::setMode(vkvm::GraphicMode::Gray_256);
vkvm::callEvent(vkvm::EventType::Redraw);
sleep(sleepTime);
vkvm::setMode(vkvm::GraphicMode::TwoColors);
vkvm::callEvent(vkvm::EventType::Redraw);
sleep(sleepTime);
}
int main() {
bool running = true;
vkvm::initialize(0);
while(running) {
displayImage("../res/P6.bmp");
sleep(sleepTime);
vkvm::setBackgroundColor(vkvm::red);
vkvm::setForegroundColor(vkvm::blue);
vkvm::callEvent(vkvm::EventType::Redraw);
sleep(sleepTime);
vkvm::setBackgroundColor(vkvm::black);
vkvm::setForegroundColor(vkvm::white);
//time to sleep for in seconds
displayImage("../res/P8.bmp");
displayImage("../res/P9.bmp");
displayImage("../res/P10.bmp");
vkvm::setText("Hello World");
sleep(sleepTime);
vkvm::setFont(vkvm::FontType::FuturisticBlack);
vkvm::callEvent(vkvm::EventType::RenderText);
sleep(sleepTime);
vkvm::setText("Hello World, lorem ipsum dolor sit amet");
sleep(sleepTime);
vkvm::setFont(vkvm::FontType::ProFontIIX);
vkvm::callEvent(vkvm::EventType::RenderText);
sleep(sleepTime);
mandelbrot();
sleep(sleepTime);
sleep(sleepTime);
setColor(vkvm::black);
sleep(sleepTime);
}
}
|
86
src/MathFunctions.cpp
Normal file
86
src/MathFunctions.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "vkvm.hpp"
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <unistd.h>
|
||||
#include <internal.hpp>
|
||||
#include <thread>
|
||||
|
||||
void sinus() {
|
||||
for (int x = 0; x < vkvm::getWidth(); ++x) {
|
||||
double y = 300 * sin(x);
|
||||
vkvm::log(vkvm::LogLevel::DEBUG, y);
|
||||
vkvm::setPixel(x, static_cast<int>(y), vkvm::getForegroundColor());
|
||||
}
|
||||
}
|
||||
|
||||
long double map(long double value, long double inputMin, long double inputMax, long double outputMin, long double outputMax) {
|
||||
return (value - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin;
|
||||
}
|
||||
|
||||
void mandelbrot() {
|
||||
vkvm::setWidth(600);
|
||||
vkvm::setHeight(800);
|
||||
|
||||
int width = vkvm::getWidth();
|
||||
int height = vkvm::getHeight();
|
||||
|
||||
long double min = -2.84;
|
||||
long double max = 1;
|
||||
int maxIterations = 200;
|
||||
double factor = 1;
|
||||
|
||||
for (int count = 0; count < 10000; count++) {
|
||||
max -= 0.1 * factor;
|
||||
min += 0.15 * factor;
|
||||
factor *= 0.9349;
|
||||
maxIterations += 5;
|
||||
|
||||
if(count > 30) {
|
||||
maxIterations *= 1.02;
|
||||
}
|
||||
|
||||
for (int x = 0; x < width; ++x) {
|
||||
for (int y = 0; y < height; ++y) {
|
||||
|
||||
long double xMap = map(x, 0, width, min, max);
|
||||
long double yMap = map(y, 0, height, min, max);
|
||||
|
||||
long double xInitial = xMap;
|
||||
long double yInitial = yMap;
|
||||
|
||||
int n = 0;
|
||||
|
||||
for (int i = 0; i < maxIterations; ++i) {
|
||||
|
||||
long double x1 = xMap * xMap - yMap * yMap;
|
||||
long double y1 = 2 * xMap * yMap;
|
||||
|
||||
xMap = x1 + xInitial;
|
||||
yMap = y1 + yInitial;
|
||||
|
||||
if ((xMap + yMap) > 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
n++;
|
||||
}
|
||||
|
||||
int brightness = map(n, 0, maxIterations, 0, 255);
|
||||
if (n == maxIterations || (brightness < 20)) {
|
||||
brightness = 0;
|
||||
}
|
||||
|
||||
int red = map(brightness * brightness, 0, 255 * 255, 0, 255);
|
||||
int green = brightness;
|
||||
int blue = map(sqrt(brightness), 0, sqrt(255), 0, 255);
|
||||
|
||||
|
||||
vkvm::setPixel(x, y, vkvm::Color(red, green, blue));
|
||||
|
||||
}
|
||||
}
|
||||
vkvm::callEvent(vkvm::EventType::Redraw);
|
||||
}
|
||||
}
|
6
src/MathFunctions.hpp
Normal file
6
src/MathFunctions.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef DEMO_MATHFUNCTIONS_H
|
||||
#define DEMO_MATHFUNCTIONS_H
|
||||
|
||||
void sinus();
|
||||
void mandelbrot();
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user