void HSV::render_texture(Canvas &canvas, ProgramObject &program, Texture &texture, float hue_offset) { GraphicContext gc = canvas.get_gc(); Rectf rect(0.0f, 0.0f, (float)gc.get_width(), (float)gc.get_height()); Rectf texture_unit1_coords(0.0f, 0.0f, 1.0f, 1.0f); Vec2f positions[6] = { Vec2f(rect.left, rect.top), Vec2f(rect.right, rect.top), Vec2f(rect.left, rect.bottom), Vec2f(rect.right, rect.top), Vec2f(rect.left, rect.bottom), Vec2f(rect.right, rect.bottom) }; Vec2f tex1_coords[6] = { Vec2f(texture_unit1_coords.left, texture_unit1_coords.top), Vec2f(texture_unit1_coords.right, texture_unit1_coords.top), Vec2f(texture_unit1_coords.left, texture_unit1_coords.bottom), Vec2f(texture_unit1_coords.right, texture_unit1_coords.top), Vec2f(texture_unit1_coords.left, texture_unit1_coords.bottom), Vec2f(texture_unit1_coords.right, texture_unit1_coords.bottom) }; PrimitivesArray primarray(gc); VertexArrayVector<Vec2f> gpu_positions = VertexArrayVector<Vec2f>(gc, positions, 6); VertexArrayVector<Vec2f> gpu_tex1_coords = VertexArrayVector<Vec2f>(gc, tex1_coords, 6); primarray.set_attributes(0, gpu_positions); primarray.set_attributes(1, gpu_tex1_coords); ProgramUniforms buffer; buffer.cl_ModelViewProjectionMatrix = canvas.get_projection() * canvas.get_modelview(); buffer.HueOffset0 = hue_offset; UniformVector<ProgramUniforms> uniform_vector(gc, &buffer, 1); gc.set_uniform_buffer(0, uniform_vector); gc.set_texture(0, texture); gc.set_program_object(program); gc.draw_primitives(type_triangles, 6, primarray); 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 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); }
// The start of the Application int App::start(const std::vector<std::string> &args) { clan::DisplayWindowDescription 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(clan::Size( 800, 480 ), false); clan::DisplayWindow window(win_desc); clan::SlotContainer cc; cc.connect(window.sig_window_close(), clan::bind_member(this, &App::on_window_close)); cc.connect(window.get_ic().get_keyboard().sig_key_up(), clan::bind_member(this, &App::on_input_up)); std::string theme; if (clan::FileHelp::file_exists("../../../Resources/GUIThemeAero/theme.css")) theme = "../../../Resources/GUIThemeAero"; else if (clan::FileHelp::file_exists("../../../Resources/GUIThemeBasic/theme.css")) theme = "../../../Resources/GUIThemeBasic"; else throw clan::Exception("No themes found"); clan::GUIWindowManagerTexture wm(window); clan::GUIManager gui(wm, theme); clan::Canvas canvas(window); // Deleted automatically by the GUI Options *options = new Options(gui, clan::Rect(0, 0, canvas.get_size())); clan::Image image_grid(canvas, "../Blend/Resources/grid.png"); clan::Texture2D texture_particle(canvas, "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(); clan::ShaderObject vertex_shader(canvas, clan::shadertype_vertex, text_shader_vertex); if(!vertex_shader.compile()) { throw clan::Exception(clan::string_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log())); } clan::ShaderObject fragment_shader(canvas, clan::shadertype_fragment, text_shader_fragment); if(!fragment_shader.compile()) { throw clan::Exception(clan::string_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log())); } clan::ProgramObject program_object(canvas); 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 clan::Exception(clan::string_format("Unable to link program object: %1", program_object.get_info_log())); } program_object.set_uniform1i("Texture0", 0); options->request_repaint(); clan::BlendStateDescription blend_state_desc; blend_state_desc.enable_blending(true); blend_state_desc.set_blend_function(clan::blend_src_alpha, clan::blend_one, clan::blend_src_alpha, clan::blend_one); clan::BlendState blend_state(canvas, blend_state_desc); clan::GameTime game_time; while (!quit) { game_time.update(); wm.process(); wm.draw_windows(canvas); int num_particles = options->num_particles; if (num_particles > max_particles) num_particles = max_particles; move_particles(game_time.get_time_elapsed(), num_particles); const float grid_xpos = 10.0f; const float grid_ypos = 10.0f; // Draw the grid image_grid.draw(canvas, grid_xpos, grid_ypos); if (num_particles > 0) { std::vector<clan::Vec2f> positions; std::vector<clan::Colorf> colors; positions.resize(num_particles); colors.resize(num_particles); for (int cnt=0; cnt<num_particles; cnt++) { positions[cnt] = clan::Vec2f(grid_xpos + particles[cnt].xpos, grid_ypos + particles[cnt].ypos); switch (cnt % 3) { case 0: colors[cnt] = clan::Colorf(1.0f, 0.0f, 0.0f, 1.0f); break; case 1: colors[cnt] = clan::Colorf(0.0f, 1.0f, 0.0f, 1.0f); break; case 2: colors[cnt] = clan::Colorf(0.0f, 0.0f, 1.0f, 1.0f); break; } }; canvas.flush(); clan::GraphicContext gc = canvas.get_gc(); canvas.set_blend_state(blend_state); clan::RasterizerStateDescription raster_state_desc; raster_state_desc.set_point_size(options->point_size); raster_state_desc.set_point_sprite_origin(clan::origin_upper_left); clan::RasterizerState raster_state(canvas, raster_state_desc); canvas.set_rasterizer_state(raster_state); clan::PrimitivesArray primarray(gc); clan::VertexArrayVector<clan::Vec2f> gpu_positions = clan::VertexArrayVector<clan::Vec2f>(gc, &positions[0], positions.size()); clan::VertexArrayVector<clan::Colorf> gpu_colors = clan::VertexArrayVector<clan::Colorf>(gc, &colors[0], colors.size()); primarray.set_attributes(0, gpu_positions); primarray.set_attributes(1, gpu_colors); ProgramUniforms buffer; buffer.cl_ModelViewProjectionMatrix = canvas.get_projection() * canvas.get_modelview(); clan::UniformVector<ProgramUniforms> uniform_vector(gc, &buffer, 1); gc.set_uniform_buffer(0, uniform_vector); gc.set_texture(0, texture_particle); gc.set_program_object(program_object); gc.draw_primitives(clan::type_points, num_particles, primarray); gc.reset_program_object(); gc.reset_texture(0); gc.reset_blend_state(); gc.reset_rasterizer_state(); } window.flip(1); clan::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_); } }
bool App::update() { game_time.update(); options->set_needs_render(); options->set_rect(clan::Size(canvas.get_size())); options->update(clan::Colorf(0.6f, 0.6f, 0.2f, 1.0f)); int num_particles = options->num_particles; if (num_particles > max_particles) num_particles = max_particles; move_particles(game_time.get_time_elapsed(), num_particles); const float grid_xpos = 10.0f; const float grid_ypos = 10.0f; // Draw the grid image_grid.draw(canvas, grid_xpos, grid_ypos); if (num_particles > 0) { std::vector<clan::Vec2f> positions; std::vector<clan::Colorf> colors; positions.resize(num_particles); colors.resize(num_particles); for (int cnt=0; cnt<num_particles; cnt++) { positions[cnt] = clan::Vec2f(grid_xpos + particles[cnt].xpos, grid_ypos + particles[cnt].ypos); switch (cnt % 3) { case 0: colors[cnt] = clan::Colorf(1.0f, 0.0f, 0.0f, 1.0f); break; case 1: colors[cnt] = clan::Colorf(0.0f, 1.0f, 0.0f, 1.0f); break; case 2: colors[cnt] = clan::Colorf(0.0f, 0.0f, 1.0f, 1.0f); break; } }; canvas.flush(); clan::GraphicContext gc = canvas.get_gc(); canvas.set_blend_state(blend_state); clan::RasterizerStateDescription raster_state_desc; raster_state_desc.set_point_size(options->point_size); raster_state_desc.set_point_sprite_origin(clan::origin_upper_left); clan::RasterizerState raster_state(canvas, raster_state_desc); canvas.set_rasterizer_state(raster_state); clan::PrimitivesArray primarray(gc); clan::VertexArrayVector<clan::Vec2f> gpu_positions = clan::VertexArrayVector<clan::Vec2f>(gc, &positions[0], positions.size()); clan::VertexArrayVector<clan::Colorf> gpu_colors = clan::VertexArrayVector<clan::Colorf>(gc, &colors[0], colors.size()); primarray.set_attributes(0, gpu_positions); primarray.set_attributes(1, gpu_colors); ProgramUniforms buffer; buffer.cl_ModelViewProjectionMatrix = canvas.get_projection() * canvas.get_transform(); clan::UniformVector<ProgramUniforms> uniform_vector(gc, &buffer, 1); gc.set_uniform_buffer(0, uniform_vector); gc.set_texture(0, texture_particle); gc.set_program_object(program_object); gc.draw_primitives(clan::type_points, num_particles, primarray); gc.reset_program_object(); gc.reset_texture(0); gc.reset_blend_state(); gc.reset_rasterizer_state(); } window.flip(1); return !quit; }