void Camera2::ensure_matrix_valid() const { if (m_matrix_valid) return; m_matrix = projection_matrix() * view_matrix(); m_matrix_valid = true; }
kmVec3 Camera::project_point(ViewportID vid, const kmVec3& point) { if(!scene_) { throw LogicError("Passes a nullptr as a camera's Scene"); } kglt::Viewport& viewport = scene_->window().viewport(vid); kmVec3 tmp; kmVec3Fill(&tmp, point.x, point.y, point.z); kmVec3 result; kmVec3MultiplyMat4(&tmp, &tmp, &view_matrix()); kmVec3MultiplyMat4(&tmp, &tmp, &projection_matrix()); tmp.x /= tmp.z; tmp.y /= tmp.z; float vp_width = viewport.width(); float vp_height = viewport.height(); result.x = (tmp.x + 1) * vp_width / 2.0; result.y = (tmp.y + 1) * vp_height / 2.0; return result; }
void MapViewer::render_map() { // Focus onto the player refocus_map(); // Calculate the projection and modelview matrix for the map std::pair<int, int> size(window->get_size()); glm::mat4 projection_matrix(glm::ortho(0.0f, float(size.first), 0.0f, float(size.second), 0.0f, 1.0f)); glm::mat4 model(glm::mat4(1.0f)); model = glm::scale (model, glm::vec3(Engine::get_actual_tile_size())); model = glm::translate(model, glm::vec3(-get_display_x(), -get_display_y(), 0.0f)); // Draw all the layers, from base to top to get the correct draw order int layer_num = 0; for (int layer_id: map->get_layers()) { auto layer(ObjectManager::get_instance().get_object<Layer>(layer_id)); if (!layer) { continue; } if (!layer->is_renderable()) { continue; } RenderableComponent *layer_render_component(layer->get_renderable_component()); Shader *layer_shader(layer_render_component->get_shader().get()); //Set the matrices layer_render_component->set_projection_matrix(projection_matrix); layer_render_component->set_modelview_matrix(model); layer_render_component->bind_shader(); //TODO: I don't want to actually expose the shader, put these into wrappers in the shader object glUniformMatrix4fv(glGetUniformLocation(layer_shader->get_program(), "mat_projection"), 1, GL_FALSE, glm::value_ptr(layer_render_component->get_projection_matrix())); glUniformMatrix4fv(glGetUniformLocation(layer_shader->get_program(), "mat_modelview"), 1, GL_FALSE, glm::value_ptr(layer_render_component->get_modelview_matrix())); layer_render_component->bind_vbos(); layer_render_component->bind_textures(); glDrawArrays(GL_TRIANGLES, 0, layer_render_component->get_num_vertices_render()); //Release the vertex buffers and texppptures layer_render_component->release_textures(); layer_render_component->release_vbos(); layer_render_component->release_shader(); //next layer layer_num ++; } }
DenseMatrix gaussian_projection_matrix(IndexType target_dimension, IndexType current_dimension) { DenseMatrix projection_matrix(target_dimension,current_dimension); for (IndexType i=0; i<target_dimension; ++i) { for (IndexType j=0; j<current_dimension; ++j) { ScalarType v1 = (ScalarType)(rand()+1.f)/((float)RAND_MAX+2.f); ScalarType v2 = (ScalarType)(rand()+1.f)/((float)RAND_MAX+2.f); ScalarType len = sqrt(-2.f*log(v1)); projection_matrix(i,j) = len*cos(2.f*M_PI*v2)/sqrt(target_dimension); } } return projection_matrix; }
void Camera::update_frustum() { //Recalculate the view matrix kmMat4Inverse(&view_matrix_, &transform_); kmMat4 mvp; kmMat4Multiply(&mvp, &projection_matrix(), &view_matrix()); frustum_.build(&mvp); //Update the frustum for this camera }
void kam_set_normal_screen(G_KONFIG * p_ber) { set_matrix_view(SCREEN_XSTART, SCREEN_YSTART, SCREEN_XRES, SCREEN_YRES); projection_matrix(&p_ber->kamera.project, p_ber->kam.fov, (float) SCREEN_XRES / (float) SCREEN_YRES, p_ber->kam.near_plane, p_ber->kam.far_plane); set_matrix_project(&p_ber->kamera.project); camera_screen_mode = CAMERA_NORMAL; }
void kam_set_kino_screen(G_KONFIG * p_ber) { int kxres = SCREEN_XRES; int kyres = ftoi(SCREEN_XRES * KINO_POMER); int kx = SCREEN_XSTART, ky = SCREEN_YSTART + (SCREEN_YRES - kyres) / 2; set_matrix_view(kx, ky, kxres, kyres); projection_matrix(&p_ber->kamera.project, p_ber->kam.fov, (float) kxres / (float) kyres, p_ber->kam.near_plane, p_ber->kam.far_plane); set_matrix_project(&p_ber->kamera.project); camera_screen_mode = CAMERA_KINO; }
/////////////////////////////////////////////////////////////////////////////// // Function Name: display // // Purpose: GLUT display callback. // // INPUTS: None. // // OUTPUTS: None. // /////////////////////////////////////////////////////////////////////////////// void display() { float t = float(GetTickCount() & 0x1FFF) / float(0x1FFF); const vmath::vec3 Y(0.0f, 1.0f, 0.0f); const vmath::vec3 Z(0.0f, 0.0f, 1.0f); // Setup glEnable(GL_CULL_FACE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate simple shading program glUseProgram(shader_prog); // Set up the model and projection matrix vmath::mat4 model_matrix(vmath::translate(0.0f, 0.0f, -5.0f) * vmath::rotate(t * 360.0f, Y) * vmath::rotate(t * 720.0f, Z)); vmath::mat4 projection_matrix(vmath::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 20.0f)); glUniformMatrix4fv(model_matrix_loc, 1, GL_FALSE, model_matrix); glUniformMatrix4fv(projection_matrix_loc, 1, GL_FALSE, projection_matrix); // Set up for a glDrawElements call glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); #ifdef USE_PRIMITIVE_RESTART // When primitive restart is on, we can call one draw command glEnable(GL_PRIMITIVE_RESTART); glPrimitiveRestartIndex(0xFFFF); glDrawElements(GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); #else // Without primitive restart, we need to call two draw commands glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, BUFFER_OFFSET(9 * sizeof(GLushort))); #endif glDisable(GL_CULL_FACE); glUseProgram(0); glutSwapBuffers(); }
void Instance::Render(GLfloat aspect) { glUseProgram(program); //We change view by click the mosue float t = float(GetTickCount() & 0x3FFFF) / float(0x3FFFF); static float q = 0.0f; static const glm::vec3 X(1.0f, 0.0f, 0.0f); static const glm::vec3 Y(0.0f, 1.0f, 0.0f); static const glm::vec3 Z(0.0f, 0.0f, 1.0f); // Set model matrices for each instance glm::mat4 matrices[INSTANCE_COUNT]; for (int n = 0; n < INSTANCE_COUNT; n++) { float a = 50.0f * float(n) / 4.0f; float b = 50.0f * float(n) / 5.0f; float c = 50.0f * float(n) / 6.0f; matrices[n] = glm::rotate(glm::mat4(1.0), a + t * 360.0f, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(1.0), b + t * 360.0f, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate(glm::mat4(1.0), c + t * 360.0f, glm::vec3(0.0f, 0.0f, 1.0f)) * glm::translate(glm::mat4(1.0), glm::vec3(10.0f + a, 40.0f + b, 50.0f + c)); } glBindBuffer(GL_TEXTURE_BUFFER, model_tbo); glBufferData(GL_TEXTURE_BUFFER, sizeof(matrices), matrices, GL_DYNAMIC_DRAW); glBindBuffer(GL_TEXTURE_BUFFER, 0); glm::mat4 projection_matrix(glm::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 5000.0f)); glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(projection_matrix)); glm::mat4 view_matrix(glm::translate(glm::mat4(1.0), glm::vec3(0.0f, 0.0f, -1500.0f)) * glm::rotate(glm::mat4(1.0), t * 360.0f * 2.0f, glm::vec3(0.0f, 1.0f, 0.0f))); glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm::value_ptr(view_matrix)); //Draw the instance m_Armadillo.Render(0, INSTANCE_COUNT); glUseProgram(0); }
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(program); GLint model_matrix_loc = glGetUniformLocation(program, "model_matrix"); GLint projection_matrix_loc = glGetUniformLocation(program, "projection_matrix"); float t = float(GetTickCount() & 0x1FFF) / float(0x1FFF); static float q = 0.0f; float aspect = 1.0f; static const vmath::vec3 X(1.0f, 0.0f, 0.0f); static const vmath::vec3 Y(0.0f, 1.0f, 0.0f); static const vmath::vec3 Z(0.0f, 0.0f, 1.0f); vmath::mat4 model_matrix(vmath::translate(0.0f, 0.0f, -5.0f) * vmath::rotate(t * 360.0f, Y) * vmath::rotate(t * 720.0f, Z)); vmath::mat4 projection_matrix(vmath::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 500.0f)); glUniformMatrix4fv(model_matrix_loc, 1, GL_FALSE, model_matrix); glUniformMatrix4fv(projection_matrix_loc, 1, GL_FALSE, projection_matrix); glBindVertexArray(VAOs[Triangles]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[ElementBuffer]); #if USE_PRIMITIVE_RESTART glEnable(GL_PRIMITIVE_RESTART); glPrimitiveRestartIndex(0xFFFF); glDrawElements(GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, NULL); #else glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, NULL); glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, BUFFER_OFFSET(9 * sizeof(GLushort))); #endif glFlush(); }
kmVec3 Camera::project_point(const RenderTarget &target, const Viewport &viewport, const kmVec3& point) { if(!window_) { throw std::logic_error("Passed a nullptr as a camera's window"); } kmVec3 tmp; kmVec3Fill(&tmp, point.x, point.y, point.z); kmVec3 result; kmVec3MultiplyMat4(&tmp, &tmp, &view_matrix()); kmVec3MultiplyMat4(&tmp, &tmp, &projection_matrix()); tmp.x /= tmp.z; tmp.y /= tmp.z; float vp_width = viewport.width_in_pixels(target); float vp_height = viewport.height_in_pixels(target); result.x = (tmp.x + 1) * vp_width / 2.0; result.y = (tmp.y + 1) * vp_height / 2.0; return result; }
glm::mat4 pv_matrix(){ return projection_matrix()*view_matrix(); }
ClipCoords TransformUnit::ViewToClip(const ViewCoords& coords) { Vec4<float> coords4(coords.x, coords.y, coords.z, 1.0f); Mat4x4<float> projection_matrix(gstate.projMatrix); return ClipCoords(projection_matrix * coords4); }