void CL_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.set_program_object(cl_program_single_texture); gc.draw_primitives(cl_triangles, 6, prim_array); gc.reset_program_object(); }
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(); }
/** * Draws the polygon on the graphics context. * * @param gc Graphics context to draw the polygon on. */ void Polygon::draw(CL_GraphicContext gc) { //Only draw if more than one point exists. if(_points.size() > 1) { //CL_Vec4f green_color(1.0f, 1.0f, 1.0f, 1.0f); CL_Vec2i points_array[_points.size()]; std::list<CL_Pointf>::iterator it_point; int n = 0; for(it_point = _points.begin(); it_point != _points.end(); ++it_point) { points_array[n] = static_cast<CL_Vec2i>(*(it_point)); n++; } CL_PrimitivesArray poly(gc); poly.set_attributes(0, points_array); poly.set_attribute(1, _line_color); //scene->get_world()->get_gc()->get_polygon_rasterizer().set_face_fill_mode_front(cl_fill_polygon); gc.set_program_object(cl_program_color_only); gc.draw_primitives(cl_line_loop,n,poly); gc.reset_program_object(); } }
void ShaderBumpMap::Use(CL_GraphicContext &gc) { if (!material_updated) { material_updated = true; program_object.set_uniform1f("MaterialShininess", material_shininess); program_object.set_uniform4f("MaterialEmission", material_emission); program_object.set_uniform4f("MaterialSpecular", material_specular); program_object.set_uniform4f("MaterialAmbient", material_ambient); } if (!light_updated) { light_updated = true; program_object.set_uniform4f("LightVector", light_vector); CL_Vec4f light_halfvector(0.0f, 0.0f, 1.0f, 0.0f); light_halfvector += light_vector; light_halfvector.normalize3(); program_object.set_uniform4f("LightHalfVector", light_halfvector); program_object.set_uniform4f("LightSpecular", light_specular); program_object.set_uniform4f("LightDiffuse", light_diffuse); program_object.set_uniform4f("LightAmbient", light_ambient); } gc.set_program_object(program_object); }
void ShaderDepth::Use(CL_GraphicContext &gc) { if (!material_updated) { material_updated = true; program_object.set_uniform4f("MaterialAmbient", material_ambient); } gc.set_program_object(program_object); }
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 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 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 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 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 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 ShaderColor::Use(CL_GraphicContext &gc) { if (!material_updated) { material_updated = true; program_object.set_uniform1f("MaterialShininess", material_shininess); program_object.set_uniform4f("MaterialEmission", material_emission); program_object.set_uniform4f("MaterialSpecular", material_specular); program_object.set_uniform4f("MaterialAmbient", material_ambient); } if (!light_updated) { light_updated = true; program_object.set_uniform3f("LightVector", light_vector); CL_Vec3f light_halfvector(0.0f, 0.0f, 1.0f); light_halfvector += light_vector; light_halfvector.normalize(); program_object.set_uniform3f("LightHalfVector", light_halfvector); program_object.set_uniform4f("LightSpecular", light_specular); program_object.set_uniform4f("LightDiffuse", light_diffuse); } if (!spot_light_updated) { spot_light_updated = true; program_object.set_uniform3f("SpotLight_Position", spot_light_position); program_object.set_uniform3f("SpotLight_Direction", spot_light_direction); program_object.set_uniform4f("SpotLight_Specular", spot_light_specular); program_object.set_uniform4f("SpotLight_Diffuse", spot_light_diffuse); float cos_cutoff = cos(spot_light_cutoff * CL_PI / 180.0f); program_object.set_uniform1f("SpotLight_SpotCosCutoff", cos_cutoff); program_object.set_uniform1f("SpotLight_SpotExponent", spot_light_exponent); } gc.set_program_object(program_object); }
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 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); }
int App::start(const std::vector<CL_String> &args) { CL_OpenGLWindowDescription description; description.set_title("GLSL Test"); description.set_size(CL_Size(1024, 768), true); CL_DisplayWindow window(description); CL_InputDevice keyboard = window.get_ic().get_keyboard(); CL_GraphicContext gc = window.get_gc(); CL_Slot slot_window_close = window.sig_window_close().connect(this, &App::window_close); // Load and link shaders CL_ProgramObject shader = CL_ProgramObject::load(gc, "Resources/vertex_shader.glsl", "Resources/fragment_shader.glsl"); shader.bind_attribute_location(0, "Position"); if (!shader.link()) throw CL_Exception("Unable to link shader program: Error:" + shader.get_info_log()); quit = false; while (!keyboard.get_keycode(CL_KEY_ESCAPE) && !quit ) { gc.clear(); gc.set_program_object(shader, cl_program_matrix_modelview_projection); //shader.set_uniform1i("SourceTexture", 0); int xpos = 0; int size = 16; int gap = size + 2; //-------- Test 1 CL_Mat2f matrix2_a = CL_Mat2f(3, 1, 2, 4); CL_Mat2f matrix2_b = CL_Mat2f(-3, 7, 2, 5); CL_Mat2f matrix2_result = CL_Mat2f::multiply(matrix2_a, matrix2_b); CL_Mat2f matrix2_a_temp = matrix2_a; if (CL_Mat2i(matrix2_result) != CL_Mat2i(matrix2_a * matrix2_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 1); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform_matrix("matrix2_b", matrix2_b); shader.set_uniform_matrix("matrix2_result", matrix2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 2 CL_Mat3f matrix3_a = CL_Mat3f(3, 1, 2, 4, 5 ,6, 4, 2, 1); CL_Mat3f matrix3_b = CL_Mat3f(4, 7, 2, 5, 3, 5, 2, 9, 3); CL_Mat3f matrix3_result = CL_Mat3f::multiply(matrix3_a, matrix3_b); CL_Mat3f matrix3_a_temp = matrix3_a; if (CL_Mat3i(matrix3_result) != CL_Mat3i(matrix3_a * matrix3_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 2); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform_matrix("matrix3_b", matrix3_b); shader.set_uniform_matrix("matrix3_result", matrix3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 3 static float test_a_values[] = {3, 1, 2, 4, 5 ,6, 4, 2, 1, 4, 6, 7, 6, 3, 7, 2}; static float test_b_values[] = {4, 7, 2, 5, 3, 5, 2, 9, 3, 3, 6, 9, 2, 4, 6, 2}; CL_Mat4f matrix4_a = CL_Mat4f(test_a_values); CL_Mat4f matrix4_b = CL_Mat4f(test_b_values); CL_Mat4f matrix4_result = CL_Mat4f::multiply(matrix4_a, matrix4_b); CL_Mat4f matrix4_a_temp = matrix4_a; if (CL_Mat4i(matrix4_result) != CL_Mat4i(matrix4_a * matrix4_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 3); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform_matrix("matrix4_b", matrix4_b); shader.set_uniform_matrix("matrix4_result", matrix4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 4 matrix2_result = CL_Mat2f::subtract(matrix2_a, matrix2_b); matrix2_a_temp = matrix2_a; if (CL_Mat2i(matrix2_result) != CL_Mat2i(matrix2_a - matrix2_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 4); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform_matrix("matrix2_b", matrix2_b); shader.set_uniform_matrix("matrix2_result", matrix2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 5 matrix3_result = CL_Mat3f::subtract(matrix3_a, matrix3_b); matrix3_a_temp = matrix3_a; if (CL_Mat3i(matrix3_result) != CL_Mat3i(matrix3_a - matrix3_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 5); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform_matrix("matrix3_b", matrix3_b); shader.set_uniform_matrix("matrix3_result", matrix3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 6 matrix4_result = CL_Mat4f::subtract(matrix4_a, matrix4_b); matrix4_a_temp = matrix4_a; if (CL_Mat4i(matrix4_result) != CL_Mat4i(matrix4_a - matrix4_b)) throw CL_Exception("Failure"); shader.set_uniform1i("test_id", 6); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform_matrix("matrix4_b", matrix4_b); shader.set_uniform_matrix("matrix4_result", matrix4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 7 CL_Vec2f vector2_a(2,3); CL_Vec2f vector2_result = vector2_a * matrix2_a; shader.set_uniform1i("test_id", 7); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform2f("vector2_a", vector2_a); shader.set_uniform2f("vector2_result", vector2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 8 vector2_result = matrix2_a * vector2_a; shader.set_uniform1i("test_id", 8); shader.set_uniform_matrix("matrix2_a", matrix2_a); shader.set_uniform2f("vector2_a", vector2_a); shader.set_uniform2f("vector2_result", vector2_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 9 CL_Vec3f vector3_a(3,5,6); CL_Vec3f vector3_result = vector3_a * matrix3_a; shader.set_uniform1i("test_id", 9); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform3f("vector3_a", vector3_a); shader.set_uniform3f("vector3_result", vector3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 10 vector3_result = matrix3_a * vector3_a; shader.set_uniform1i("test_id", 10); shader.set_uniform_matrix("matrix3_a", matrix3_a); shader.set_uniform3f("vector3_a", vector3_a); shader.set_uniform3f("vector3_result", vector3_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 11 CL_Vec4f vector4_a(4,3,5,6); CL_Vec4f vector4_result = vector4_a * matrix4_a; shader.set_uniform1i("test_id", 11); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform4f("vector4_a", vector4_a); shader.set_uniform4f("vector4_result", vector4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; //-------- Test 12 vector4_result = matrix4_a * vector4_a; shader.set_uniform1i("test_id", 12); shader.set_uniform_matrix("matrix4_a", matrix4_a); shader.set_uniform4f("vector4_a", vector4_a); shader.set_uniform4f("vector4_result", vector4_result); draw(gc, CL_Rect(xpos, 0, CL_Size(size, size))); xpos+=gap; gc.reset_program_object(); window.flip(); CL_System::sleep(10); CL_KeepAlive::process(); } return 0; }
void ContainerRenderer::render(CL_GraphicContext& gc) { renderQueue_->preRender(); gc.clear(CL_Colorf(0.f, 0.f, 0.f, 0.f)); boost::shared_ptr<CL_ProgramObject> shader = ui::Manager::getShaderManager()->getGumpShader(); gc.set_program_object(*shader, cl_program_matrix_modelview_projection); CL_Texture huesTexture = data::Manager::getHuesLoader()->getHuesTexture(); gc.set_texture(0, huesTexture); // set texture unit 1 active to avoid overriding the hue texture with newly loaded object textures gc.set_texture(1, huesTexture); shader->set_uniform1i("HueTexture", 0); shader->set_uniform1i("ObjectTexture", 1); RenderQueue::const_iterator igIter = renderQueue_->begin(); RenderQueue::const_iterator igEnd = renderQueue_->end(); CL_Vec2f vertexCoords[6]; bool renderingComplete = true; // draw background container texture boost::shared_ptr<ui::Texture> bgTex = containerView_->getBackgroundTexture(); if (bgTex && bgTex->isReadComplete()) { CL_Vec3f hueInfo(0, 0, 1); CL_Rectf rect(0, 0, CL_Sizef(bgTex->getWidth(), bgTex->getHeight())); vertexCoords[0] = CL_Vec2f(rect.left, rect.top); vertexCoords[1] = CL_Vec2f(rect.right, rect.top); vertexCoords[2] = CL_Vec2f(rect.left, rect.bottom); vertexCoords[3] = CL_Vec2f(rect.right, rect.top); vertexCoords[4] = CL_Vec2f(rect.left, rect.bottom); vertexCoords[5] = CL_Vec2f(rect.right, rect.bottom); CL_Rectf texCoordHelper = bgTex->getNormalizedTextureCoords(); CL_Vec2f texCoords[6] = { CL_Vec2f(texCoordHelper.left, texCoordHelper.top), CL_Vec2f(texCoordHelper.right, texCoordHelper.top), CL_Vec2f(texCoordHelper.left, texCoordHelper.bottom), CL_Vec2f(texCoordHelper.right, texCoordHelper.top), CL_Vec2f(texCoordHelper.left, texCoordHelper.bottom), CL_Vec2f(texCoordHelper.right, texCoordHelper.bottom) }; CL_PrimitivesArray primarray(gc); primarray.set_attributes(0, vertexCoords); primarray.set_attributes(1, texCoords); primarray.set_attribute(2, hueInfo); gc.set_texture(1, bgTex->getTexture()); gc.draw_primitives(cl_triangles, 6, primarray); } else { renderingComplete = false; } for (; igIter != igEnd; ++igIter) { boost::shared_ptr<world::IngameObject> curObj = *igIter; // just items in a container if (!curObj->isDynamicItem()) { continue; } // check if texture is ready to be drawn boost::shared_ptr<ui::Texture> tex = curObj->getIngameTexture(); if (!tex) { continue; } if (!tex->isReadComplete()) { renderingComplete = false; continue; } CL_Rectf rect(curObj->getLocXDraw(), curObj->getLocYDraw(), CL_Sizef(tex->getWidth(), tex->getHeight())); vertexCoords[0] = CL_Vec2f(rect.left, rect.top); vertexCoords[1] = CL_Vec2f(rect.right, rect.top); vertexCoords[2] = CL_Vec2f(rect.left, rect.bottom); vertexCoords[3] = CL_Vec2f(rect.right, rect.top); vertexCoords[4] = CL_Vec2f(rect.left, rect.bottom); vertexCoords[5] = CL_Vec2f(rect.right, rect.bottom); CL_Rectf texCoordHelper = tex->getNormalizedTextureCoords(); CL_Vec2f texCoords[6] = { CL_Vec2f(texCoordHelper.left, texCoordHelper.top), CL_Vec2f(texCoordHelper.right, texCoordHelper.top), CL_Vec2f(texCoordHelper.left, texCoordHelper.bottom), CL_Vec2f(texCoordHelper.right, texCoordHelper.top), CL_Vec2f(texCoordHelper.left, texCoordHelper.bottom), CL_Vec2f(texCoordHelper.right, texCoordHelper.bottom) }; CL_PrimitivesArray primarray(gc); primarray.set_attributes(0, vertexCoords); primarray.set_attributes(1, texCoords); primarray.set_attribute(2, curObj->getHueInfo(false)); gc.set_texture(1, tex->getTexture()); gc.draw_primitives(cl_triangles, 6, primarray); } gc.reset_textures(); gc.reset_program_object(); renderQueue_->postRender(renderingComplete); if (!renderingComplete) { ui::Manager::getSingleton()->queueComponentRepaint(containerView_); } }
void ShaderColorGeometry::Use(CL_GraphicContext &gc) { gc.set_program_object(program_object); }
// 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; 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; }
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(); } }
void Shader::Use(CL_GraphicContext &gc) { gc.set_program_object(program_object); }