void draw_scene( void ) { glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 14.0f, -12.0f, 7.0f ); GFX_rotate( 48.5f, 0.0f, 0.0f, 1.0f ); GFX_rotate( 72.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); unsigned int i = 0; while( i != obj->n_objmesh ) { objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); GFX_translate( objmesh->location.x, objmesh->location.y, objmesh->location.z ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } }
void templateAppDraw(void) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); if (touche_delta.x) { vec3 forward = { 0.0f, 1.0f, 0.0f } , direction; float r = rotz * DEG_TO_RAD, c = cosf(r), s = sinf(r); direction.x = c * forward.x - s * forward.y; direction.y = s * forward.x + c * forward.y; eye_location.x += direction.x * -touche_delta.x; eye_location.y += direction.y * -touche_delta.x; } GFX_translate(eye_location.x, eye_location.y, eye_location.z); GFX_rotate(rotz, 0.0f, 0.0f, 1.0f); GFX_rotate(90.0f, 1.0f, 0.0f, 0.0f); mat4_invert(GFX_get_modelview_matrix()); unsigned int i = 0; while (i != obj->n_objmesh) { OBJMESH *objmesh = &obj->objmesh[i]; GFX_push_matrix(); GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); glUniformMatrix4fv(program->uniform_array[0].location, 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } }
void draw_scene( void ) { glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, -90.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 14.0f, -12.0f, 7.0f ); GFX_rotate( 48.5f, 0.0f, 0.0f, 1.0f ); GFX_rotate( 72.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, texture->tid ); mat4 projector_matrix_copy; mat4_copy_mat4( &projector_matrix_copy, &projector_matrix ); unsigned int i = 0; while( i != obj->n_objmesh ) { objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); GFX_translate( objmesh->location.x, objmesh->location.y, objmesh->location.z ); mat4_copy_mat4( &projector_matrix, &projector_matrix_copy ); mat4_translate( &projector_matrix, &projector_matrix, &objmesh->location ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } }
void templateAppDraw(void) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(45.0f, (float)viewport_matrix[2] / (float)viewport_matrix[3], 0.1f, 100.0f, 0.0f); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_translate(0.0f, -14.0f, 3.0f); GFX_rotate(90.0, 1.0f, 0.0f, 0.0f); mat4_invert(GFX_get_modelview_matrix()); }
void templateAppDraw( void ) { glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, -90.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 14.0f, -12.0f, 7.0f ); GFX_rotate( 48.5f, 0.0f, 0.0f, 1.0f ); GFX_rotate( 72.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); unsigned int i = 0; while( i != obj->n_objmesh ) { objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); GFX_translate( objmesh->location.x, objmesh->location.y, objmesh->location.z ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } }
void templateAppDraw( void ) { glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, 0.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 0.0f, -14.0f, 3.0f ); GFX_rotate( 90.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); GFX_push_matrix(); if( auto_rotate ) rot_angle.z += 1.0f; GFX_rotate( rot_angle.x, 1.0f, 0.0f, 0.0f ); GFX_rotate( rot_angle.z, 0.0f, 0.0f, 1.0f ); if( MD5_draw_action( md5, 1.0f / 60.0f ) ) { MD5_set_pose( md5, idle->pose ); } MD5_draw( md5 ); GFX_pop_matrix(); }
// Get the raytracing ray of the camera at a given screen position. void camera_get_ray(const camera_t *camera, const vec2_t *win, const vec4_t *view, vec3_t *o, vec3_t *d) { // XXX: ugly algo. mat4_t inv; vec3_t p; vec3_t norm_pos = vec3((2 * win->x - view->v[0]) / view->v[2] - 1, (2 * win->y - view->v[1]) / view->v[3] - 1, - 1); if (camera->ortho) { inv = mat4_inverted(mat4_mul(camera->proj_mat, camera->view_mat)); *o = mat4_mul_vec(inv, vec4(norm_pos.x, norm_pos.y, 0, 1)).xyz; *d = mat4_mul_vec(inv, vec4(0, 0, -1, 0)).xyz; } else { inv = mat4_inverted(camera->view_mat); *o = mat4_mul_vec(inv, vec4(0, 0, 0, 1)).xyz; inv = mat4_mul(camera->proj_mat, camera->view_mat); mat4_invert(&inv); p = mat4_mul_vec3(inv, norm_pos); *d = vec3_sub(p, *o); } }
void draw_scene(void) { glBindFramebuffer(GL_FRAMEBUFFER, main_buffer); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glViewport(0, 0, viewport_matrix[2], viewport_matrix[3]); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(45.0f, (float)viewport_matrix[2] / (float)viewport_matrix[3], 0.1f, 100.0f, -90.0f); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_translate(14.0f, -12.0f, 7.0f); GFX_rotate(48.5f, 0.0f, 0.0f, 1.0f); GFX_rotate(72.0, 1.0f, 0.0f, 0.0f); mat4_invert(GFX_get_modelview_matrix()); mat4 projector_matrix_copy; mat4_copy_mat4(&projector_matrix_copy, &projector_matrix); unsigned int i = 0; PROGRAM *program = OBJ_get_program(obj, "lighting", 0); while (i != obj->n_objmaterial) { obj->objmaterial[i].program = program; ++i; } glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, depth_texture); i = 0; while (i != obj->n_objmesh) { objmesh = &obj->objmesh[i]; GFX_push_matrix(); GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); mat4_copy_mat4(&projector_matrix, &projector_matrix_copy); mat4_translate(&projector_matrix, &projector_matrix, &objmesh->location); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } }
int main(void) { _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); int width = 800, height = 600; GLFWwindow* window; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(width, height, "cpu-voxels", NULL, NULL); if (!window) { glfwTerminate(); return 1; } glfwMakeContextCurrent(window); glfwSwapInterval(0); glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, mouse_move_callback); glfwSetKeyCallback(window, key_callback); vec3 eye = vec3_create(0.0f, 0.0f, VOXEL_BRICK_SIZE * 4); vec3 center = vec3f(0.0f); vec3 up = vec3_create(0.0, 1.0, 0.0 ); orbit_camera_init(eye, center, up); // TODO: handle resize int dw, dh; glfwGetFramebufferSize(window, &dw, &dh); int stride = 3; int total = dw*dh*stride; uint8_t *data = malloc(total); vec3 ro; //, rd; mat4 m4inverted, view; mat4 projection; mat4_perspective( projection, M_PI/4.0, (float)width/(float)height, 0.1, 1000.0 ); GLuint texture[1]; #ifdef ENABLE_THREADS screen_area areas[TOTAL_THREADS]; threadpool thpool = thpool_init(TOTAL_THREADS); #else screen_area areas[1]; #endif glGenTextures(1, texture); float start = glfwGetTime(); int fps = 0; voxel_brick my_first_brick = voxel_brick_create(); // TODO: make this work when the brick lb corner is not oriented at 0,0,0 voxel_brick_position(my_first_brick, vec3f(0.0f)); voxel_brick_fill(my_first_brick, &brick_fill); while (!glfwWindowShouldClose(window)) { if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) { orbit_camera_rotate(0, 0, -.1, 0); } if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) { orbit_camera_rotate(0, 0, .1, 0); } if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { orbit_camera_rotate(0, 0, 0, .1); } if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { orbit_camera_rotate(0, 0, 0, -.1); } glfwGetFramebufferSize(window, &width, &height); float now = glfwGetTime(); if (now - start > 1) { unsigned long long total_rays = (fps * width * height); printf("fps: %i (%f Mrays/s)@%ix%i - %i threads\n", fps, total_rays/1000000.0, width, height, TOTAL_THREADS); start = now; fps = 0; } fps++; orbit_camera_view(view); ro = mat4_get_eye(view); mat4_mul(m4inverted, projection, view); mat4_invert(m4inverted, m4inverted); // compute 3 points so that we can interpolate instead of unprojecting // on every point vec3 rda, rdb, planeYPosition, dcol, drow; vec3 t0 = vec3_create(0, 0, 0), tx = vec3_create(1, 0, 0), ty = vec3_create(0, 1, 0); vec4 viewport = { 0, 0, width, height }; rda = orbit_camera_unproject(t0, viewport, m4inverted); rdb = orbit_camera_unproject(tx, viewport, m4inverted); planeYPosition = orbit_camera_unproject(ty, viewport, m4inverted); dcol = planeYPosition - rda; drow = rdb - rda; int i=0, bh = height; #ifdef ENABLE_THREADS bh = (height/TOTAL_THREADS); for (i; i<TOTAL_THREADS; i++) { #endif areas[i].dcol = dcol; areas[i].drow = drow; areas[i].pos = planeYPosition; areas[i].ro = ro; areas[i].x = 0; areas[i].y = i*bh; areas[i].width = width; areas[i].height = areas[i].y + (int)(bh); areas[i].screen_height = (int)(height); areas[i].stride = stride; areas[i].data = data; areas[i].render_id = i; areas[i].brick = my_first_brick; #ifdef ENABLE_THREADS thpool_add_work(thpool, (void *)render_screen_area, (void *)(&areas[i])); } thpool_wait(thpool); #else render_screen_area((void *)(&areas[i])); #endif #ifdef RENDER glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_CULL_FACE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glScalef(1.0f, -1.0f, 1.0f); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f( -1, -1); glTexCoord2f(1.0f, 0.0f); glVertex2f( 1, -1); glTexCoord2f(1.0f, 1.0f); glVertex2f( 1, 1); glTexCoord2f(0.0f, 1.0f); glVertex2f( -1, 1); glEnd(); glfwSwapBuffers(window); glDeleteTextures(1, &texture[0]); #endif glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }