void CL_RenderBatch3D::flush(CL_GraphicContext &gc) { if (position > 0) { gc.set_modelview(CL_Mat4f::identity()); gc.set_program_object(cl_program_sprite); if (use_glyph_program) { CL_BlendMode old_blend_mode = gc.get_blend_mode(); CL_BlendMode blend_mode; blend_mode.set_blend_color(constant_color); blend_mode.set_blend_function(cl_blend_constant_color, cl_blend_one_minus_src_color, cl_blend_zero, cl_blend_one); gc.set_blend_mode(blend_mode); for (int i = 0; i < num_current_textures; i++) gc.set_texture(i, current_textures[i]); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, &vertices[0].position, sizeof(SpriteVertex)); prim_array.set_attributes(1, &vertices[0].color, sizeof(SpriteVertex)); prim_array.set_attributes(2, &vertices[0].texcoord, sizeof(SpriteVertex)); prim_array.set_attributes(3, &vertices[0].texindex, sizeof(SpriteVertex)); gc.draw_primitives(cl_triangles, position, prim_array); for (int i = 0; i < num_current_textures; i++) gc.reset_texture(i); gc.set_blend_mode(old_blend_mode); } else { for (int i = 0; i < num_current_textures; i++) gc.set_texture(i, current_textures[i]); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, &vertices[0].position, sizeof(SpriteVertex)); prim_array.set_attributes(1, &vertices[0].color, sizeof(SpriteVertex)); prim_array.set_attributes(2, &vertices[0].texcoord, sizeof(SpriteVertex)); prim_array.set_attributes(3, &vertices[0].texindex, sizeof(SpriteVertex)); gc.draw_primitives(cl_triangles, position, prim_array); for (int i = 0; i < num_current_textures; i++) gc.reset_texture(i); } gc.reset_program_object(); gc.set_modelview(modelview); position = 0; for (int i = 0; i < num_current_textures; i++) current_textures[i] = CL_Texture(); num_current_textures = 0; } }
void Model_Impl::Draw(CL_GraphicContext &gc, GraphicStore *gs, const CL_Mat4f &modelview_matrix) { gc.set_modelview(modelview_matrix); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vbo_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vbo_normals, 3, cl_type_float, (void *) 0); if (!vbo_texcoords.is_null()) { prim_array.set_attributes(2, vbo_texcoords, 2, cl_type_float, (void *) 0); gc.set_texture(0, gs->texture_underwater); gc.set_texture(1, gs->texture_background); gs->shader_texture.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_texture.Use(gc); } else { throw CL_Exception("What! no texure coordinates?"); } gc.draw_primitives(cl_triangles, vbo_size, prim_array); gc.reset_texture(0); gc.reset_texture(0); }
void CL_Draw::gradient_fill(CL_GraphicContext &gc, float x1, float y1, float x2, float y2, const CL_Gradient &gradient) { CL_Vec2f positions[6] = { CL_Vec2f(x1, y1), CL_Vec2f(x2, y1), CL_Vec2f(x1, y2), CL_Vec2f(x2, y1), CL_Vec2f(x1, y2), CL_Vec2f(x2, y2) }; #define cl_color_to_color4d(c) c.get_red(), c.get_green(), c.get_blue(), c.get_alpha() CL_Vec4f colors[6] = { CL_Vec4f(cl_color_to_color4d(gradient.top_left)), CL_Vec4f(cl_color_to_color4d(gradient.top_right)), CL_Vec4f(cl_color_to_color4d(gradient.bottom_left)), CL_Vec4f(cl_color_to_color4d(gradient.top_right)), CL_Vec4f(cl_color_to_color4d(gradient.bottom_left)), CL_Vec4f(cl_color_to_color4d(gradient.bottom_right)) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attributes(1, colors); gc.set_program_object(cl_program_color_only); gc.draw_primitives(cl_triangles, 6, prim_array); gc.reset_program_object(); }
void App::draw_texture(CL_GraphicContext &gc, const CL_Rectf &rect, const CL_Colorf &color, const CL_Rectf &texture_unit1_coords) { CL_Vec2f positions[6] = { CL_Vec2f(rect.left, rect.top), CL_Vec2f(rect.right, rect.top), CL_Vec2f(rect.left, rect.bottom), CL_Vec2f(rect.right, rect.top), CL_Vec2f(rect.left, rect.bottom), CL_Vec2f(rect.right, rect.bottom) }; CL_Vec2f tex1_coords[6] = { CL_Vec2f(texture_unit1_coords.left, texture_unit1_coords.top), CL_Vec2f(texture_unit1_coords.right, texture_unit1_coords.top), CL_Vec2f(texture_unit1_coords.left, texture_unit1_coords.bottom), CL_Vec2f(texture_unit1_coords.right, texture_unit1_coords.top), CL_Vec2f(texture_unit1_coords.left, texture_unit1_coords.bottom), CL_Vec2f(texture_unit1_coords.right, texture_unit1_coords.bottom) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attribute(1, color); prim_array.set_attributes(2, tex1_coords); gc.draw_primitives(cl_triangles, 6, prim_array); }
void App::render_from_lightsource(CL_GraphicContext &gc, CL_FrameBuffer &framebuffer) { CL_PrimitivesArray prim_array(gc); gc.set_frame_buffer(framebuffer); gc.set_map_mode(cl_user_projection); CL_Rect viewport_rect2(0, 0, CL_Size(scene.gs->texture_shadow.get_width(), scene.gs->texture_shadow.get_height())); gc.set_viewport(viewport_rect2); CL_Mat4f perp = CL_Mat4f::perspective(67.0f, 1.0f, 0.1f, 1000.0f); gc.set_projection(scene.gs->light_projection); CL_BufferControl buffer_control; buffer_control.set_depth_compare_function(cl_comparefunc_lequal); buffer_control.enable_depth_write(true); buffer_control.enable_depth_test(true); buffer_control.enable_stencil_test(false); buffer_control.enable_color_write(false); gc.set_buffer_control(buffer_control); gc.clear_depth(1.0f); CL_Mat4f modelview_matrix = scene.gs->light_modelview; scene.Draw(modelview_matrix, gc, true); gc.reset_program_object(); gc.set_modelview(CL_Mat4f::identity()); gc.set_map_mode(CL_MapMode(cl_map_2d_upper_left)); gc.reset_frame_buffer(); }
void CL_Draw::point(CL_GraphicContext &gc, float x1, float y1, const CL_Colorf &color) { CL_Vec2f positions[1] = { CL_Vec2f(x1, y1) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attribute(1, color); gc.set_program_object(cl_program_color_only); gc.draw_primitives(cl_points, 1, prim_array); gc.reset_program_object(); }
void ParticleObject::Draw(GraphicContext &gc, GraphicStore *gs, const Mat4f &modelview_matrix) { Mat4f matrix_modelview_projection = gs->camera_projection * modelview_matrix; PrimitivesArray prim_array(gc); prim_array.set_attributes(0, object_positions_vbo, 3, type_float, 0); prim_array.set_attributes(1, object_colours_vbo, 4, type_float, 0); gs->shader_color_geometry.Use(gc, matrix_modelview_projection); gc.set_texture(0, gs->texture_ball); gc.draw_primitives(type_points, num_points, prim_array); gc.reset_texture(0); }
void Model_Impl::Draw(GraphicContext &gc, GraphicStore *gs, const Mat4f &modelview_matrix) { Mat4f matrix_modelview_projection = gs->camera_projection * modelview_matrix; Mat3f normal_matrix = Mat3f(modelview_matrix); normal_matrix.inverse(); normal_matrix.transpose(); PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vbo_positions, 3, type_float, 0); prim_array.set_attributes(1, vbo_normals, 3, type_float, 0); gs->shader_color.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_color.Use(gc, modelview_matrix, matrix_modelview_projection, Mat4f(normal_matrix)); gc.draw_primitives(type_triangles, vbo_size, prim_array); }
void Model_Impl::Draw(CL_GraphicContext &gc, GraphicStore *gs, const CL_Mat4f &modelview_matrix) { gc.set_modelview(modelview_matrix); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vbo_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vbo_normals, 3, cl_type_float, (void *) 0); gs->shader_color.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_color.Use(gc); gc.draw_primitives(cl_triangles, vbo_size, prim_array); }
void ObjectPolygon::Draw(CL_GraphicContext &gc, CL_Texture &texture_image) { ObjectVertex *vptr = m_pVertex; CL_Vec3f positions[6]; CL_Vec3f texture_positions[6]; CL_Vec3f normals[6]; // We convert quads into triangles. This is silly, and this entire example should be rewritten to just use triangles if (m_NumVertex!=4) throw CL_Exception("Ooops"); positions[0] = m_pVertex[0].m_Point; texture_positions[0] = m_pVertex[0].m_TexturePoint; normals[0] = m_Normal; positions[1] = m_pVertex[1].m_Point; texture_positions[1] = m_pVertex[1].m_TexturePoint; normals[1] = m_Normal; positions[2] = m_pVertex[2].m_Point; texture_positions[2] = m_pVertex[2].m_TexturePoint; normals[2] = m_Normal; positions[3] = m_pVertex[2].m_Point; texture_positions[3] = m_pVertex[2].m_TexturePoint; normals[3] = m_Normal; positions[4] = m_pVertex[3].m_Point; texture_positions[4] = m_pVertex[3].m_TexturePoint; normals[4] = m_Normal; positions[5] = m_pVertex[0].m_Point; texture_positions[5] = m_pVertex[0].m_TexturePoint; normals[5] = m_Normal; CL_PrimitivesArray prim_array(gc); gc.set_texture(0, texture_image); prim_array.set_attributes(cl_attrib_position, positions); prim_array.set_attribute(cl_attrib_color, CL_Colorf(1.0f,1.0f,1.0f, 1.0f)); prim_array.set_attributes(cl_attrib_texture_position, texture_positions); prim_array.set_attributes(cl_attrib_normal, normals); gc.set_primitives_array(prim_array); gc.draw_primitives_array(cl_triangles, 6); gc.reset_texture(0); gc.reset_primitives_array(); }
void GameTerrain::render_sprite(Canvas &canvas, const LightModel &light_model, Rectf dest) { canvas.push_modelview(); canvas.mult_scale(4.0f, 1.0f, 4.0f); canvas.mult_modelview(Mat4f::translate(-size.width/2.0f, 0.0f, -size.height/2.0f)); gc.set_culled(true); gc.set_front_face(cl_face_side_counter_clockwise); PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vertex_buffer, 3, cl_type_float, (int) &static_cast<Vertex*>(0)->position, sizeof(Vertex)); prim_array.set_attributes(1, vertex_buffer, 3, cl_type_float, (int) &static_cast<Vertex*>(0)->normal, sizeof(Vertex)); gc.set_texture(0, texture_base); gc.set_texture(1, texture_areas); gc.set_texture(2, texture_colors); gc.set_texture(3, texture_borders); gc.set_program_object(shader_program); Material material; // material.shininess = 25.0f; shader_program.set_uniform4f("area", Vec4f(dest.left, dest.top, dest.get_width(), dest.get_height())); shader_program.set_uniform1i("texture_base", 0); shader_program.set_uniform1i("texture_areas", 1); shader_program.set_uniform1i("texture_colors", 2); shader_program.set_uniform1i("texture_borders", 3); /* shader_program.set_uniform("lightSourcePosition", light_model.light_sources[0].position); shader_program.set_uniform("frontLightProductAmbient", light_model.get_light_ambient(material, light_model.light_sources[0])); shader_program.set_uniform("frontLightProductDiffuse", light_model.get_light_diffuse(material, light_model.light_sources[0])); shader_program.set_uniform("frontLightProductSpecular", light_model.get_light_specular(material, light_model.light_sources[0])); shader_program.set_uniform("frontMaterialShininess", material.shininess); shader_program.set_uniform("frontLightModelProductSceneColor", light_model.get_scene_color(material)); */ gc.draw_primitives(type_triangles, num_vertices, prim_array); gc.reset_program_object(); gc.reset_texture(0); gc.reset_texture(1); gc.reset_texture(2); gc.reset_texture(3); gc.reset_polygon_rasterizer(); canvas.pop_modelview(); }
void App::draw(CL_GraphicContext &gc, const CL_Rectf &rect) { CL_Vec2f positions[6] = { CL_Vec2f(rect.left, rect.top), CL_Vec2f(rect.right, rect.top), CL_Vec2f(rect.left, rect.bottom), CL_Vec2f(rect.right, rect.top), CL_Vec2f(rect.left, rect.bottom), CL_Vec2f(rect.right, rect.bottom) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); gc.draw_primitives(cl_triangles, 6, prim_array); }
void CL_Draw::triangle(CL_GraphicContext &gc, const CL_Pointf &a, const CL_Pointf &b, const CL_Pointf &c, const CL_Colorf &color) { CL_Vec2f positions[3] = { CL_Vec2f(a.x, a.y), CL_Vec2f(b.x, b.y), CL_Vec2f(c.x, c.y) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attribute(1, color); gc.set_program_object(cl_program_color_only); gc.draw_primitives(cl_triangles, 3, prim_array); gc.reset_program_object(); }
void CL_Draw::box(CL_GraphicContext &gc, float x1, float y1, float x2, float y2, const CL_Colorf &color) { CL_Vec2f positions[4] = { CL_Vec2f(x1, y1), CL_Vec2f(x2, y1), CL_Vec2f(x2, y2), CL_Vec2f(x1, y2) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attribute(1, color); gc.set_program_object(cl_program_color_only); gc.draw_primitives(cl_line_loop, 4, prim_array); gc.reset_program_object(); }
void Skybox::render(CL_GraphicContext &gc, const Camera &camera) { Camera cam = camera; cam.get_position().set_position(0,0,0); cam.setup_gc(gc, 0.1f, 10.0f); gc.set_texture(0, skybox_texture); gc.set_program_object(program_object); program_object.set_uniform1i(("texture1"), 0); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); gc.draw_primitives(cl_triangles, 6*6, prim_array); gc.reset_program_object(); gc.reset_texture(0); }
void Model_Impl::Draw(CL_GraphicContext &gc, GraphicStore *gs, const CL_Mat4f &modelview_matrix, bool is_draw_shadow) { gc.set_modelview(modelview_matrix); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vbo_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vbo_normals, 3, cl_type_float, (void *) 0); if (is_draw_shadow) { gs->shader_depth.Use(gc); gc.draw_primitives(cl_triangles, vbo_size, prim_array); } else { if (!vbo_texcoords.is_null()) { prim_array.set_attributes(2, vbo_texcoords, 2, cl_type_float, (void *) 0); gs->shader_texture.SetShadowMatrix(gs->shadow_matrix); gc.set_texture(0, gs->texture_brick); gc.set_texture(1, gs->texture_shadow); gs->shader_texture.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_texture.Use(gc); } else { gs->shader_color.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_color.Use(gc); } gc.draw_primitives(cl_triangles, vbo_size, prim_array); gc.reset_texture(0); gc.reset_texture(1); } }
void CL_Draw::texture( CL_GraphicContext &gc, const CL_Texture &texture, const CL_Quadf &quad, const CL_Colorf &color, const CL_Rectf &texture_unit1_coords) { CL_Vec2f positions[6] = { CL_Vec2f(quad.p), CL_Vec2f(quad.q), CL_Vec2f(quad.s), CL_Vec2f(quad.q), CL_Vec2f(quad.s), CL_Vec2f(quad.r) }; CL_Vec2f tex1_coords[6] = { CL_Vec2f(texture_unit1_coords.left, texture_unit1_coords.top), CL_Vec2f(texture_unit1_coords.right, texture_unit1_coords.top), CL_Vec2f(texture_unit1_coords.left, texture_unit1_coords.bottom), CL_Vec2f(texture_unit1_coords.right, texture_unit1_coords.top), CL_Vec2f(texture_unit1_coords.left, texture_unit1_coords.bottom), CL_Vec2f(texture_unit1_coords.right, texture_unit1_coords.bottom) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attribute(1, color); prim_array.set_attributes(2, tex1_coords); gc.set_texture(0, texture); gc.set_program_object(cl_program_single_texture); gc.draw_primitives(cl_triangles, 6, prim_array); gc.reset_program_object(); gc.reset_texture(0); }
// The start of the Application int App::start(const std::vector<CL_String> &args) { CL_DisplayWindowDescription win_desc; win_desc.set_allow_resize(true); win_desc.set_title("Vertex Buffer Object Example"); win_desc.set_depth_size(16); win_desc.set_size(CL_Size( 800, 600 ), false); CL_DisplayWindow window(win_desc); CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); CL_GraphicContext gc = window.get_gc(); Shader shader(gc); // Prepare the display gc.set_map_mode(cl_user_projection); CL_PolygonRasterizer polygon_rasterizer; polygon_rasterizer.set_culled(true); polygon_rasterizer.set_face_cull_mode(cl_cull_back); polygon_rasterizer.set_front_face(cl_face_side_clockwise); gc.set_polygon_rasterizer(polygon_rasterizer); CL_BufferControl buffer_control; buffer_control.enable_depth_test(true); buffer_control.set_depth_compare_function(cl_comparefunc_lequal); buffer_control.enable_depth_write(true); gc.set_buffer_control(buffer_control); std::vector<CL_Vec3f> object_positions; std::vector<CL_Vec3f> object_normals; std::vector<CL_Vec4f> object_material_ambient; const int num_cubes = 20000; object_positions.reserve(num_cubes * 6 * 6); // 6 faces, and 6 vertices per face object_normals.reserve(num_cubes * 6 * 6); object_material_ambient.reserve(num_cubes * 6 * 6); for (int cnt=0; cnt < num_cubes; cnt++) { create_cube(object_positions, object_normals, object_material_ambient); } CL_VertexArrayBuffer vb_positions(gc, &object_positions[0], sizeof(CL_Vec3f) * object_positions.size()); CL_VertexArrayBuffer vb_normals(gc, &object_normals[0], sizeof(CL_Vec3f) * object_normals.size()); CL_VertexArrayBuffer vb_material_ambient(gc, &object_material_ambient[0], sizeof(CL_Vec4f) * object_material_ambient.size()); // ** Note, at this point "object_positions, object_normals and object_material_ambient" // ** can be destroyed. But for the purpose of this example, is it kept CL_Font fps_font(gc, "tahoma", 20); FramerateCounter frameratecounter; unsigned int time_last = CL_System::get_time(); float angle = 0.0f; is_vertex_buffer_on = true; while (!quit) { unsigned int time_now = CL_System::get_time(); float time_diff = (float) (time_now - time_last); time_last = time_now; gc.clear(CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f)); gc.clear_depth(1.0f); gc.set_map_mode(cl_map_2d_upper_left); CL_String fps = cl_format("%1 fps", frameratecounter.get_framerate()); fps_font.draw_text(gc, gc.get_width() - 100, 30, fps); CL_String info = cl_format("%1 vertices", (int) object_positions.size()); fps_font.draw_text(gc, 30, 30, info); fps_font.draw_text(gc, 30, gc.get_height() - 8, "Press any key to toggle the Vertex Buffer option"); if (is_vertex_buffer_on) { fps_font.draw_text(gc, 200, 30, "Vertex Buffer = ON"); } else { fps_font.draw_text(gc, 200, 30, "Vertex Buffer = OFF"); } gc.set_map_mode(cl_user_projection); CL_Mat4f perp = CL_Mat4f::perspective(45.0f, ((float) gc.get_width()) / ((float) gc.get_height()), 0.1f, 100000.0f); gc.set_projection(perp); angle += time_diff / 20.0f; if (angle >= 360.0f) angle -= 360.0f; gc.push_modelview(); gc.set_modelview(CL_Mat4f::identity()); gc.mult_scale(1.0f,1.0f, -1.0f); // So +'ve Z goes into the screen gc.mult_translate(0.0f, 0.0f, 800.0f); gc.mult_rotate(CL_Angle(angle*2.0f, cl_degrees), 0.0f, 1.0f, 0.0f, false); gc.mult_rotate(CL_Angle(angle, cl_degrees), 1.0f, 0.0f, 0.0f, false); shader.Set(gc); shader.Use(gc); CL_PrimitivesArray prim_array(gc); if (is_vertex_buffer_on) { prim_array.set_attributes(0, vb_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vb_normals, 3, cl_type_float, (void *) 0); prim_array.set_attributes(2, vb_material_ambient, 4, cl_type_float, (void *) 0); } else { prim_array.set_attributes(0, &object_positions[0]); prim_array.set_attributes(1, &object_normals[0]); prim_array.set_attributes(2, &object_material_ambient[0]); } gc.draw_primitives(cl_triangles, object_positions.size(), prim_array); gc.pop_modelview(); gc.reset_program_object(); window.flip(0); frameratecounter.frame_shown(); CL_KeepAlive::process(); } return 0; }
void Model_Impl::Draw(CL_GraphicContext &gc, GraphicStore *gs, const CL_Mat4f &modelview_matrix, bool use_geometry_shader) { if (!object_positions.size()) return; if (update_vbo) { update_vbo = false; if (object_texcoords.size()) { object_texcoords_vbo = CL_VertexArrayBuffer(gc, &object_texcoords[0], sizeof(CL_Vec2f) * object_texcoords.size()); } object_positions_vbo = CL_VertexArrayBuffer(gc, &object_positions[0], sizeof(CL_Vec3f) * object_positions.size()); object_normals_vbo = CL_VertexArrayBuffer(gc, &object_normals[0], sizeof(CL_Vec3f) * object_normals.size()); } gc.set_modelview(modelview_matrix); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, object_positions_vbo, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, object_normals_vbo, 3, cl_type_float, (void *) 0); if (!use_geometry_shader) { if (object_texcoords.size()) { prim_array.set_attributes(2, object_texcoords_vbo, 2, cl_type_float, (void *) 0); gc.set_texture(0, gs->texture_brick); gs->shader_texture.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_texture.Use(gc); } else { gs->shader_color.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_color.Use(gc); } } else { if (object_texcoords.size()) throw CL_Exception("Shader not supported"); gs->shader_color_geometry.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_color_geometry.Use(gc); clDisable(CL_CULL_FACE); // For for example, so you can see inside the teapot } gc.draw_primitives(cl_triangles, object_positions.size(), prim_array); if (use_geometry_shader) clEnable(CL_CULL_FACE); if (object_texcoords.size()) { gc.reset_texture(0); } }
void App::recursive_render(CL_GraphicContext &gc, const struct aiScene *sc, const struct aiNode* nd, bool use_texture_coords) { int i; unsigned int n = 0, t; struct aiMatrix4x4 m = nd->mTransformation; // update transform aiTransposeMatrix4(&m); gc.push_modelview(); gc.mult_modelview((float*)&m); // draw all meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; if (mesh->mNormals == NULL) throw CL_Exception("This example expects normals to be set"); std::vector<CL_Vec3f> normals; std::vector<CL_Vec3f> vertices; std::vector<CL_Vec3f> tex_coords; normals.reserve(mesh->mNumFaces * 3); vertices.reserve(mesh->mNumFaces * 3); if (use_texture_coords) { if (mesh->mTextureCoords == NULL || mesh->mTextureCoords[0] == NULL) throw CL_Exception("This example expects texcoords to be set for this object"); tex_coords.reserve(mesh->mNumFaces * 3); } for (t = 0; t < mesh->mNumFaces; ++t) { const struct aiFace* face = &mesh->mFaces[t]; if (face->mNumIndices != 3) throw CL_Exception("This example only supports triangles"); for(i = 0; i < face->mNumIndices; i++) { int index = face->mIndices[i]; normals.push_back(&mesh->mNormals[index].x); vertices.push_back( &mesh->mVertices[index].x); if (use_texture_coords) tex_coords.push_back( &mesh->mTextureCoords[0][index].x); } } if (!vertices.empty()) { CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(cl_attrib_position, &vertices[0]); prim_array.set_attribute(cl_attrib_color, CL_Colorf::white); prim_array.set_attributes(cl_attrib_normal, &normals[0]); if (use_texture_coords) prim_array.set_attributes(cl_attrib_texture_position, &tex_coords[0]); gc.draw_primitives(cl_triangles, vertices.size(), prim_array); } } // draw all children for (n = 0; n < nd->mNumChildren; ++n) { recursive_render(gc, sc, nd->mChildren[n], use_texture_coords); } gc.pop_modelview(); }
// The start of the Application int App::start(const std::vector<CL_String> &args) { quit = false; CL_GL1WindowDescription desc; desc.set_title("ClanLib Object 3D Example"); desc.set_size(CL_Size(640, 480), true); desc.set_multisampling(4); desc.set_depth_size(16); CL_DisplayWindow window(desc); #ifdef _DEBUG //struct aiLogStream stream; //stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL); //aiAttachLogStream(&stream); //stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt"); //aiAttachLogStream(&stream); #endif // Connect the Window close event CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); // Connect a keyboard handler to on_key_up() CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); // Get the graphic context CL_GraphicContext gc = window.get_gc(); #ifdef USE_OPENGL_1 CL_GraphicContext_GL1 gc_gl1 = gc; #endif // Prepare the display gc.set_map_mode(cl_user_projection); CL_PolygonRasterizer polygon_rasterizer; polygon_rasterizer.set_culled(true); polygon_rasterizer.set_face_cull_mode(cl_cull_back); polygon_rasterizer.set_front_face(cl_face_side_clockwise); gc.set_polygon_rasterizer(polygon_rasterizer); CL_BufferControl buffer_control; buffer_control.enable_depth_test(true); buffer_control.set_depth_compare_function(cl_comparefunc_lequal); buffer_control.enable_depth_write(true); gc.set_buffer_control(buffer_control); #ifdef USE_OPENGL_1 // Set the lights CL_LightModel_GL1 light_model; light_model.enable_lighting(true); light_model.set_flat_shading(false); light_model.set_scene_ambient_light(CL_Colorf(0.2f, 0.2f, 0.2f, 1.0f)); gc_gl1.set_light_model(light_model); CL_LightSource_GL1 light_distant; light_distant.set_spot_cutoff(180.0f); light_distant.set_diffuse_intensity(CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); light_distant.set_position(CL_Vec4f(0.0f, -2.0f, 30.0f, 0.0f).normalize3()); gc_gl1.set_light(0, light_distant); cl1Enable(GL_NORMALIZE); #endif #ifdef USE_OPENGL_2 Shader shader(gc); #endif // Create the objects aiSetImportPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE,89.53f); const struct aiScene* scene_teapot = aiImportFile("../Clan3D/Resources/teapot.dae",aiProcessPreset_TargetRealtime_MaxQuality); if (!scene_teapot) throw CL_Exception("Cannot load the teapot model"); const struct aiScene* scene_clanlib = aiImportFile("../Clan3D/Resources/clanlib.dae",aiProcessPreset_TargetRealtime_MaxQuality); if (!scene_clanlib) throw CL_Exception("Cannot load the clanlib model"); const struct aiScene* scene_tuxball = aiImportFile("../Clan3D/Resources/tux_ball.dae",aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_FlipUVs); if (!scene_tuxball) throw CL_Exception("Cannot load the tux ball model"); // Load the texture CL_Texture tux(gc, "../Clan3D/Resources/tux.png"); float angle = 0.0f; // Run until someone presses escape while (!quit) { CL_Mat4f perp = CL_Mat4f::perspective(45.0f, ((float) gc.get_width()) / ((float) gc.get_height()), 0.1f, 1000.0f); gc.set_projection(perp); gc.clear(CL_Colorf::black); gc.clear_depth(1.0f); angle += 1.0f; if (angle >= 360.0f) angle -= 360.0f; #ifdef USE_OPENGL_2 shader.Set(gc); shader.Use(gc); #else gc.set_program_object(cl_program_color_only); #endif CL_PrimitivesArray prim_array(gc); gc.set_modelview(CL_Mat4f::identity()); gc.mult_scale(1.0f,1.0f, -1.0f); // So +'ve Z goes into the screen gc.mult_translate(0.0f, 0.0f, 2.0f); gc.mult_rotate(CL_Angle(angle, cl_degrees), 0.0f, 1.0f, 0.0f, false); gc.push_modelview(); recursive_render(gc, scene_teapot, scene_teapot->mRootNode, false); gc.pop_modelview(); gc.push_modelview(); gc.mult_scale(0.5f, 0.5f, 0.5f); gc.mult_translate(0.0f, -0.5f, 0.0f); recursive_render(gc, scene_clanlib, scene_clanlib->mRootNode, false); gc.pop_modelview(); #ifdef USE_OPENGL_2 shader.Set(gc, 0); shader.Use(gc); #else gc.set_program_object(cl_program_single_texture); #endif gc.set_texture(0, tux); gc.set_modelview(CL_Mat4f::identity()); gc.mult_scale(1.0f,1.0f, -1.0f); // So +'ve Z goes into the screen gc.mult_translate(0.7f, 0.5f, 2.0f); gc.mult_scale(0.05f, 0.05f, 0.05f); gc.mult_rotate(CL_Angle(angle * 4.0f, cl_degrees), 0.0f, 1.0f, 0.0f, false); recursive_render(gc, scene_tuxball, scene_tuxball->mRootNode, true); gc.reset_texture(0); gc.reset_program_object(); // Flip the display, showing on the screen what we have drawed // since last call to flip() window.flip(1); // This call processes user input and other events CL_KeepAlive::process(); } aiReleaseImport(scene_tuxball); aiReleaseImport(scene_clanlib); aiReleaseImport(scene_teapot); aiDetachAllLogStreams(); return 0; }
// The start of the Application int App::start(const std::vector<CL_String> &args) { CL_OpenGLWindowDescription win_desc; //win_desc.set_version(3, 2, false); win_desc.set_allow_resize(true); win_desc.set_title("Point Sprite Example"); win_desc.set_size(CL_Size( 800, 480 ), false); CL_DisplayWindow window(win_desc); CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); CL_String theme; if (CL_FileHelp::file_exists("../../../Resources/GUIThemeAero/theme.css")) theme = "../../../Resources/GUIThemeAero"; else if (CL_FileHelp::file_exists("../../../Resources/GUIThemeBasic/theme.css")) theme = "../../../Resources/GUIThemeBasic"; else throw CL_Exception("No themes found"); CL_GUIWindowManagerTexture wm(window); CL_GUIManager gui(wm, theme); CL_GraphicContext gc = window.get_gc(); // Deleted automatically by the GUI Options *options = new Options(gui, CL_Rect(0, 0, gc.get_size())); CL_Image image_grid(gc, "../Blend/Resources/grid.png"); CL_Texture texture_particle(gc, "Resources/particle.png"); float grid_width = (float) image_grid.get_width(); float grid_height = (float) image_grid.get_height(); grid_space = (float) (image_grid.get_width()); setup_particles(); CL_ShaderObject vertex_shader(gc, cl_shadertype_vertex, text_shader_vertex); if(!vertex_shader.compile()) { throw CL_Exception(cl_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log())); } CL_ShaderObject fragment_shader(gc, cl_shadertype_fragment, text_shader_fragment); if(!fragment_shader.compile()) { throw CL_Exception(cl_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log())); } CL_ProgramObject program_object(gc); program_object.attach(vertex_shader); program_object.attach(fragment_shader); program_object.bind_attribute_location(0, "InPosition"); program_object.bind_attribute_location(1, "InColor"); if (!program_object.link()) { throw CL_Exception(cl_format("Unable to link program object: %1", program_object.get_info_log())); } options->request_repaint(); unsigned int time_last = CL_System::get_time(); while (!quit) { unsigned int time_now = CL_System::get_time(); float time_diff = (float) (time_now - time_last); time_last = time_now; wm.process(); wm.draw_windows(gc); int num_particles = options->num_particles; if (num_particles > max_particles) num_particles = max_particles; move_particles(time_diff, num_particles); const float grid_xpos = 10.0f; const float grid_ypos = 10.0f; // Draw the grid image_grid.draw(gc, grid_xpos, grid_ypos); if (num_particles > 0) { std::vector<CL_Vec2f> positions; std::vector<CL_Vec4f> colors; positions.resize(num_particles); colors.resize(num_particles); for (int cnt=0; cnt<num_particles; cnt++) { positions[cnt] = CL_Vec2f(grid_xpos + particles[cnt].xpos, grid_ypos + particles[cnt].ypos); switch (cnt % 3) { case 0: colors[cnt] = CL_Vec4f(1.0f, 0.0f, 0.0f, 1.0f); break; case 1: colors[cnt] = CL_Vec4f(0.0f, 1.0f, 0.0f, 1.0f); break; case 2: colors[cnt] = CL_Vec4f(0.0f, 0.0f, 1.0f, 1.0f); break; } }; CL_BlendMode blend; blend.enable_blending(true); blend.set_blend_function(cl_blend_src_alpha, cl_blend_one, cl_blend_src_alpha, cl_blend_one); gc.set_blend_mode(blend); CL_Pen pen; pen.enable_point_sprite(true); pen.set_point_size(options->point_size); pen.set_point_sprite_origin(cl_point_sprite_origin_upper_left); gc.set_pen(pen); program_object.set_uniform1i("Texture0", 0); gc.set_texture(0, texture_particle); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, &positions[0]); prim_array.set_attributes(1, &colors[0]); gc.set_program_object(program_object); gc.draw_primitives(cl_points, num_particles, prim_array); gc.reset_program_object(); gc.reset_texture(0); gc.reset_blend_mode(); gc.reset_pen(); } window.flip(1); CL_KeepAlive::process(); } return 0; }
int App::start(const std::vector<std::string> &args) { OpenGLWindowDescription description; description.set_title("UniformBlock Shader"); //description.set_version(3, 1, false); description.set_size(Size(1024, 768), true); DisplayWindow window(description); InputDevice keyboard = window.get_ic().get_keyboard(); GraphicContext gc = window.get_gc(); Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); Slot slot_window_close = window.sig_window_close().connect(this, &App::window_close); // Load and link shaders ProgramObject shader = ProgramObject::load(gc, "Resources/vertex_shader.glsl", "Resources/fragment_shader.glsl"); shader.bind_attribute_location(0, "Position"); if (!shader.link()) throw Exception("Unable to link shader program: Error:" + shader.get_info_log()); int block_size = shader.get_uniform_block_size("TestBlock"); const int num_blocks = 16; ProgramUniformBlock block(gc, block_size * num_blocks); std::vector<float> data_to_upload; data_to_upload.resize((num_blocks * block_size) / sizeof(float) ); float test_colour = 1.0f; for (int cnt=0; cnt<num_blocks; cnt++) { int offset = cnt * block_size / sizeof(float) ; data_to_upload[offset + 0] = test_colour; data_to_upload[offset + 1] = 0.5f; data_to_upload[offset + 2] = 1.0f; test_colour -= 0.05f; } block.upload_data(0, &data_to_upload[0], block_size * num_blocks); quit = false; Font font(gc, "tahoma", 32); clan::ubyte64 startTime = System::get_time(); //// Test code //GLuint uniform_index = -1; //const char *names[] = {"src_color"}; //glGetUniformIndices(handle, 1, names, &uniform_index); //if (uniform_index >=0) //{ // GLint offset; // GLint singleSize; // GLint uniform_type; // glGetActiveUniformsiv(handle, 1, &uniform_index, GL_UNIFORM_TYPE, &uniform_type); // glGetActiveUniformsiv(handle, 1, &uniform_index, GL_UNIFORM_OFFSET, &offset); // glGetActiveUniformsiv(handle, 1, &uniform_index, GL_UNIFORM_SIZE, &singleSize); // if ((uniform_type != GL_FLOAT_VEC3) || (offset !=0) || (singleSize != 1)) // { // throw Exception("well it seems it does not work"); // } //} while (!quit) { gc.clear(Colorf(0.1f, 0.1f, 0.2f)); OpenGL::check_error(); for (int test_run_y=0; test_run_y < 16; test_run_y++) { shader.set_uniform_block("TestBlock", block, test_run_y*block_size); for (int test_run_x=0; test_run_x < 16; test_run_x++) { Vec2f positions[3]; float size = 32.0f; positions[0].x = test_run_x * size; positions[0].y = test_run_y * size + size; positions[1].x = test_run_x * size + size; positions[1].y = test_run_y * size + size; positions[2].x = test_run_x * size + size; positions[2].y = test_run_y * size; PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); gc.set_program_object(shader, cl_program_matrix_modelview_projection); gc.draw_primitives(cl_triangles, 3, prim_array); gc.reset_program_object(); } } font.draw_text(gc, 32, 200, "Hello World"); window.flip(1); KeepAlive::process(); } return 0; }
void CL_Draw::gradient_circle(CL_GraphicContext &gc, const CL_Pointf ¢er, const CL_Pointf ¢ergradient, float radius, const CL_Gradient &gradient) { float offset_x = 0; float offset_y = 0; if(radius < 4) radius = 4; float rotationcount = (radius - 3); float halfpi = 1.5707963267948966192313216916398f; float turn = halfpi / rotationcount; if(center.distance(center + centergradient) < radius) { offset_x = centergradient.x; offset_y = -centergradient.y; } CL_Vec4f colors[3] = { CL_Vec4f(gradient.top_left.get_red(), gradient.top_left.get_green(), gradient.top_left.get_blue(), gradient.top_left.get_alpha()), CL_Vec4f(gradient.bottom_right.get_red(), gradient.bottom_right.get_green(), gradient.bottom_right.get_blue(), gradient.bottom_right.get_alpha()), CL_Vec4f(gradient.bottom_right.get_red(), gradient.bottom_right.get_green(), gradient.bottom_right.get_blue(), gradient.bottom_right.get_alpha()) }; CL_Vec4f triangle_colors[4*3]; for (int i=0; i<3; i++) { triangle_colors[0*3+i] = colors[i]; triangle_colors[1*3+i] = colors[i]; triangle_colors[2*3+i] = colors[i]; triangle_colors[3*3+i] = colors[i]; } for(float i = 0; i < rotationcount ; i++) { float pos1 = cos(i * turn); float pos2 = sin(i * turn); float pos3 = cos((i+1) * turn); float pos4 = sin((i+1) * turn); CL_Vec2f positions[4*3] = { // 90 triangle: CL_Vec2f(center.x + offset_x , center.y + offset_y), CL_Vec2f(center.x + ((float)radius * pos1), center.y + ((float)radius * pos2)), CL_Vec2f(center.x + ((float)radius * pos3), center.y + ((float)radius * pos4)), // 0 triangle: CL_Vec2f(center.x + offset_x , center.y + offset_y), CL_Vec2f(center.x + ((float)radius * pos2), center.y - ((float)radius * pos1)), CL_Vec2f(center.x + ((float)radius * pos4), center.y - ((float)radius * pos3)), // 270 triangle: CL_Vec2f(center.x + offset_x , center.y + offset_y), CL_Vec2f(center.x - ((float)radius * pos1), center.y - ((float)radius * pos2)), CL_Vec2f(center.x - ((float)radius * pos3), center.y - ((float)radius * pos4)), // 180 triangle: CL_Vec2f(center.x + offset_x , center.y + offset_y), CL_Vec2f(center.x - ((float)radius * pos2), center.y + ((float)radius * pos1)), CL_Vec2f(center.x - ((float)radius * pos4), center.y + ((float)radius * pos3)) }; CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, positions); prim_array.set_attributes(1, triangle_colors); gc.set_program_object(cl_program_color_only); gc.draw_primitives(cl_triangles, 4*3, prim_array); gc.reset_program_object(); } }