static void noise_render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); material* noise_mat = asset_get(P("./noise.mat")); GLuint handle = shader_program_handle(material_get_entry(noise_mat, 0)->program); GLuint random_tex = texture_handle(asset_get(P("$CORANGE/textures/random.dds"))); glUseProgram(handle); glActiveTexture(GL_TEXTURE0 + 0 ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, random_tex); glUniform1i(glGetUniformLocation(handle, "noise_texture"), 0); glUniform1f(glGetUniformLocation(handle, "time"), shader_time); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0, -1.0, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0, -1.0, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0, 1.0, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0, 1.0, 0.0f); glEnd(); glActiveTexture(GL_TEXTURE0 + 0 ); glDisable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glUseProgram(0); }
void marching_cubes_render_shadows(light* l) { mat4 viewm = light_view_matrix(l); mat4 projm = light_proj_matrix(l); mat4_to_array(viewm, view_matrix); mat4_to_array(projm, proj_matrix); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(view_matrix); glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj_matrix); mat4_to_array(mat4_id(), world_matrix); material* depth_mat = asset_get_load(P("$CORANGE/shaders/depth.mat")); shader_program* depth_shader = material_get_entry(depth_mat, 0)->program; glUseProgram(*depth_shader); GLint world_matrix_u = glGetUniformLocation(*depth_shader, "world_matrix"); glUniformMatrix4fv(world_matrix_u, 1, 0, world_matrix); GLint proj_matrix_u = glGetUniformLocation(*depth_shader, "proj_matrix"); glUniformMatrix4fv(proj_matrix_u, 1, 0, proj_matrix); GLint view_matrix_u = glGetUniformLocation(*depth_shader, "view_matrix"); glUniformMatrix4fv(view_matrix_u, 1, 0, view_matrix); glBindBuffer(GL_ARRAY_BUFFER, vertex_positions); glVertexPointer(4, GL_FLOAT, 0, (void*)0); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_TRIANGLES, 0, num_verts); glDisableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glUseProgram(0); }
void marching_cubes_render(bool wireframe, camera* c, light* l) { const int full_size = width * height * depth; kernel_memory_gl_aquire(point_color_buffer); kernel_run(write_point_color_back, full_size); kernel_memory_gl_release(point_color_buffer); /* Before we do the metaballs lets just do a rendering based upon volume data */ /* glPointSize(1.0f); glBindBuffer(GL_ARRAY_BUFFER, point_positions); glVertexPointer(4, GL_FLOAT, 0, (void*)0); glEnableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, point_colors); glColorPointer(4, GL_FLOAT, 0, (void*)0); glEnableClientState(GL_COLOR_ARRAY); glDrawArrays(GL_POINTS, 0, full_size); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glPointSize(1.0f); */ /* Then Draw Triangles */ if (wireframe) { glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); } material* metaballs_mat = asset_get_load(P("./shaders/metaballs.mat")); shader_program* metaballs = material_get_entry(metaballs_mat, 0)->program; GLuint NORMALS = glGetAttribLocation(*metaballs, "normals"); glUseProgram(*metaballs); GLint light_position_u = glGetUniformLocation(*metaballs, "light_position"); glUniform3f(light_position_u, l->position.x, l->position.y, l->position.z); GLint camera_position_u = glGetUniformLocation(*metaballs, "camera_position"); glUniform3f(camera_position_u, c->position.x, c->position.y, c->position.z); mat4 lviewm = light_view_matrix(l); mat4 lprojm = light_proj_matrix(l); mat4_to_array(lviewm, lview_matrix); mat4_to_array(lprojm, lproj_matrix); GLint lproj_matrix_u = glGetUniformLocation(*metaballs, "light_proj"); glUniformMatrix4fv(lproj_matrix_u, 1, 0, lproj_matrix); GLint lview_matrix_u = glGetUniformLocation(*metaballs, "light_view"); glUniformMatrix4fv(lview_matrix_u, 1, 0, lview_matrix); texture* env_map = asset_get_load(P("./resources/metaballs_env.dds")); glActiveTexture(GL_TEXTURE0 + 0 ); glBindTexture(GL_TEXTURE_2D, *env_map); glEnable(GL_TEXTURE_2D); glUniform1i(glGetUniformLocation(*metaballs, "env_map"), 0); texture* shadow_map = shadow_mapper_depth_texture(); glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, *shadow_map); glEnable(GL_TEXTURE_2D); glUniform1i(glGetUniformLocation(*metaballs, "shadow_map"), 1); glBindBuffer(GL_ARRAY_BUFFER, vertex_positions); glVertexPointer(4, GL_FLOAT, 0, (void*)0); glEnableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, vertex_normals); glVertexAttribPointer(NORMALS, 4, GL_FLOAT, GL_FALSE, 0, (void*)0); glEnableVertexAttribArray(NORMALS); glDrawArrays(GL_TRIANGLES, 0, num_verts); glDisableClientState(GL_VERTEX_ARRAY); glDisableVertexAttribArray(NORMALS); glActiveTexture(GL_TEXTURE0 + 1 ); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0 + 0 ); glDisable(GL_TEXTURE_2D); glUseProgram(0); if (wireframe) { glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); } }
int main(int argc, char **argv) { #ifdef _WIN32 FILE* ctt = fopen("CON", "w" ); FILE* fout = freopen( "CON", "w", stdout ); FILE* ferr = freopen( "CON", "w", stderr ); #endif corange_init("../../assets_core"); graphics_viewport_set_title("Teapot"); graphics_viewport_set_size(1280, 720); camera* cam = entity_new("camera", camera); cam->position = vec3_new(5, 5, 5); cam->target = vec3_new(0, 0, 0); teapot_shader = asset_hndl_new_load(P("./assets/teapot.mat")); teapot_object = asset_hndl_new_load(P("./assets/teapot.obj")); int running = 1; SDL_Event e = {0}; while(running) { frame_begin(); camera* cam = entity_get("camera"); while(SDL_PollEvent(&e)) { switch(e.type){ case SDL_KEYDOWN: case SDL_KEYUP: if (e.key.keysym.sym == SDLK_ESCAPE) { running = 0; } if (e.key.keysym.sym == SDLK_PRINTSCREEN) { graphics_viewport_screenshot(); } if (e.key.keysym.sym == SDLK_r && e.key.keysym.mod == KMOD_LCTRL) { asset_reload_all(); } break; case SDL_QUIT: running = 0; break; } camera_control_orbit(cam, e); ui_event(e); } ui_update(); glClearColor(0.25, 0.25, 0.25, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); shader_program* shader = material_first_program(asset_hndl_ptr(&teapot_shader)); shader_program_enable(shader); shader_program_set_mat4(shader, "world", mat4_id()); shader_program_set_mat4(shader, "view", camera_view_matrix(cam)); shader_program_set_mat4(shader, "proj", camera_proj_matrix(cam)); shader_program_set_texture(shader, "cube_beach", 0, asset_hndl_new_load(P("$CORANGE/water/cube_sea.dds"))); shader_program_set_vec3(shader, "camera_direction", camera_direction(cam)); renderable* r = asset_hndl_ptr(&teapot_object); for(int i=0; i < r->num_surfaces; i++) { renderable_surface* s = r->surfaces[i]; int mentry_id = min(i, ((material*)asset_hndl_ptr(&r->material))->num_entries-1); material_entry* me = material_get_entry(asset_hndl_ptr(&r->material), mentry_id); glBindBuffer(GL_ARRAY_BUFFER, s->vertex_vbo); shader_program_enable_attribute(shader, "vPosition", 3, 18, (void*)0); shader_program_enable_attribute(shader, "vNormal", 3, 18, (void*)(sizeof(float) * 3)); //shader_program_enable_attribute(shader, "vTangent", 3, 18, (void*)(sizeof(float) * 6)); //shader_program_enable_attribute(shader, "vBinormal", 3, 18, (void*)(sizeof(float) * 9)); //shader_program_enable_attribute(shader, "vTexcoord", 2, 18, (void*)(sizeof(float) * 12)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->triangle_vbo); glDrawElements(GL_TRIANGLES, s->num_triangles * 3, GL_UNSIGNED_INT, (void*)0); shader_program_disable_attribute(shader, "vPosition"); shader_program_disable_attribute(shader, "vNormal"); //shader_program_disable_attribute(shader, "vTangent"); //shader_program_disable_attribute(shader, "vBinormal"); //shader_program_disable_attribute(shader, "vTexcoord"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); } shader_program_disable(shader); glDisable(GL_DEPTH_TEST); ui_render(); graphics_swap(); frame_end(); } corange_finish(); return 0; }
void ui_rectangle_render(ui_rectangle* r) { if(!r->active) { return; } int width = graphics_viewport_width(); int height = graphics_viewport_height(); asset_hndl mat = asset_hndl_new_load(P("$CORANGE/shaders/ui.mat")); shader_program* program_ui = material_get_entry(asset_hndl_ptr(&mat), 0)->program; shader_program_enable(program_ui); shader_program_set_mat4(program_ui, "world", mat4_id()); shader_program_set_mat4(program_ui, "view", mat4_id()); shader_program_set_mat4(program_ui, "proj", mat4_orthographic(0, width, height, 0, -1, 1)); glEnable(GL_BLEND); glBlendFunc(r->blend_src, r->blend_dst); if (asset_hndl_isnull(&r->texture)) { shader_program_set_texture(program_ui, "diffuse", 0, asset_hndl_new_load(P("$CORANGE/textures/white.dds"))); } else { shader_program_set_texture(program_ui, "diffuse", 0, r->texture); } shader_program_set_texture(program_ui, "random", 1, asset_hndl_new_load(P("$CORANGE/textures/random.dds"))); shader_program_set_float(program_ui, "time", r->time); shader_program_set_float(program_ui, "glitch", r->glitch); float rect_colors[] = { r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w, r->color.x, r->color.y, r->color.z, r->color.w }; float rect_positions[] = { r->top_left.x, r->top_left.y, r->top_left.x, r->bottom_right.y, r->bottom_right.x, r->bottom_right.y, r->top_left.x, r->top_left.y, r->bottom_right.x, r->top_left.y, r->bottom_right.x, r->bottom_right.y, }; float rect_texcoords[12]; if (r->texture_tile) { float width = r->bottom_right.x - r->top_left.x; float height = r->bottom_right.y - r->top_left.y; rect_texcoords[0] = 0; rect_texcoords[1] = height / r->texture_height; rect_texcoords[2] = width / r->texture_width; rect_texcoords[3] = height / r->texture_height; rect_texcoords[4] = width / r->texture_width; rect_texcoords[5] = 0; rect_texcoords[6] = 0; rect_texcoords[7] = height / r->texture_height; rect_texcoords[8] = 0; rect_texcoords[9] = 0; rect_texcoords[10] = width / r->texture_width; rect_texcoords[11] = 0; } else { rect_texcoords[0] = 0; rect_texcoords[1] = 0; rect_texcoords[2] = 1; rect_texcoords[3] = 0; rect_texcoords[4] = 1; rect_texcoords[5] = 1; rect_texcoords[6] = 0; rect_texcoords[7] = 0; rect_texcoords[8] = 0; rect_texcoords[9] = 1; rect_texcoords[10] = 1; rect_texcoords[11] = 1; } shader_program_enable_attribute(program_ui, "vPosition", 2, 2, rect_positions); shader_program_enable_attribute(program_ui, "vTexcoord", 2, 2, rect_texcoords); shader_program_enable_attribute(program_ui, "vColor", 4, 4, rect_colors); glDrawArrays(GL_TRIANGLES, 0, 6); shader_program_disable_attribute(program_ui, "vPosition"); shader_program_disable_attribute(program_ui, "vTexcoord"); shader_program_disable_attribute(program_ui, "vColor"); if (r->border_size > 0) { glLineWidth(r->border_size); float border_colors[] = { r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w, r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w }; float border_positions[] = { r->top_left.x, r->top_left.y, r->bottom_right.x, r->top_left.y, r->bottom_right.x, r->bottom_right.y, r->top_left.x, r->bottom_right.y, r->top_left.x, r->top_left.y }; float border_texcoord[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; shader_program_enable_attribute(program_ui, "vPosition", 2, 2, border_positions); shader_program_enable_attribute(program_ui, "vTexcoord", 2, 2, border_texcoord); shader_program_enable_attribute(program_ui, "vColor", 4, 4, border_colors); glDrawArrays(GL_LINE_STRIP, 0, 5); shader_program_disable_attribute(program_ui, "vPosition"); shader_program_disable_attribute(program_ui, "vTexcoord"); shader_program_disable_attribute(program_ui, "vColor"); glLineWidth(1); } glDisable(GL_BLEND); shader_program_disable(program_ui); }