void TileMap::set_cur_x(CL_GraphicContext& gc, int x) { if (x < 0) x = 0; else if (get_width() - x < gc.get_width()) x = get_width() - gc.get_width(); cur_map_x = x; }
//Fonction à appeller à chaque changement de résolution void Game::resizeGLScene() { CL_GraphicContext *gc = CL_Display::get_current_window()->get_gc(); glViewport(0, 0, (GLsizei) gc->get_width(), (GLsizei) gc->get_height()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat) gc->get_width() / (GLfloat) gc->get_height(), 0.1f, 500.f); glMatrixMode(GL_MODELVIEW); }
void App::render(CL_GraphicContext &gc) { gc.clear(CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f)); scene.gs->image_grid.draw(gc, 420.0f, 120.0f); // Draw a grid in the backgound gc.set_map_mode(cl_user_projection); CL_Rect viewport_rect(0, 0, CL_Size(gc.get_width(), gc.get_height())); gc.set_viewport(viewport_rect); gc.set_projection(scene.gs->camera_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(true); gc.set_buffer_control(buffer_control); gc.clear_depth(1.0f); CL_Mat4f modelview_matrix = scene.gs->camera_modelview; scene.Draw(modelview_matrix, gc); gc.reset_program_object(); }
void Viewport::prepareGC(CL_GraphicContext &p_gc) { p_gc.push_modelview(); if (m_impl->m_attachPoint != NULL) { // calculate world clip rect const int stageWidth = Gfx::Stage::getWidth(); const int stageHeight = Gfx::Stage::getHeight(); m_impl->m_worldClipRect.left = m_impl->m_attachPoint->x - stageWidth / 2 / m_impl->m_scale; m_impl->m_worldClipRect.top = m_impl->m_attachPoint->y - stageHeight / 2 / m_impl->m_scale; m_impl->m_worldClipRect.right = m_impl->m_worldClipRect.left + stageWidth / m_impl->m_scale; m_impl->m_worldClipRect.bottom = m_impl->m_worldClipRect.top + stageHeight / m_impl->m_scale; } // apply new scale const float horizScale = p_gc.get_width() / m_impl->m_worldClipRect.get_width(); const float vertScale = p_gc.get_height() / m_impl->m_worldClipRect.get_height(); p_gc.mult_scale(horizScale, vertScale); // apply translations p_gc.mult_translate(-m_impl->m_worldClipRect.left, -m_impl->m_worldClipRect.top); }
void App::calculate_matricies(CL_GraphicContext &gc) { scene.gs->camera_projection = CL_Mat4f::perspective(45.0f, ((float) gc.get_width()) / ((float) gc.get_height()), 0.1f, 1000.0f); scene.gs->camera_modelview = CL_Mat4f::identity(); camera->GetWorldMatrix(scene.gs->camera_modelview); scene.gs->camera_modelview.scale_self(1.0f, 1.0f, -1.0f); scene.gs->camera_modelview.inverse(); }
GSLobby::GSLobby(CL_GraphicContext& gc, CL_ResourceManager& resources) : m_gsgame(new GSGame(gc, resources)){ CL_FontDescription desc; desc.set_typeface_name("tahoma"); desc.set_height(32); m_font.reset(new CL_Font_System(gc, desc)); m_xpos = gc.get_width()/2-100; m_ypos = gc.get_height()/4; m_playbtn.initialize(gc, resources, "game_lobby/play_btn", CL_Vec2<float>(m_xpos, m_ypos+100)); m_backbtn.initialize(gc, resources, "game_lobby/back_btn", CL_Vec2<float>(m_xpos, m_ypos+200)); }
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 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 TileMap::draw(CL_GraphicContext &gc) { int screen_width = gc.get_width(); int screen_height = gc.get_height(); int start_tile_x = cl_max(0, current_map_position_x / tile_width); int start_tile_y = cl_max(0, current_map_position_y / tile_height); int scrolling_pixel_offset_x = current_map_position_x - start_tile_x * tile_width; int scrolling_pixel_offset_y = current_map_position_y - start_tile_y * tile_height; int tiles_on_screen_horizontally = screen_width / tile_width + 1; int tiles_on_screen_vertically = screen_height / tile_height + 1; // since we might show half tiles on edges, make sure we display one more tile to fill screen tiles_on_screen_horizontally++; tiles_on_screen_vertically++; // make sure we don't draw more than the level size if(tiles_on_screen_horizontally + start_tile_x > map_width) tiles_on_screen_horizontally = map_width - start_tile_x; if(tiles_on_screen_vertically + start_tile_y > map_height) tiles_on_screen_vertically = map_height - start_tile_y; for(size_t l = 0; l < layers.size(); ++l) { for(int current_tile_y = 0; current_tile_y < tiles_on_screen_vertically; ++current_tile_y) { for(int current_tile_x = 0; current_tile_x < tiles_on_screen_horizontally; ++current_tile_x) { int index = (start_tile_y + current_tile_y) * map_width + (start_tile_x + current_tile_x); int sprite_index = layers[l].map[index]; int tile_position_screen_x = current_tile_x * tile_width - scrolling_pixel_offset_x; int tile_position_screen_y = current_tile_y * tile_height - scrolling_pixel_offset_y; sprite_tiles.set_frame(sprite_index); sprite_tiles.draw(gc, tile_position_screen_x, tile_position_screen_y); } } } }
void App::calculate_matricies(CL_GraphicContext &gc) { scene.gs->camera_projection = CL_Mat4f::perspective(45.0f, ((float) gc.get_width()) / ((float) gc.get_height()), 0.1f, 1000.0f); float ortho_size = 60.0f / 2.0f; scene.gs->light_projection = CL_Mat4f::ortho(-ortho_size, ortho_size, -ortho_size, ortho_size, 0.1f, 1000.0f); scene.gs->camera_modelview = CL_Mat4f::identity(); camera->GetWorldMatrix(scene.gs->camera_modelview); scene.gs->camera_modelview.scale_self(1.0f, 1.0f, -1.0f); scene.gs->camera_modelview.inverse(); scene.gs->light_modelview = CL_Mat4f::identity(); light->GetWorldMatrix(scene.gs->light_modelview); scene.gs->light_modelview.scale_self(1.0f, 1.0f, -1.0f); scene.gs->light_modelview.inverse(); scene.gs->shadow_matrix = scene.gs->light_modelview * scene.gs->light_projection; }
bool GUI::run() { static int total_time = 0, fps_count = 0, last_fps= 0; static int start_time = 0; if (start_time == 0) { start_time = CL_System::get_time(); } CL_GraphicContext gc = app->get_window()->get_gc(); gc.set_map_mode(CL_MapMode(cl_map_2d_upper_left)); CL_Draw::gradient_fill(gc, app->get_window()->get_viewport(), CL_Gradient(CL_Colorf(0.4f, 0.4f, 0.4f, 1.0f), CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f))); run_manager(gc); CL_String fps = cl_format("FPS: %1", last_fps); fps_font.draw_text(gc, gc.get_width() - 100 - 2, 24 - 2, fps, CL_Colorf(0.0f, 0.0f, 0.0f, 1.0f)); fps_font.draw_text(gc, gc.get_width() - 100, 24, fps, CL_Colorf::white); fps_font.draw_text(gc, 24, gc.get_height() - 16, "Rendering GUI onto a CL_Texture, then onto the display window.", CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f)); fps_count++; int time = CL_System::get_time(); total_time += time - start_time; start_time = time; if(total_time >= 1000) { last_fps = fps_count; total_time -= 1000; fps_count = 0; } balls.Run(gc); CL_KeepAlive::process(); app->get_window()->flip(0); return true; }
void App::calculate_matrix(CL_GraphicContext &gc) { float lens_zoom = 3.2f; float lens_near = 0.1f; float lens_far = 10000.0f; float lens_aspect = 1.0f; float fov = 2.0f * atan2(1.0f, lens_zoom); float aspect = 1.0f; float width = (float) gc.get_width(); float height = (float) gc.get_height(); if (height) aspect = ( width * lens_aspect) / height; fov = (fov * 180.0f) / CL_PI; projection_matrix = CL_Mat4f::perspective( fov, aspect, lens_near, lens_far); modelview_matrix = CL_Mat4f::identity(); modelview_matrix.matrix[2 + (4*2)] = -1.0f; }
void Viewport::prepareGC(CL_GraphicContext &p_gc) { p_gc.push_modelview(); if (m_attachPoint != NULL) { const int stageWidth = Gfx::Stage::getWidth(); const int stageHeight = Gfx::Stage::getHeight(); m_x = m_attachPoint->x - stageWidth / 2 / m_scale; m_y = m_attachPoint->y - stageHeight / 2 / m_scale; m_width = stageWidth / m_scale; m_height = stageHeight / m_scale; } const float horizScale = p_gc.get_width() / m_width; const float vertScale = p_gc.get_height() / m_height; p_gc.mult_scale(horizScale, vertScale); p_gc.mult_translate(-m_x, -m_y); }
void App::create_shooter( const CL_InputEvent &key, const CL_StringRef &str, bool use_red, bool use_green, bool use_blue) { // Set the firing line CL_Vec3f vector(0.0f, 0.0f, 20.0f); CL_GraphicContext gc = window.get_gc(); float width = (float) gc.get_width(); float height = (float) gc.get_height(); vector.x = ((key.mouse_pos.x - width/2)* 10.0f) / width; vector.y = -((key.mouse_pos.y - height/2) * 10.0f) / height; // Create the text offset TextShooter shooter(vector_font, str); shooter.set_start_time(CL_System::get_time()); shooter.set_duration(2 * 1000); shooter.set_initial_white_time(1000); shooter.set_end_fade_time(1000); shooter.set_start_position(CL_Vec3f(0.0f, 0.0f, 0.2f)); shooter.set_end_position(vector); shooter.set_color_component(use_red, use_green, use_blue); text_shooter.push_back(shooter); }
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); }
void TileMap::draw(CL_GraphicContext& gc) { int screen_width = gc.get_width(); int screen_height = gc.get_height(); int horiz_tiles = screen_width / tile_width + 1; int vert_tiles = screen_height / tile_height + 1; // avoid half tiles by increasing by one horiz_tiles++; vert_tiles++; int start_tilex = cl_max(0, cur_map_x / tile_width); int start_tiley = cl_max(0, cur_map_y / tile_height); int scroll_offsetx = cur_map_x - start_tilex * tile_width; int scroll_offsety = cur_map_y - start_tiley * tile_width; if (horiz_tiles + start_tilex > map_width) horiz_tiles = map_width - start_tilex; if (vert_tiles + start_tiley > map_height) vert_tiles = map_height - start_tiley; for (MapLayer& layer : layers) { for (int cur_y = 0; cur_y < vert_tiles; ++cur_y) { for (int cur_x = 0; cur_x < horiz_tiles; ++cur_x) { int idx = (start_tiley + cur_y) * map_width + start_tilex + cur_x; int sprite_idx = layer.map[idx]; int tile_xpos = cur_x * tile_width - scroll_offsetx; int tile_ypos = cur_y * tile_height - scroll_offsety; tiles.set_frame(sprite_idx); tiles.draw(gc, tile_xpos, tile_ypos); } } } }
// The start of the Application int App::start(const std::vector<CL_String> &args) { quit = false; // Set the window CL_DisplayWindowDescription desc; desc.set_title("ClanLib Worker GC Example"); desc.set_size(CL_Size(640, 480), true); desc.set_allow_resize(true); CL_DisplayWindow window(desc); CL_OpenGL::set_active(NULL); // wglCreateContextAttribsARB will fail if the shared context is current in a different thread. WorkerGC worker_gc; CL_System::sleep(200); // Fixme // 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(); worker_gc.Start("../../../Examples/Game/DiceWar/Resources/lobby_background1.png"); CL_Sprite spr_logo; float sin_count = 0.0f; float ypos = 0.0f; float ydir = 0.3f; unsigned int last_time = CL_System::get_time(); // Run until someone presses escape while (!quit) { unsigned int current_time = CL_System::get_time(); float time_delta_ms = static_cast<float> (current_time - last_time); last_time = current_time; if (!spr_logo.is_null()) { gc.clear(CL_Colorf(0.0f,0.0f,0.2f)); spr_logo.draw(gc, (float) gc.get_width()-spr_logo.get_width(), (float) gc.get_height()-spr_logo.get_height()); } else { gc.clear(CL_Colorf(0.5f,0.0f,0.0f)); } // Move the lines ypos += ydir * time_delta_ms; if (ydir > 0.0f) { if ((ypos+200.0f) >= gc.get_height()) { ypos = (float) (gc.get_height() - 200); ydir *= -1.0f; } } else { if (ypos <= 0.0f) { ypos = 0.0f; ydir *= -1.0f; } } CL_Draw::fill(gc, 0, ypos-1.0f, (float) gc.get_width(), ypos-8.0f,CL_Colorf(1.0f, 1.0f, 1.0f)); CL_Draw::fill(gc, 0, ypos+198.0f, (float) gc.get_width(), ypos+190.0f, CL_Colorf(1.0f, 1.0f, 1.0f)); // Flip the display, showing on the screen what we have drawed // since last call to flip() window.flip(1); if (worker_gc.IsReady()) { spr_logo = worker_gc.Get(); } // This call processes user input and other events CL_KeepAlive::process(0); } return 0; }
// The start of the Application int App::start(const std::vector<CL_String> &args) { // Setup the window CL_DisplayWindowDescription win_desc; win_desc.set_allow_resize(true); win_desc.set_title("Input Example"); win_desc.set_size(CL_Size( 700, 700 ), false); window = CL_DisplayWindow(win_desc); // Connect the slots that we require CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); CL_Slot slot_input_down = (window.get_ic().get_keyboard()).sig_key_down().connect(this, &App::on_input_down); CL_Slot slot_mouse_down = (window.get_ic().get_mouse()).sig_key_down().connect(this, &App::on_mouse_down); CL_Slot slot_mouse_dblclick = (window.get_ic().get_mouse()).sig_key_dblclk().connect(this, &App::on_mouse_down); std::vector<CL_Slot> slot_joystick; int max_joysticks = window.get_ic().get_joystick_count(); for (int joystick_number=0; joystick_number < max_joysticks; joystick_number++) { CL_Slot current_joystick = window.get_ic().get_joystick(joystick_number).sig_key_down().connect(this, &App::on_joystick_down, joystick_number); slot_joystick.push_back(current_joystick); } CL_GraphicContext gc = window.get_gc(); font = CL_Font(gc, "tahoma", 16); vector_font = CL_Font_Vector("../../Game/DiceWar/Resources/bitstream_vera_sans/VeraBd.ttf", 256); calculate_matrix(gc); while(!quit) { gc.set_map_mode(cl_map_2d_upper_left); CL_Draw::gradient_fill(gc, CL_Rect(0, 0, gc.get_width(), gc.get_height()/2), CL_Gradient(CL_Colorf(0.2f, 0.2f, 0.8f, 1.0f), CL_Colorf(0.0f, 0.0f, 0.2f, 1.0f))); CL_Draw::gradient_fill(gc, CL_Rect(0, gc.get_height()/2, gc.get_width(), gc.get_height()), CL_Gradient(CL_Colorf(0.0f, 0.0f, 0.2f, 1.0f), CL_Colorf(0.2f, 0.2f, 0.8f, 1.0f))); font.draw_text(gc, 8, 20, "Press any key, mouse button or joystick button to fire text. Use mouse to control direction."); int yoffset = gc.get_height() - 20; const int y_gap = 20; // Draw Keyboard Information draw_keyboard_state(gc, yoffset); yoffset -= y_gap; // Draw Mouse Information draw_mouse_state(gc, yoffset); yoffset -= y_gap; // Draw Joysticks Information for (int joystick_number=0; joystick_number < max_joysticks; joystick_number++) { draw_joystick_state(gc, joystick_number, yoffset); yoffset -= y_gap; } // Draw Tablet Information int max_tablets = window.get_ic().get_tablet_count(); for (int tablet_number=0; tablet_number < max_tablets; tablet_number++) { draw_tablet_state(gc, tablet_number, yoffset); yoffset -= y_gap; } gc.set_map_mode(cl_user_projection); gc.set_projection(projection_matrix); gc.set_modelview(modelview_matrix); draw_text_shooter(gc); window.flip(1); CL_KeepAlive::process(); } return 0; }
bool TextShooter::draw(CL_GraphicContext &gc, unsigned int current_time) { int time_delta = current_time - start_time; if (time_delta < 0) // Not on screen return true; if (time_delta >= duration) // Reached destination return false; CL_Vec3f current_position; // Get position current_position.x = start_position.x + (((end_position.x - start_position.x) * time_delta) / duration); current_position.y = start_position.y + (((end_position.y - start_position.y) * time_delta) / duration); current_position.z = start_position.z + (((end_position.z - start_position.z) * time_delta) / duration); // Get fade towards end float font_alpha = (float) (duration - time_delta); font_alpha /= end_fade_time; if (font_alpha > 1.0f) font_alpha = 1.0f; // Get initial white colour setting float font_white = (float) time_delta; font_white /= initial_white_time; if (font_white > 1.0f) font_white = 1.0f; font_white = 1.0f - font_white; // Calculate color CL_Colorf font_color; if (use_red_component) { font_color.r = 1.0f; } else { font_color.r = font_white; } if (use_green_component) { font_color.g = 1.0f; } else { font_color.g = font_white; } if (use_blue_component) { font_color.b = 1.0f; } else { font_color.b = font_white; } font_color.a = font_alpha; // Draw the text gc.push_translate(current_position.x, current_position.y, current_position.z); gc.push_scale( 2.0f / gc.get_width(), -2.0f / gc.get_height()); CL_Size text_size = vector_font.get_text_size(gc, text); vector_font.draw_text(gc, -text_size.width/2, text_size.height/4, text, font_color); gc.pop_modelview(); gc.pop_modelview(); return true; }
void start() { float x1 = -10; float y1 = -10; // glBindTexture(GL_TEXTURE_2D, texName); // float roty = -45; int light_x = 0; int light_xd = 1; vec3i light_pos( 0, 40, 0 ); //int steps = 1; //light_scene ls( planes_, solid_ ); auto t_old = CL_System::get_microseconds(); bool light_on = true; bool light_button_down = false; double delta_t = 0.01; player p1; //rad_core_ = make_unique<rad_core_threaded>( scene_static_, light_static_ ); // rad_core_ = make_rad_core_threaded(scene_static_, light_static_); // auto rad_core2 = make_unique<rad_core_opencl>( cl_context_, cl_cqueue_, scene_static_, light_static_ ); // rad_core2->load_kernel( cl_context_, cl_used_devices_ ); // float min_ff = 5e-5; // vab.render(planes_.begin(), planes_.end() ); // vbo_builder vbob(scene_static_.planes().size()); // vbob.update_index_buffer(scene_static_.planes().size()); // vbob.update_vertices( scene_static_.planes().begin(), scene_static_.planes().end()); // std::ifstream is( "cryistal-castle-hidden-ramp.txt" ); std::ifstream is( "house1.txt" ); render_unit runit(is, vec3f( -40.0, -20.0, -40.0 )); // std::ifstream is2( "cryistal-castle-tree-wave.txt" ); // std::ifstream is2( "cryistal-castle-hidden-ramp.txt" ); // std::ifstream is2( "house1.txt" ); // render_unit runit2(is2, vec3f( 60.0, -20.0, 0.0 )); #if 0 cl::BufferGL buf; //cl::Buffer buf; try { //cl_int cl_err; //buf = cl::Buffer( clCreateFromGLBuffer( cl_context_(), CL_MEM_WRITE_ONLY, vbob.buffers_[1], &cl_err )); //assert( cl_err == CL_SUCCESS ); buf = cl::BufferGL( cl_context_, CL_MEM_WRITE_ONLY, vbob.buffers_[1] ); cl_fcolor_ = cl::Buffer( cl_context_, CL_MEM_READ_ONLY, scene_static_.planes().size() * 3 * sizeof(float) ); cl_kernel_.setArg(0, buf() ); //cl_kernel_.setArg(1, cl_fcolor_ ); cl_kernel_.setArg(1, rad_core2->f_rad() ); cl_uint cl_color_size = scene_static_.planes().size(); cl_kernel_.setArg(2, cl_color_size ); } catch( cl::Error x ) { std::cerr << "cl error during gl buffer setup\ncall: " << x.what() << "\nerror code: " << cl_str_error( x.err() ) << "\n"; throw; } #endif bool light_changed = true; //rad_core_ = make_unique<rad_core_lockfree>( scene_static_, light_static_ ); bool do_quit = false; while ( !do_quit ) { //cube c(x1, 0, y1); CL_GraphicContext gc = wnd_.get_gc(); CL_InputDevice &keyboard = wnd_.get_ic().get_keyboard(); CL_InputDevice &mouse = wnd_.get_ic().get_mouse(); // if( keyboard.get_keycode(CL_KEY_I) ) { // // roty += 1; // // min_ff += 5e-6; // } // if( keyboard.get_keycode(CL_KEY_K) ) { // //roty -= 1; // min_ff -= 5e-6; // } // // std::cout << "min ff: " << min_ff << "\n"; p1.input(keyboard, mouse); p1.frame(t_old * 1.0e-3, delta_t); int light_speed = 1; do_quit = keyboard.get_keycode(CL_KEY_Q); if ( keyboard.get_keycode(CL_KEY_LEFT) ) { light_pos.x += light_speed; light_changed = true; } if ( keyboard.get_keycode(CL_KEY_RIGHT) ) { light_pos.x -= light_speed; light_changed = true; } if ( keyboard.get_keycode(CL_KEY_UP) ) { light_pos.z += light_speed; light_changed = true; } if ( keyboard.get_keycode(CL_KEY_DOWN) ) { light_pos.z -= light_speed; light_changed = true; } if ( keyboard.get_keycode(CL_KEY_L )) { if ( !light_button_down ) { light_on = !light_on; light_changed = true; } light_button_down = true; } else { light_button_down = false; } glEnable(GL_CULL_FACE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // glTranslatef(0,0,-100); // glRotatef(45,1,0,0); // glRotatef(roty,0,1,0); if ( light_x > 60 || light_x < -60 ) { light_xd = -light_xd; // light_x = -20; } // light_planes(vec3i(light_x, 10, 10 )); if( light_changed ) { //ls.reset_emit(); // light_dynamic_.clear_emit(); runit.clear_emit(); // runit2.clear_emit(); //ls.render_light(vec3f( 40, 50.0, light_x ), vec3f(1.0, 1.0, 1.0 )); if ( light_on ) { //ls.render_light(light_pos, vec3f(1.0, 0.8, 0.6 )); //vec3f light_pos( p1.pos) // vec3f light_pos = (p1.pos()* pump_factor_) - base_pos_; vec3f light_weird = (light_pos * pump_factor_) - base_pos_; // light_utils::render_light( light_dynamic_.emit(), scene_static_, light_weird, vec3f(1.0, 0.8, 0.6 )); runit.render_light(light_weird, vec3f(1.0, 0.8, 0.6 )); // runit2.render_light(light_weird, vec3f(1.0, 0.8, 0.6 )); } //ls.post(); light_changed = false; } runit.update(); // runit2.update(); // rad_core2->set_emit( *light_dynamic_.emit() ); // rad_core_->set_emit( *light_dynamic_.emit() ); //ls.render_emit_patches(); //steps = 1; //ls.do_radiosity( steps ); //rad_core2->run(); // rad_core_->copy( light_dynamic_.rad() ); // stupid: transfer rgb energy fomr light scene to planes // for ( size_t i = 0; i < scene_static_.planes().size(); ++i ) { // plane &p = const_cast<plane &>(scene_static_.planes()[i]); // FIXME: HACK!!! is the energy_rgp stored in the planes actually used? remove it! // p.energy_rgb((*light_dynamic_.rad())[i]); // } // light_x += light_xd; // glPushMatrix(); //CL_Mat4f proj = CL_Mat4f::look_at( 20, 20, 20, 0, 0, 0, 0.0, 1.0, 0.0 ); //vab.update_color( planes_.begin(), planes_.end() ); // vab.update_color( ls.rad_rgb().begin(), ls.rad_rgb().end() ); // vab.setup_gl_pointers(); // vbob.update_color(light_dynamic_.rad()->begin(), light_dynamic_.rad()->end()); #if 0 try { cl_int err; // glFinish(); const auto &rad_colors = *light_dynamic_.rad(); // cl_cqueue_.enqueueWriteBuffer( cl_fcolor_, false, 0, rad_colors.size() * sizeof(vec3f), rad_colors.data() ); // err = clEnqueueAcquireGLObjects( cl_cqueue_(), 1, &(buf()), 0, 0, 0 ); assert( err == CL_SUCCESS ); //cl_kernel_.setArg(1, rad_core2->f_rad() ); cl_kernel_.setArg(1, cl_fcolor_ ); cl_cqueue_.enqueueNDRangeKernel( cl_kernel_, 0, cl::NDRange(scene_static_.planes().size()) ); // unmap buffer object err = clEnqueueReleaseGLObjects( cl_cqueue_(), 1, &(buf()), 0,0,0); assert( err == CL_SUCCESS ); cl_cqueue_.finish(); } catch( cl::Error x ) { std::cerr << "cl error during kernel execution\ncall: " << x.what() << "\nerror code: " << cl_str_error( x.err() ) << "\n"; throw; } #endif //setup_perspective( p1 ); setup_ortho(); int ortho_width = 320; int ortho_heigth = 200; glViewport( gc.get_width() - ortho_width, gc.get_height() - ortho_heigth, ortho_width, ortho_heigth ); // vab.draw_arrays(); // render_quads(); // glPopMatrix(); setup_perspective(p1); glViewport( 0, 0, gc.get_width(), gc.get_height()); // render_quads(); // vab.setup_gl_pointers(); // vab.draw_arrays(); // vbob.draw_arrays(); runit.draw(); // runit2.draw(); wnd_.flip(1); auto t = CL_System::get_microseconds(); // std::cout << "fps: " << 1e6 / (t - t_old) << "\n"; delta_t = (t - t_old) * 1.0e-6; t_old = t; // std::cout << "delta: " << delta_t << "\n"; // CL_System::sleep( 1000 / 60. ); // getchar(); CL_KeepAlive::process(); x1 += 1; if ( x1 > 10 ) { x1 = -10; y1 += 1; } if ( y1 > 10 ) { y1 = -10; } } }
// 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; }
int App::start(const std::vector<CL_String> &args) { CL_OpenGLWindowDescription description; description.set_title("NightVision Shader"); 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_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); CL_Slot slot_window_close = window.sig_window_close().connect(this, &App::window_close); // Create offscreen texture CL_Texture texture_offscreen(gc, gc.get_width(), gc.get_height()); texture_offscreen.set_min_filter(cl_filter_nearest); texture_offscreen.set_mag_filter(cl_filter_nearest); CL_Texture texture_mask(gc, gc.get_width(), gc.get_height()); texture_mask.set_min_filter(cl_filter_nearest); texture_mask.set_mag_filter(cl_filter_nearest); // Create offscreen framebuffer CL_FrameBuffer framebuffer_offscreen(gc); framebuffer_offscreen.attach_color_buffer(0, texture_offscreen); CL_FrameBuffer framebuffer_mask(gc); framebuffer_mask.attach_color_buffer(0, texture_mask); CL_Image background(gc, "../PostProcessing/Resources/background.png"); CL_Image ball(gc, "../PostProcessing/Resources/ball.png"); ball.set_alignment(origin_center); CL_Texture noise_texture(gc, "Resources/noise_texture_0001.png"); noise_texture.set_wrap_mode(cl_wrap_repeat, cl_wrap_repeat); // 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"); shader.bind_attribute_location(2, "TexCoord0"); if (!shader.link()) throw CL_Exception("Unable to link shader program: Error:" + shader.get_info_log()); quit = false; float amount = 0.0f; float timer = 0.0f; float scale = 1.0f; CL_Font font(gc, "tahoma", 32); // Shader based on: http://www.geeks3d.com/20091009/shader-library-night-vision-post-processing-filter-glsl/ elapsedTime = 0.0f; // seconds luminanceThreshold = 0.2f; colorAmplification = 4.0f; effectCoverage = 0.65f; // Render the mask gc.set_frame_buffer(framebuffer_mask); gc.clear(); for (float offset=0.0f; offset<=1.0f; offset+=0.01f) { CL_Draw::circle(gc, gc.get_width() / 2.0f, gc.get_height() / 2.0f, 400.0f - offset * 64.0f, CL_Colorf(offset, offset, offset, 1.0f)); } gc.reset_frame_buffer(); unsigned int startTime = CL_System::get_time(); while (!quit) { timer = (CL_System::get_time() - startTime) / 1000.0f; elapsedTime = timer; // Render standard image to offscreen buffer gc.set_frame_buffer(framebuffer_offscreen); background.draw(gc, 0, 0); ball.draw(gc, gc.get_width() / 2 + 200 * sinf(timer / 2.0f), gc.get_height() / 2 + 200 * cosf(timer / 2.0f)); gc.reset_frame_buffer(); render_night_vision(gc, texture_offscreen, texture_mask, noise_texture, shader); const int gap = 32; font.draw_text(gc, 10, 64+gap*0, CL_String("luminanceThreshold : " + CL_StringHelp::float_to_text(luminanceThreshold) + " (Press Q,W)" )); font.draw_text(gc, 10, 64+gap*1, CL_String("colorAmplification : " + CL_StringHelp::float_to_text(colorAmplification) + " (Press A,S)" )); font.draw_text(gc, 10, 64+gap*2, CL_String("effectCoverage : " + CL_StringHelp::float_to_text(effectCoverage) + " (Press Z,X)" )); window.flip(); CL_System::sleep(10); CL_KeepAlive::process(); } return 0; }
void run() { quit = false; CL_DisplayWindowDescription window_desc; window_desc.set_size(CL_Size(1920, 1080), true); window_desc.set_title("Luna"); CL_DisplayWindow window(window_desc); CL_Slot slot_quit = window.sig_window_close().connect(this, &RootWindow::on_window_close); CL_GraphicContext gc = window.get_gc(); CL_InputDevice keyboard = window.get_ic().get_keyboard(); CL_FontDescription font_desc; font_desc.set_typeface_name("Monospace"); font_desc.set_height(16); CL_Font_System font(gc, font_desc); CL_FontMetrics fmetrics = font.get_font_metrics(); int fntWidth = fmetrics.get_max_character_width(); CL_SpriteDescription spr_desc; spr_desc.add_frame(CL_ImageProviderFactory::load("spaceship.png")); CL_Sprite node(gc, spr_desc); node.set_play_loop(true); node.set_play_pingpong(false); node.set_frame(0); CL_Image pict(gc,"stars.jpg"); x = gc.get_width() / 2 ; y = gc.get_height() / 2 ; U = 0; W = 0; V = 0; H = 0; step = 0.2; while (!quit) { if(keyboard.get_keycode(CL_KEY_ESCAPE) == true) quit = true; if(keyboard.get_keycode(CL_KEY_LEFT) == true) U -= step ; if(keyboard.get_keycode(CL_KEY_RIGHT) == true) U += step ; if(keyboard.get_keycode(CL_KEY_UP) == true) V -= step ; if(keyboard.get_keycode(CL_KEY_DOWN) == true) V += step ; if(x < -node.get_width()) { x = gc.get_width(); } else if(x > gc.get_width()) { x = -node.get_width(); } else x += U ; if(y < step) { y = step; V = 0; } else if(y > gc.get_height()-node.get_height()) { y = gc.get_height()-node.get_height(); V = 0; } else y += V; gc.clear(); pict.draw(gc,W,H); pict.draw(gc,W,H-gc.get_height() ); pict.draw(gc,W-gc.get_width(),H ); H++ ; if( H > gc.get_height() ) H = 0; node.draw(gc, x, y); CL_String velocity = cl_format("Velocity U[%1] V[%2] --- Coordinate X[%3] Y[%4] --- Origin W[%5] H[%6]",U,V,x,y,W,H); font.draw_text(gc, gc.get_width()/2 - (velocity.length() * fntWidth)/2, 16, velocity); window.flip(); CL_KeepAlive::process(); CL_System::sleep(10); } }
void FramerateCounter::show_fps(CL_GraphicContext &gc, CL_Font &fps_font) { CL_String fps = cl_format("%1 fps", get_framerate()); fps_font.draw_text(gc, gc.get_width() - 100, 30, fps); }
int App::start(const std::vector<CL_String> &args) { CL_OpenGLWindowDescription description; description.set_title("Guassian Blur Shader"); 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_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up); CL_Slot slot_window_close = window.sig_window_close().connect(this, &App::window_close); // Create offscreen texture CL_Texture texture_offscreen(gc, gc.get_width(), gc.get_height()); texture_offscreen.set_min_filter(cl_filter_nearest); texture_offscreen.set_mag_filter(cl_filter_nearest); CL_Texture texture_offscreen2(gc, gc.get_width(), gc.get_height()); texture_offscreen2.set_min_filter(cl_filter_nearest); texture_offscreen2.set_mag_filter(cl_filter_nearest); // Create offscreen framebuffer CL_FrameBuffer framebuffer_offscreen(gc); framebuffer_offscreen.attach_color_buffer(0, texture_offscreen); CL_FrameBuffer framebuffer_offscreen2(gc); framebuffer_offscreen2.attach_color_buffer(0, texture_offscreen2); CL_Image background(gc, "../PostProcessing/Resources/background.png"); CL_Image ball(gc, "../PostProcessing/Resources/ball.png"); ball.set_alignment(origin_center); // 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"); shader.bind_attribute_location(2, "TexCoord0"); if (!shader.link()) throw CL_Exception("Unable to link shader program: Error:" + shader.get_info_log()); quit = false; float amount = 0.0f; float timer = 0.0f; float scale = 1.0f; CL_Font font(gc, "tahoma", 32); blur = 1.0f; unsigned int startTime = CL_System::get_time(); while (!quit) { timer = (CL_System::get_time() - startTime) / 1000.0f; // Render standard image to offscreen buffer gc.set_frame_buffer(framebuffer_offscreen); background.draw(gc, 0, 0); ball.draw(gc, gc.get_width() / 2 + 200 * sinf(timer / 2.0f), gc.get_height() / 2 + 200 * cosf(timer / 2.0f)); gc.reset_frame_buffer(); gc.set_frame_buffer(framebuffer_offscreen2); render_gaussian_blur(gc, blur, texture_offscreen, shader, 1.0f / texture_offscreen2.get_width(), 0.0f); gc.reset_frame_buffer(); render_gaussian_blur(gc, blur, texture_offscreen2, shader, 0.0f, 1.0f / texture_offscreen2.get_height()); CL_String text( "Press 1 to 9 to control blur amount. Currently it is :" + CL_StringHelp::float_to_text(blur) ); font.draw_text(gc, 10, 64, text); window.flip(); CL_System::sleep(10); CL_KeepAlive::process(); } return 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; }
int Application::main(const std::vector<CL_String> &args) { try { #ifdef DEBUG // Crea una ventan en consola para la salida de texto si no está disponible CL_ConsoleWindow console("JATG", 80, 1000); // 1000 permite una barra vertical CL_ConsoleLogger logger; #endif // Crea la ventana CL_DisplayWindowDescription desc; desc.set_title("JATG"); desc.set_size(CL_Size(1024,768), true); // Usa esta resolución desc.set_fullscreen(true); desc.set_decorations(false); CL_DisplayWindow window(desc); CL_GraphicContext gc = window.get_gc(); CL_Mat4f matrix = CL_Mat4f::scale( (float) gc.get_width() / 1024.0f, (float) gc.get_height() / 768.0f, 1.0f); gc.set_modelview(matrix); CL_SoundOutput output(44100); //Inicializa Frecuencia // Crea el mundo Mundo mundo(window); // Corre el bucle del juego mundo.gameloop(); } catch (CL_Exception& exception) { CL_Console::write_line("Excepcion Tomada:"); CL_Console::write_line(exception.message); std::vector<CL_String> stacktrace = exception.get_stack_trace(); int tamaño = stacktrace.size(); if (tamaño > 0) { CL_Console::write_line("Stack Trace:"); for (int i=0; i < tamaño; i++) { CL_Console::write_line(stacktrace[i]); } } CL_Console::wait_for_key(); //Espera por una tecla } return 0; }
// The start of the Application int App::start(const std::vector<CL_String> &args) { // Setup the window CL_DisplayWindowDescription win_desc; win_desc.set_allow_resize(true); win_desc.set_title("3D GUI Example"); win_desc.set_size(CL_Size( 700, 700 ), false); window = CL_DisplayWindow(win_desc); // Connect the slots that we require CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close); CL_Slot slot_input_down = (window.get_ic().get_keyboard()).sig_key_down().connect(this, &App::on_input_down); CL_GraphicContext gc = window.get_gc(); CL_Font font = CL_Font(gc, "tahoma", 16); // Initialise the GUI system GUI gui(this); // NOTE: The GUI component positions are still in 2D world, therefore // be careful not to overlap windows, else unpredicted results may occur! window1 = new Window1(gui, CL_Rect(0,0, CL_Size(256, 256))); slider_1_xrotation = new Slider(gui, CL_Rect(0, 512, CL_Size(200, 17))); slider_1_xrotation->object_matrix.translate_self(0.0f, 0.8f, 3.0f); slider_1_xrotation->object_matrix.multiply(CL_Mat4f::rotate(CL_Angle(10, cl_degrees), 0.0f, 0.0f, 1.0f)); slider_1_yrotation = new Slider(gui, CL_Rect(256*1, 512, CL_Size(200, 17))); slider_1_yrotation->object_matrix.translate_self(0.0f, 0.7f, 3.0f); slider_1_yrotation->object_matrix.multiply(CL_Mat4f::rotate(CL_Angle(10, cl_degrees), 0.0f, 0.0f, 1.0f)); slider_1_zrotation = new Slider(gui, CL_Rect(256*2, 512, CL_Size(200, 17))); slider_1_zrotation->object_matrix.translate_self(0.0f, 0.6f, 3.0f); slider_1_zrotation->object_matrix.multiply(CL_Mat4f::rotate(CL_Angle(10, cl_degrees), 0.0f, 0.0f, 1.0f)); slider_1_xtranslation = new Slider(gui, CL_Rect(256*3, 512, CL_Size(200, 17))); slider_1_xtranslation->object_matrix.translate_self(0.0f, 0.5f, 3.0f); slider_1_xtranslation->object_matrix.multiply(CL_Mat4f::rotate(CL_Angle(10, cl_degrees), 0.0f, 0.0f, 1.0f)); slider_1_xtranslation->component->set_position(500); slider_1_ytranslation = new Slider(gui, CL_Rect(256*4, 512, CL_Size(200, 17))); slider_1_ytranslation->object_matrix.translate_self(0.0f, 0.4f, 3.0f); slider_1_ytranslation->object_matrix.multiply(CL_Mat4f::rotate(CL_Angle(10, cl_degrees), 0.0f, 0.0f, 1.0f)); slider_1_ytranslation->component->set_position(500); slider_1_ztranslation = new Slider(gui, CL_Rect(256*5, 512, CL_Size(200, 17))); slider_1_ztranslation->object_matrix.translate_self(0.0f, 0.3f, 3.0f); slider_1_ztranslation->object_matrix.multiply(CL_Mat4f::rotate(CL_Angle(10, cl_degrees), 0.0f, 0.0f, 1.0f)); slider_1_ztranslation->component->set_position(500); while(!quit) { calculate_matrix(); gc.set_modelview(CL_Mat4f::identity()); gc.set_map_mode(CL_MapMode(cl_map_2d_upper_left)); // Draw the gradient CL_Draw::gradient_fill(gc, CL_Rect(0, 0, gc.get_width(), gc.get_height()/2), CL_Gradient(CL_Colorf(0.2f, 0.2f, 0.8f, 1.0f), CL_Colorf(0.0f, 0.0f, 0.2f, 1.0f))); CL_Draw::gradient_fill(gc, CL_Rect(0, gc.get_height()/2, gc.get_width(), gc.get_height()), CL_Gradient(CL_Colorf(0.0f, 0.0f, 0.2f, 1.0f), CL_Colorf(0.2f, 0.2f, 0.8f, 1.0f))); font.draw_text(gc, 8, 20, "GUI3D"); int xoffset = 160; int yoffset = 70; const int ygap = 35; font.draw_text(gc, xoffset, yoffset, "X Rotation"); yoffset += ygap; font.draw_text(gc, xoffset, yoffset, "Y Rotation"); yoffset += ygap; font.draw_text(gc, xoffset, yoffset, "Z Rotation"); yoffset += ygap; font.draw_text(gc, xoffset, yoffset, "X Translation"); yoffset += ygap; font.draw_text(gc, xoffset, yoffset, "Y Translation"); yoffset += ygap; font.draw_text(gc, xoffset, yoffset, "Z Translation"); yoffset += ygap; if (!gui.run()) break; gc.set_map_mode(cl_user_projection); gc.set_projection(projection_matrix); gc.set_modelview(modelview_matrix); control_window(); gui.draw(); window.flip(1); CL_KeepAlive::process(); } return 0; }