Skip to content

Instantly share code, notes, and snippets.

@agustinsivoplas
Created November 21, 2015 15:58
Show Gist options
  • Save agustinsivoplas/0fe2ece92afba9796312 to your computer and use it in GitHub Desktop.
Save agustinsivoplas/0fe2ece92afba9796312 to your computer and use it in GitHub Desktop.
#include "framework/canvas.h"
#include "framework/mm.h"
#include "framework/color.h"
#include "Scene/Vec4.h"
#include "Scene/Camera.h"
#include "Scene/Light.h"
#include "Scene/Sphere.h"
#include "Scene/Scene.h"
#include "Scene/Intersection.h"
#include "Scene/Raytracer.h"
#include "float.h"
#include "cg_parser.h"
#include "time.h"
enum bool {
false, true
};
typedef enum bool bool;
typedef struct Coords {
int xMin, yMin, xMax, yMax, threadId;
} Coords;
Scene* scene;
int cw = 500;
int ch = 500;
void *ray_tracer_thread(void *data) {
int xMin, yMin, xMax, yMax, threadId;
Coords* paintCoords = (Coords*) data;
xMin = paintCoords->xMin;
yMin = paintCoords->yMin;
xMax = paintCoords->xMax;
yMax = paintCoords->yMax;
threadId = paintCoords->threadId;
Vec4* DAux = (Vec4*) calloc(1, sizeof (Vec4));
for (int x = xMin; x < xMax; x++) {
for (int y = yMin; y < yMax; y++) {
float xAux = x * (scene->camera->viewportWidth / cw);
float yAux = y * (scene->camera->viewportHeight / ch);
float zAux = scene->camera->distPlanoProyeccion;
init_vec4(DAux, xAux, yAux, zAux, 0);
Color colorAux = ray_tracing(scene->camera->position, DAux, 1, FLT_MAX, 4, scene);
cg_putpixel(x, y, colorAux);
}
}
free(DAux);
return NULL;
}
void init_coords(Coords* coords, int xMin, int xMax, int yMin, int yMax, int threadId) {
coords->xMin = xMin;
coords->xMax = xMax;
coords->yMin = yMin;
coords->yMax = yMax;
coords->threadId = threadId;
}
int main(int argc, char* argv[]) {
// Crear una ventana de 500x500 pixels:
cg_init(cw, ch, NULL);
#ifdef WIN32
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
#endif
bool b = true;
b = 1 && 0;
float r = rand() / (float) RAND_MAX;
if (!b)
printf("bool ok!!! %f\n", r);
scene = (Scene*) calloc(1, sizeof (Scene));
//Descomentar para escena !
//cg_parse_conf("Escenas_Raytracer/escena1.txt", scene);
cg_parse_conf("Escenas_Raytracer/escena2.txt", scene);
Vec4* D = (Vec4*) calloc(1, sizeof (Vec4));
Color color;
int done = 0;
char buff[100];
time_t now;
//Poner en 0 si se quiere ver la diferencia sin usar threads
int useThreads = 1;
if (useThreads) {
printf("\nEmpiezo a pintar los rayos usando threads.\n");
now = time(0);
strftime(buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime(&now));
printf("%s\n", buff);
//Declaramos 4 threads ya que tenemos un procesador de 4 nucleos
pthread_t thread1, thread2, thread3, thread4;
Coords* coords1 = (Coords*) calloc(1, sizeof (Coords));
Coords* coords2 = (Coords*) calloc(1, sizeof (Coords));
Coords* coords3 = (Coords*) calloc(1, sizeof (Coords));
Coords* coords4 = (Coords*) calloc(1, sizeof (Coords));
init_coords(coords1, -cw / 2, 0, -ch / 2, 0, 1);
pthread_create(&thread1, NULL, ray_tracer_thread, (void*) coords1);
init_coords(coords2, 0, cw / 2, -ch / 2, 0, 2);
pthread_create(&thread2, NULL, ray_tracer_thread, (void*) coords2);
init_coords(coords3, -cw / 2, 0, 0, ch / 2, 3);
pthread_create(&thread3, NULL, ray_tracer_thread, (void*) coords3);
init_coords(coords4, 0, cw / 2, 0, ch / 2, 4);
pthread_create(&thread4, NULL, ray_tracer_thread, (void*) coords4);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
cg_repaint();
printf("\nTermino de pintar los rayos usando threads.\n");
now = time(0);
strftime(buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime(&now));
printf("%s\n", buff);
free(coords1);
free(coords2);
free(coords3);
free(coords4);
} else {
printf("\nEmpiezo a pintar los rayos sin usar threads.\n");
now = time(0);
strftime(buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime(&now));
printf("%s\n", buff);
for (int x = -cw / 2; x < cw / 2; x++) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym != SDLK_ESCAPE)
break;
case SDL_QUIT: done = 1;
}
}
if (!done) {
for (int y = -ch / 2; y < ch / 2; y++) {
float xAux = x * (scene->camera->viewportWidth / cw);
float yAux = y * (scene->camera->viewportHeight / ch);
float zAux = scene->camera->distPlanoProyeccion;
init_vec4(D, xAux, yAux, zAux, 0);
color = ray_tracing(scene->camera->position, D, 1, FLT_MAX, 4, scene);
cg_putpixel(x, y, color);
}
cg_repaint();
}
}
printf("\nTermino de pintar los rayos sin usar threads.\n");
now = time(0);
strftime(buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime(&now));
printf("%s\n", buff);
}
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym != SDLK_ESCAPE)
break;
case SDL_QUIT: done = 1;
}
}
}
// Liberar recursos:
cg_close();
free_scene(scene);
// Ejemplo del modulo de Manejo de Memoria (MM):
int* pint = (int *) cg_malloc(10 * sizeof (int));
printf("pint is a pointer: %p\n", pint);
cg_free(pint); // olvidarse de liberar este objeto produce un mensaje
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment