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 App::render_night_vision(CL_GraphicContext &gc, CL_Texture &source_texture, CL_Texture &mask_texture, CL_Texture &noise_texture, CL_ProgramObject &program_object) { program_object.set_uniform1i("sceneBuffer", 0); program_object.set_uniform1i("noiseTex", 1); program_object.set_uniform1i("maskTex", 2); program_object.set_uniform1f("elapsedTime", elapsedTime); program_object.set_uniform1f("luminanceThreshold", luminanceThreshold); program_object.set_uniform1f("colorAmplification", colorAmplification); program_object.set_uniform1f("effectCoverage", effectCoverage); gc.set_texture(0, source_texture); gc.set_texture(1, noise_texture); gc.set_texture(2, mask_texture); gc.set_program_object(program_object, cl_program_matrix_modelview_projection); draw_texture(gc, CL_Rectf(0,0,gc.get_width(),gc.get_height()), CL_Colorf::white, CL_Rectf(0.0f, 0.0f, 1.0f, 1.0f)); gc.reset_program_object(); gc.reset_texture(2); gc.reset_texture(1); gc.reset_texture(0); }
void ExampleText::draw_text(CL_GraphicContext &gc, CL_Texture &texture, CL_Angle angle) { gc.set_texture(0, texture); gc.push_modelview(); gc.mult_translate(gc.get_width()/2.0f, gc.get_height()/2.0f); gc.mult_rotate(angle, 0.0f, 0.0f, 1.0f); CL_Draw::texture(gc, CL_Rectf(-300.0f, -300.0f, 300.0f, 300.0f), CL_Colorf(1.0f, 1.0f, 1.0f, 0.7f)); gc.pop_modelview(); gc.reset_texture(0); }
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 App::render_gaussian_blur(CL_GraphicContext &gc, float blur_amount, CL_Texture &source_texture, CL_ProgramObject &program_object, float dx, float dy) { int sampleCount = 15; float *sampleWeights = new float[sampleCount]; CL_Vec2f *sampleOffsets = new CL_Vec2f[sampleCount]; sampleWeights[0] = compute_gaussian(0, blur_amount); sampleOffsets[0] = CL_Vec2f(0.0, 0.0); float totalWeights = sampleWeights[0]; for (int i = 0; i < sampleCount / 2; i++) { float weight = compute_gaussian(i + 1.0f, blur_amount); sampleWeights[i * 2 + 1] = weight; sampleWeights[i * 2 + 2] = weight; totalWeights += weight * 2; float sampleOffset = i * 2 + 1.5f; CL_Vec2f delta = CL_Vec2f(dx * sampleOffset, dy * sampleOffset); sampleOffsets[i * 2 + 1] = delta; sampleOffsets[i * 2 + 2] = CL_Vec2f(-delta.x, -delta.y); } for (int i = 0; i < sampleCount; i++) { sampleWeights[i] /= totalWeights; } program_object.set_uniform1i("SourceTexture", 0); program_object.set_uniformfv("SampleOffsets", 2, sampleCount, (float *)sampleOffsets); program_object.set_uniformfv("SampleWeights", 1, sampleCount, sampleWeights); gc.set_texture(0, source_texture); gc.set_program_object(program_object, cl_program_matrix_modelview_projection); draw_texture(gc, CL_Rectf(0,0,gc.get_width(),gc.get_height()), CL_Colorf::white, CL_Rectf(0.0f, 0.0f, 1.0f, 1.0f)); gc.reset_program_object(); gc.reset_texture(0); }
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 HSVSpriteBatch::flush(CL_GraphicContext &gc) { if (fill_position > 0) { CL_PrimitivesArray primarray(gc); primarray.set_attributes(0, positions); primarray.set_attributes(1, hue_offsets); primarray.set_attributes(2, tex1_coords); gc.set_texture(0, current_texture); gc.set_program_object(program, cl_program_matrix_modelview_projection); gc.draw_primitives(cl_triangles, fill_position, primarray); gc.reset_program_object(); gc.reset_texture(0); fill_position = 0; current_texture = CL_Texture(); } }
void ShaderImpl::end(CL_GraphicContext &p_gc) { G_ASSERT(m_initialized); G_ASSERT(m_began); // detach frame buffer p_gc.reset_frame_buffer(); m_frameBuffer.detach_color_buffer(0, m_texture); // prepare shader m_program.set_uniform1i("tex", 0); m_program.set_uniform1i("textureWidth", m_drawRect.get_width()); m_program.set_uniform1i("textureHeight", m_drawRect.get_height()); m_parent->setUniforms(m_program); // draw texture using shader p_gc.set_modelview(CL_Mat4f::identity()); p_gc.mult_translate(m_drawRect.left, m_drawRect.top); p_gc.mult_scale(m_drawRect.get_width(), m_drawRect.get_height()); p_gc.set_texture(0, m_texture); p_gc.set_program_object(m_program); p_gc.draw_primitives(cl_quads, 4, m_quad); p_gc.reset_program_object(); p_gc.reset_texture(0); #if defined(DRAW_WIREFRAME) CL_Draw::line(p_gc, 0, 0, 1, 0, CL_Colorf::red); CL_Draw::line(p_gc, 1, 0, 1, 1, CL_Colorf::red); CL_Draw::line(p_gc, 1, 1, 0, 1, CL_Colorf::red); CL_Draw::line(p_gc, 0, 1, 0, 0, CL_Colorf::red); #endif // DRAW_WIREFRAME // reset modelview matrix p_gc.pop_modelview(); m_began = false; }
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); }
void HSV::render_texture(CL_GraphicContext &gc, CL_ProgramObject &program, CL_Texture &texture, float hue_offset) { CL_Rectf rect(0.0f, 0.0f, (float)gc.get_width(), (float)gc.get_height()); CL_Rectf texture_unit1_coords(0.0f, 0.0f, 1.0f, 1.0f); 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 primarray(gc); primarray.set_attributes(0, positions); primarray.set_attribute(1, CL_Vec1f(hue_offset)); primarray.set_attributes(2, tex1_coords); gc.set_texture(0, texture); gc.set_program_object(program, cl_program_matrix_modelview_projection); gc.draw_primitives(cl_triangles, 6, primarray); gc.reset_program_object(); gc.reset_texture(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); } }
// 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; }
// The start of the Application int App::start(const std::vector<CL_String> &args) { quit = false; // Create a console window for text-output if not available CL_ConsoleWindow console("Console", 80, 200); try { // Set the window // This opens a 640 x 480 window, including the frame size // If you want more control over the window, pass CL_DisplayWindowDescription to CL_DisplayWindow // (This is useful to create a borderless window of a specific size) // If you require target specific control over the window, use the derived CL_OpenGLWindowDescription // (This contains the multisampling options) #ifdef USE_SWRENDER CL_DisplayWindowDescription desc; #else CL_OpenGLWindowDescription desc; // desc.set_multisampling(4); #endif desc.set_title("ClanLib 2D Test"); desc.set_size(CL_Size(800, 600), true); CL_DisplayWindow window(desc); // 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(); CL_Texture texture_image(gc, "tux.png"); texture_image.set_wrap_mode(cl_wrap_repeat, cl_wrap_repeat, cl_wrap_repeat); texture_image.set_min_filter(cl_filter_linear); texture_image.set_mag_filter(cl_filter_linear); CL_ResourceManager resources("resources.xml"); CL_Sprite sprite(gc, "test", &resources); //sprite.set_linear_filter(true); CL_Font small_font = CL_Font(gc, "Tahoma", 12); float test_base_angle = 0.0f; float test_angle = 0.0f; float test_angle_pitch = 0.0f; float test_angle_yaw = 0.0f; float test_scale = 1.0f; bool test_scale_dir = false; // Run until someone presses escape while (!quit) { gc.clear(CL_Colorf(0.0f,0.0f,0.2f)); gc.set_map_mode(CL_MapMode(cl_map_2d_upper_left)); // CL_Draw::point() for (int xcnt=0; xcnt<8; xcnt++) { for (int ycnt=0; ycnt<6; ycnt++) { CL_Draw::point(gc, xcnt*2, ycnt*2, CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); } } small_font.draw_text(gc, 32, 10, "8*6 Points (0 + 2x), (0 + 2y)"); // CL_Draw::line() for (int xcnt=0; xcnt<4; xcnt++) { for (int ycnt=0; ycnt<3; ycnt++) { const int offset_y = 16; const int line_length = 6; const int spacing = 8; CL_Draw::line(gc, xcnt*spacing, (ycnt*spacing) + offset_y, line_length + (xcnt*spacing), (line_length + (ycnt*spacing)) + offset_y, CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); } } small_font.draw_text(gc, 48, 30, "4*3 Lines (0 + 8x), (32 + 8y), (6 + 8x), (38 + 8y)"); // CL_Draw::box() for (int xcnt=0; xcnt<4; xcnt++) { for (int ycnt=0; ycnt<3; ycnt++) { const int offset_y = 48; const int line_length = 6; const int spacing = 8; CL_Draw::box(gc, xcnt*spacing, (ycnt*spacing) + offset_y, line_length + (xcnt*spacing), (line_length + (ycnt*spacing)) + offset_y, CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); } } small_font.draw_text(gc, 48, 66, "4*3 Box (0 + 8x), (32 + 8y), (6 + 8x), (38 + 8y)"); // CL_Draw::fill() for (int xcnt=0; xcnt<4; xcnt++) { for (int ycnt=0; ycnt<3; ycnt++) { const int offset_y = 80; const int line_length = 6; const int spacing = 8; CL_Draw::fill(gc, xcnt*spacing, (ycnt*spacing) + offset_y, line_length + (xcnt*spacing), (line_length + (ycnt*spacing)) + offset_y, CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); } } small_font.draw_text(gc, 48, 90, "4*3 Fill (0 + 8x), (32 + 8y), (6 + 8x), (38 + 8y)"); // CL_Draw::gradient_fill() CL_Gradient gradient; gradient.top_left = CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f); gradient.top_right = CL_Colorf(1.0f, 0.0f, 0.0f, 1.0f); gradient.bottom_left = CL_Colorf(0.0f, 0.0f, 1.0f, 1.0f); gradient.bottom_right = CL_Colorf(0.0f, 1.0f, 0.0f, 1.0f); for (int xcnt=0; xcnt<4; xcnt++) { for (int ycnt=0; ycnt<3; ycnt++) { const int offset_y = 110; const int line_length = 6; const int spacing = 8; CL_Draw::gradient_fill(gc, xcnt*spacing, (ycnt*spacing) + offset_y, line_length + (xcnt*spacing), (line_length + (ycnt*spacing)) + offset_y, gradient); } } small_font.draw_text(gc, 48, 115, "4*3 GradientFill (0 + 8x), (32 + 8y), (6 + 8x), (38 + 8y)"); small_font.draw_text(gc, 48, 125, "top left = white. top right = red"); small_font.draw_text(gc, 48, 135, "bottom left = blue. bottom right = green"); // CL_Draw::circle() { const int offset_y = 140; int radius = 5; CL_Draw::circle(gc, radius, offset_y + radius, radius, CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); const int offset_x = 16; radius = 16; CL_Draw::circle(gc, offset_x + radius, offset_y + radius, radius, CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); } small_font.draw_text(gc, 54, 149, "Circle (5, 145) Radius = 5"); small_font.draw_text(gc, 54, 159, "Circle (32, 156) Radius = 16"); // CL_Draw::gradient_circle() { CL_Gradient gradient; gradient.top_left = CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f); gradient.top_right = CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f); gradient.bottom_left = CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f); gradient.bottom_right = CL_Colorf(1.0f, 0.0f, 0.0f, 1.0f); const int offset_y = 180; float radius = 17.0; float xpos = radius; float ypos = offset_y + radius; CL_Draw::gradient_circle(gc, CL_Pointf( xpos, ypos ), CL_Pointf(radius/2.0, 0.0f), radius, gradient); const int offset_x = 40; radius = 17.0; xpos = offset_x + radius; ypos = offset_y + radius; CL_Draw::gradient_circle(gc, CL_Pointf( xpos, ypos), CL_Pointf(0.0f, radius/2.0), radius, gradient); } small_font.draw_text(gc, 80, 189, "Gradient Circle (16, 196) Radius = 17. Gradient right"); small_font.draw_text(gc, 80, 199, "Gradient Circle (56, 196) Radius = 17. Gradient up"); small_font.draw_text(gc, 80, 209, "centre = white, outer = red"); // CL_Draw::triangle() { const float offset_y = 220.0f; const float size = 12.0f; CL_Draw::triangle(gc, CL_Pointf(0.0f, offset_y), CL_Pointf(0.0f, offset_y + size), CL_Pointf(size, offset_y + size), CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); float offset_x = 16.0f; CL_Draw::triangle(gc, CL_Pointf(offset_x + 0.0f, offset_y + size), CL_Pointf(offset_x + size, offset_y + size), CL_Pointf(offset_x + 0.0f, offset_y), CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); offset_x = 32.0f; CL_Draw::triangle(gc, CL_Pointf(offset_x + size, offset_y + size), CL_Pointf(offset_x + 0.0f, offset_y), CL_Pointf(offset_x + 0.0f, offset_y + size), CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); } small_font.draw_text(gc, 48, 229, "3 Triangles (12 pixel size) (Left vertical edge)."); small_font.draw_text(gc, 48, 239, "Top Left: (0,220) (16,220) (32,220)"); // CL_Draw::texture() gc.set_texture(0, texture_image); { float offset_x = 0.0f; float offset_y = 250.0f; CL_Rectf src_rect(offset_x, offset_y, CL_Sizef(31, 47)); CL_Rectf texture_coords(0.0, 0.0, 1.0f, 1.0f); CL_Colorf color(1.0f, 1.0f, 1.0f, 1.0f); CL_Draw::texture(gc, src_rect, color, texture_coords); offset_x = 33.0f; src_rect = CL_Rectf(offset_x, offset_y, CL_Sizef(31, 47)); texture_coords = CL_Rectf(0.25f, 0.25f, 0.75f, 0.75f); color = CL_Colorf(1.0f, 0.0f, 0.0f, 1.0f); CL_Draw::texture(gc, src_rect, color, texture_coords); } gc.reset_texture(0); small_font.draw_text(gc, 76, 260, "Texture (0, 250) size=(31,47)"); small_font.draw_text(gc, 76, 275, "Texture (33, 250) size=(31,47) (red, magnify*2)"); // CL_RoundedRect { CL_RoundedRect roundedrect(CL_Sizef(64.0f, 32.0f), 15.0f); float offset_x = 0.0f; float offset_y = 300.0f; CL_Origin origin = origin_top_left; roundedrect.draw(gc, CL_Pointf(offset_x, offset_y), CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f), origin); offset_y = 340.0f; roundedrect.fill(gc, CL_Pointf(offset_x, offset_y), CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f), origin); offset_y = 380.0f; CL_Gradient gradient; gradient.top_left = CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f); gradient.top_right = CL_Colorf(1.0f, 0.0f, 0.0f, 1.0f); gradient.bottom_left = CL_Colorf(0.0f, 0.0f, 1.0f, 1.0f); gradient.bottom_right = CL_Colorf(0.0f, 1.0f, 0.0f, 1.0f); roundedrect.fill(gc, CL_Pointf(offset_x, offset_y), gradient, origin); offset_y = 420.0f; roundedrect.set_control_point_bl(CL_Pointf(0.4f, 0.8f)); roundedrect.set_rounding_bottom_left(CL_Sizef(0.2f, 0.6f)); roundedrect.set_control_point_tl(CL_Pointf(0.2f, 0.4f)); roundedrect.set_rounding_top_left(CL_Sizef(0.4f, 0.2f)); roundedrect.set_control_point_tr(CL_Pointf(0.6f, 0.2f)); roundedrect.set_rounding_top_right(CL_Sizef(0.8f, 0.4f)); roundedrect.set_control_point_br(CL_Pointf(0.6f, 0.8f)); roundedrect.set_rounding_bottom_right(CL_Sizef(0.8f, 0.6f)); roundedrect.fill(gc, CL_Pointf(offset_x, offset_y), gradient, origin); } small_font.draw_text(gc, 76, 310, "RoundedRect - draw (0, 300) size=(64,32)"); small_font.draw_text(gc, 76, 325, "(RoundedRect - draw gradient - is not implemented)"); small_font.draw_text(gc, 76, 350, "RoundedRect - fill (0, 340) size=(64,32)"); small_font.draw_text(gc, 76, 390, "RoundedRect - fill gradient (0, 380) size=(64,32)"); small_font.draw_text(gc, 76, 400, "top left = white. top right = red"); small_font.draw_text(gc, 76, 410, "bottom left = blue. bottom right = green"); small_font.draw_text(gc, 76, 430, "RoundedRect - fill gradient (0, 420) size=(64,32)"); small_font.draw_text(gc, 76, 440, "Controling control / rounding points"); // CL_Sprite { test_base_angle+=5.0f; if (test_base_angle >= 360.0f) { test_base_angle = 0.0f; } #ifndef USE_SWRENDER clEnable(GL_MULTISAMPLE); #endif sprite.set_base_angle(CL_Angle(test_base_angle, cl_degrees)); sprite.draw(gc, 350, 20); sprite.set_base_angle(CL_Angle(0, cl_degrees)); #ifndef USE_SWRENDER clDisable(GL_MULTISAMPLE); #endif } small_font.draw_text(gc, 370, 20, "Sprite - Base angle"); small_font.draw_text(gc, 370, 35, "Multisampling enabled"); { test_angle+=5.0f; if (test_angle >= 360.0f) { test_angle = 0.0f; } sprite.set_angle(CL_Angle(test_angle, cl_degrees)); sprite.draw(gc, 350, 60); sprite.set_angle(CL_Angle(0, cl_degrees)); } small_font.draw_text(gc, 370, 60, "Sprite - Angle"); { test_angle_pitch+=5.0f; if (test_angle_pitch >= 360.0f) { test_angle_pitch = 0.0f; } sprite.set_angle_pitch(CL_Angle(test_angle_pitch, cl_degrees)); sprite.draw(gc, 350, 100); sprite.set_angle_pitch(CL_Angle(0, cl_degrees)); } small_font.draw_text(gc, 370, 100, "Sprite - Angle Pitch"); { test_angle_yaw+=5.0f; if (test_angle_yaw >= 360.0f) { test_angle_yaw = 0.0f; } sprite.set_angle_yaw(CL_Angle(test_angle_yaw, cl_degrees)); sprite.draw(gc, 350, 140); sprite.set_angle_yaw(CL_Angle(0, cl_degrees)); } small_font.draw_text(gc, 370, 140, "Sprite - Angle Yaw"); { if (test_scale_dir) { test_scale += 0.1f; if (test_scale >= 2.0f) { test_scale = 2.0f; test_scale_dir = false; } }else { test_scale -= 0.1f; if (test_scale <= -2.0f) { test_scale = -2.0f; test_scale_dir = true; } } sprite.set_scale(test_scale, 1.0f); sprite.draw(gc, 350, 180); sprite.set_scale(1.0f, test_scale); sprite.draw(gc, 390, 180); sprite.set_scale(1.0f, 1.0f); } small_font.draw_text(gc, 420, 180, "Sprite - Set Scale (x), (y)"); // 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(); } small_font = CL_Font(); } catch(CL_Exception& exception) { CL_Console::write_line("Exception caught:"); CL_Console::write_line(exception.message); // Display the stack trace (if available) std::vector<CL_String> stacktrace = exception.get_stack_trace(); int size = stacktrace.size(); if (size > 0) { CL_Console::write_line("Stack Trace:"); for (int cnt=0; cnt < size; cnt++) { CL_Console::write_line(stacktrace[cnt]); } } console.display_close_message(); return -1; } return 0; }
// 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; }