Texture rendering now works, but colors seem broken. But it works, and works better

parent 41c251ec
No preview for this file type
......@@ -5,6 +5,7 @@
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <stdint.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <complex.h>
......@@ -42,9 +43,10 @@ mandelbrot(Complex c) {
}
typedef struct{
short int r;
short int g;
short int b;
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} Color;
typedef struct {
......@@ -57,8 +59,8 @@ void
calculate_set(Array *arr, int height, int width, ViewInfo view)
{
#pragma omp parallel for schedule(guided)
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double real = view.x_min + (x * (view.x_max - view.x_min)) / width;
double imag = view.y_min + (y * (view.y_max - view.y_min)) / height;
Complex c = {real, imag};
......@@ -66,22 +68,23 @@ calculate_set(Array *arr, int height, int width, ViewInfo view)
int iterations = mandelbrot(c);
if (iterations == MAX_ITERATIONS){
arr->pointer[x * height + y].r = 0;
arr->pointer[x * height + y].g = 0;
arr->pointer[x * height + y].b = 0;
arr->pointer[y * width + x].r = 0;
arr->pointer[y * width + x].g = 0;
arr->pointer[y * width + x].b = 0;
arr->pointer[y * width + x].a = 0;
} else {
double t = (double)iterations / MAX_ITERATIONS;
t = 0.5 + 0.5 * tan(log(t + 0.0001) * 3.0);
arr->pointer[x * height + y].r = 255 * t * 0.5;
arr->pointer[x * height + y].g = 255 * t * 0.4;
arr->pointer[x * height + y].b = 255 * t;
arr->pointer[y * width + x].r = 255 * t * 0.5;
arr->pointer[y * width + x].g = 255 * t * 0.4;
arr->pointer[y * width + x].b = 255 * t;
arr->pointer[y * width + x].a = 255;
}
}
}
}
void
update_texture(App *app, Array *arr) {
void *pixels;
......@@ -92,21 +95,13 @@ update_texture(App *app, Array *arr) {
return;
}
uint32_t *dst = pixels;
Color *src = arr->pointer;
for (int y = 0; y < app->win_height; y++) {
for (int x = 0; x < app->win_width; x++) {
Color c = src[x * app->win_height + y];
// Convert RGB to RGBA8888 format
*dst++ = (c.b << 0) | (c.g << 8) | (c.r << 16) | (255 << 24);
}
dst += pitch / sizeof(uint32_t) - app->win_width;
}
memcpy(pixels, arr->pointer, app->win_width * app->win_height * sizeof(Color));
SDL_UnlockTexture(app->texture);
}
void
render_cl(Array *arr, App *app, ViewInfo view) {
......@@ -125,40 +120,6 @@ render_cl(Array *arr, App *app, ViewInfo view) {
}
void
render(App *app, ViewInfo view, int start_x, int end_x, int start_y, int end_y) {
SDL_SetRenderDrawColor(app->renderer, 0, 0, 50, 255);
SDL_RenderClear(app->renderer);
//#pragma omp parallel for schedule(guided)
for (int x = start_x; x < end_x; x++) {
for (int y = start_y; y < end_y; y++) {
double real = view.x_min + (x * (view.x_max - view.x_min)) / app->win_width;
double imag = view.y_min + (y * (view.y_max - view.y_min)) / app->win_height;
Complex c = {real, imag};
int iterations = mandelbrot(c);
if (iterations == MAX_ITERATIONS){
SDL_SetRenderDrawColor(app->renderer, 0, 0, 0, 255);
} else {
double t = (double)iterations / MAX_ITERATIONS;
t = 0.5 + 0.5 * cos(log(t + 0.0001) * 3.0);
int r = (int)(255 * t);
int g = (int)(255 * t * 0.4);
int b = (int)(255 * t * 0.2);
SDL_SetRenderDrawColor(app->renderer, r, g, b, 255);
}
SDL_RenderDrawPoint(app->renderer, x, y);
}
}
}
int
main(void)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment