Browse Source

Merge branch 'main' of https://github.com/ailrst/blackpink into richal

richal
rv3392 4 years ago
parent
commit
32c0086b3d
  1. 3
      .gitmodules
  2. 0
      SCENE.h
  3. 1
      c-colours
  4. 60
      camera.c
  5. 3
      camera.h
  6. 8
      distfuncs.c
  7. 82
      main.c
  8. 5
      types.h

3
.gitmodules vendored

@ -1,3 +0,0 @@ @@ -1,3 +0,0 @@
[submodule "c-colours"]
path = c-colours
url = https://git.topost.net/alistair/c-colours.git

1
c-colours

@ -1 +0,0 @@ @@ -1 +0,0 @@
Subproject commit ebf612394a92e9f697ed56c55fc236cb72b511c7

60
camera.c

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
#include "types.h"
#include "math.h"
#include "vect.h"
#include "distfuncs.h"
#include "SCENE.h"
#define DRAW_DIST 100.0
#define DRAW_DIST 10000.0
#define MAX_ITERATIONS 25
#define EPSILON 0.1
@ -88,6 +90,7 @@ march(struct ray *r, struct object *scene) @@ -88,6 +90,7 @@ march(struct ray *r, struct object *scene)
{
double travel_dist = 0;
double scene_dist;
double min_dist = DRAW_DIST;
int i;
struct colour out = (struct colour) {};
for (i = 0; (i < MAX_ITERATIONS) && (travel_dist < DRAW_DIST); i++) {
@ -98,6 +101,9 @@ march(struct ray *r, struct object *scene) @@ -98,6 +101,9 @@ march(struct ray *r, struct object *scene)
break;
}
if (min_dist > scene_dist)
min_dist = scene_dist;
manifoldstep(r, scene_dist);
travel_dist += scene_dist;
}
@ -108,6 +114,56 @@ march(struct ray *r, struct object *scene) @@ -108,6 +114,56 @@ march(struct ray *r, struct object *scene)
.iterations = i,
.col = out,
.travel_dist = travel_dist,
.scene_dist = scene_dist
.scene_dist = min_dist
};
}
union badpixelformat {
struct {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
};
Uint32 pixel;
};
Uint32 get_stl_colour(struct colour *cl) {
struct colour c = get_rgb(*cl);
union badpixelformat p;
p.r = c.r;
p.g = c.g;
p.b = c.b;
p.a = c.a;
return p.pixel;
}
Uint32
process_pixel(int i, int j)
{
struct object white_sphere = new_sphere(100);
struct vec *pos = new_vec(4);
struct vec *dir = new_vec4(i - B_INTERNAL_WIDTH/2, j - B_INTERNAL_HEIGHT/2, 100, 0);
struct ray r = (struct ray) {
.pos = *pos,
.dir = *normalise_vec_ip(dir),
};
struct pixel_info p = march(&r, &white_sphere);
p.col.r -= p.iterations*10;
p.col.b -= p.iterations*10;
p.col.g -= p.iterations*10;
// printf("%d, ", p.iterations);
if (p.col.r < 0) p.col.r = 0;
if (p.col.g < 0) p.col.g = 0;
if (p.col.b < 0) p.col.b = 0;
if (p.col.r > 255) p.col.r = 255;
if (p.col.g > 255) p.col.g = 255;
if (p.col.b > 255) p.col.b = 255;
// p.col.b = 255.0 / p.scene_dist;
// if (p.col.b > 255) p.col.b = 255;
free_vec(pos);
free_vec(dir);
return get_stl_colour(&p.col);
}

3
camera.h

@ -1 +1,2 @@ @@ -1 +1,2 @@
struct pixel_info march(struct ray *r, struct object *scene);
struct pixel_info march(struct ray *r, struct object *scene);
Uint32 process_pixel(int, int);

8
distfuncs.c

@ -5,7 +5,10 @@ @@ -5,7 +5,10 @@
double sdf_sphere(struct vec *x) {
const double r = 1.4;
struct vec *v = copy_vec(x);
v->elements[2] -= 5;
v->elements[1] += (SDL_GetTicks()/1000.0) - 5;
double res = magnitude_vec(v) - r;
free_vec(v);
@ -46,14 +49,13 @@ double sdf_box(struct vec *v) { @@ -46,14 +49,13 @@ double sdf_box(struct vec *v) {
struct colour simple_col(struct ray *ray) {
struct colour c = {.r = 0, .g = 255, .b = 0, .a = 255, .sp=CS_RGB};
struct colour c = {.r = 255, .g = 255, .b = 255, .a = 255, .sp=CS_RGB};
return (c);
}
struct object new_sphere(double radius) {
struct object s;
struct vec * v = new_vec4(0,0,5,0);
s.sol.pos = *v;
s.sol.op = B_ADD;

82
main.c

@ -8,11 +8,16 @@ @@ -8,11 +8,16 @@
#include <SDL2/SDL_gamecontroller.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_thread.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
int keyboardstate[322] = {}; // 322 is the number of SDLK_DOWN events
int exitnow = 0;
SDL_Renderer * ren;
Uint32 pixels[B_INTERNAL_HEIGHT][B_INTERNAL_WIDTH];
struct object white_sphere;
struct SDL_Window* make_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@ -23,7 +28,7 @@ struct SDL_Window* make_window(void) { @@ -23,7 +28,7 @@ struct SDL_Window* make_window(void) {
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
B_WINDOW_WIDTH, B_WINDOW_HEIGHT,
0);
SDL_WINDOW_RESIZABLE);
}
@ -69,50 +74,70 @@ int input_loop(void *ptr) { @@ -69,50 +74,70 @@ int input_loop(void *ptr) {
return 0;
}
static int raymarch_threadfn(void *data) {
int id = *(int *)data;
int main(int argc, char **argv) {
/* draw stuff */
/* march the rays */
for (int i = 0; i < B_INTERNAL_WIDTH; i++) {
for (int j = 0; j < B_INTERNAL_HEIGHT; j++) {
/* checkerboard rendering */
if ((i + j * B_INTERNAL_WIDTH) % B_NUM_RAYMARCH_THREADS != id) {
continue;
}
//sdlb_draw_col_pixel(p.col, j, i);
pixels[j][i] = process_pixel(i, j);
}
}
free(data);
return 0;
}
int main(int argc, char **argv) {
SDL_Window * win = make_window();
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_RenderSetLogicalSize(ren, B_INTERNAL_HEIGHT, B_INTERNAL_HEIGHT);
// use this to turn on antristroptic filtering
// SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
SDL_Thread *input_thread = SDL_CreateThread(input_loop, "input", (void *)NULL);
struct colour c = get_random_color();
double elapsed;
Uint64 start, end;
struct object white_sphere = new_sphere(100);
white_sphere = new_sphere(100);
/* texture for drawing into */
SDL_Rect view = {.w = B_INTERNAL_WIDTH, .h = B_INTERNAL_HEIGHT, .x = 0, .y = 0};
SDL_Texture *texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, B_INTERNAL_WIDTH, B_INTERNAL_HEIGHT);
SDL_Thread ** threads = calloc(B_NUM_RAYMARCH_THREADS, sizeof(SDL_Thread *));
while (!exitnow) {
/* clear the view */
start = SDL_GetPerformanceCounter();
// SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
// SDL_RenderClear(ren);
SDL_RenderClear(ren);
if (SDL_GetTicks() % 800 == 0) {
c = get_random_color();
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) {
int *tid = malloc(sizeof(int));
*tid = i;
threads[i] = SDL_CreateThread(raymarch_threadfn, "raymarch", tid);
// tid is freed in raymarch_threadfn
}
/* draw stuff */
/* march the rays */
for (int i = 0; i < B_INTERNAL_WIDTH; i++) {
for (int j = 0; j < B_INTERNAL_HEIGHT; j++) {
struct vec *pos = new_vec(4);
struct vec *dir = new_vec4(i - B_INTERNAL_WIDTH/2, j - B_INTERNAL_HEIGHT/2, 100, 0);
struct ray r = (struct ray) {
.pos = *pos,
.dir = *normalise_vec_ip(dir),
};
struct pixel_info p = march(&r, &white_sphere);
p.col.r += p.iterations;
p.col.b += 255 * p.travel_dist / 100;
sdlb_draw_col_pixel(p.col, j, i);
// free_vec(pos);
// free_vec(dir);
}
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) {
int status;
SDL_WaitThread(threads[i], &status);
}
SDL_UpdateTexture(texture, &view, pixels, B_INTERNAL_WIDTH * sizeof(Uint32));
SDL_RenderCopy(ren, texture, NULL, NULL);
SDL_RenderPresent(ren);
@ -121,11 +146,14 @@ int main(int argc, char **argv) { @@ -121,11 +146,14 @@ int main(int argc, char **argv) {
if (el > 0) {
elapsed = 1000 / el;
}
printf("framerate: %f\r", elapsed);
printf("\rframerate: %f", elapsed);
fflush(stdout);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
}
@ -134,6 +162,8 @@ int main(int argc, char **argv) { @@ -134,6 +162,8 @@ int main(int argc, char **argv) {
* DRAWING UTILITIES
*
*/
void sdlb_set_colour(struct colour col) {
struct colour c = get_rgb(col);
SDL_SetRenderDrawColor(ren, c.r, c.g, c.b, c.a);

5
types.h

@ -1,10 +1,11 @@ @@ -1,10 +1,11 @@
#ifndef TYPES_H
#define TYPES_H
#define B_NUM_RAYMARCH_THREADS 8
#define B_WINDOW_WIDTH 800
#define B_WINDOW_HEIGHT 600
#define B_INTERNAL_HEIGHT 800
#define B_INTERNAL_WIDTH 600
#define B_INTERNAL_HEIGHT 100
#define B_INTERNAL_WIDTH 100
#include "vect.h"
#include "main.h"

Loading…
Cancel
Save