bool render() { glm::ivec2 WindowSize(this->getWindowSize()); glm::vec3 MinScissor( 10000.f); glm::vec3 MaxScissor(-10000.f); { glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); glm::mat4* Pointer = (glm::mat4*)glMapBufferRange( GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); glm::mat4 Projection = glm::perspective(glm::pi<float>() * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f); glm::mat4 View = this->view(); glm::mat4 Model = glm::mat4(1.0f); *Pointer = Projection * View * Model; // Make sure the uniform buffer is uploaded glUnmapBuffer(GL_UNIFORM_BUFFER); glm::mat4 Ortho = glm::ortho(0.0f, 0.0f, float(WindowSize.x), float(WindowSize.y)); for(GLsizei i = 0; i < VertexCount; ++i) { glm::vec3 Projected = glm::project( glm::vec3(VertexData[i].Position, 0.0f), View * Model, Projection, glm::ivec4(0, 0, WindowSize.x, WindowSize.y)); MinScissor = glm::min(MinScissor, glm::vec3(Projected)); MaxScissor = glm::max(MaxScissor, glm::vec3(Projected)); } } glViewport(0, 0, WindowSize.x, WindowSize.y); glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]); glScissor(GLint(MinScissor.x), GLint(MinScissor.y), GLsizei(MaxScissor.x - MinScissor.x), GLsizei(MaxScissor.y - MinScissor.y)); glEnable(GL_SCISSOR_TEST); glClearBufferfv(GL_COLOR, 0, &glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)[0]); // Bind the program for use glUseProgram(ProgramName); glUniform1i(UniformDiffuse, 0); glUniformBlockBinding(ProgramName, UniformTransform, semantic::uniform::TRANSFORM0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, TextureName); glBindBufferBase(GL_UNIFORM_BUFFER, semantic::uniform::TRANSFORM0, BufferName[buffer::TRANSFORM]); glBindVertexArray(VertexArrayName); glDrawArraysInstanced(GL_TRIANGLES, 0, VertexCount, 1); glDisable(GL_SCISSOR_TEST); return true; }
void display() { // Compute the MVP (Model View Projection matrix) glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y)); glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Window.RotationCurrent.y, glm::vec3(1.f, 0.f, 0.f)); glm::mat4 View = glm::rotate(ViewRotateX, Window.RotationCurrent.x, glm::vec3(0.f, 1.f, 0.f)); glm::mat4 Model = glm::mat4(1.0f); glm::mat4 MVP = Projection * View * Model; glViewport(0, 0, Window.Size.x, Window.Size.y); glClearColor(1.0f, 0.5f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glm::vec3 MinScissor( 10000.f); glm::vec3 MaxScissor(-10000.f); glm::mat4 Ortho = glm::ortho(0.0f, 0.0f, 640.f, 480.0f); for(GLsizei i = 0; i < VertexCount; ++i) { glm::vec3 Projected = glm::project( glm::vec3(VertexData[i].Position, 0.0f), View * Model, Projection, glm::ivec4(0, 0, Window.Size.x, Window.Size.y)); MinScissor = glm::min(MinScissor, glm::vec3(Projected)); MaxScissor = glm::max(MaxScissor, glm::vec3(Projected)); } glScissor(GLint(MinScissor.x), GLint(MinScissor.y), GLsizei(MaxScissor.x - MinScissor.x), GLsizei(MaxScissor.y - MinScissor.y)); glEnable(GL_SCISSOR_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Bind the program for use glUseProgram(ProgramName); glUniform1i(UniformDiffuse, 0); glUniformMatrix4fv(UniformMVP, 1, GL_FALSE, &MVP[0][0]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, Texture2DName); glBindVertexArray(VertexArrayName); glDrawArrays(GL_TRIANGLES, 0, VertexCount); glf::checkError("display"); glf::swapBuffers(); }
void display() { glm::vec3 MinScissor( 10000.f); glm::vec3 MaxScissor(-10000.f); // Update of the uniform buffer { glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); glm::mat4* Pointer = (glm::mat4*)glMapBufferRange( GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y)); glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Window.RotationCurrent.y, glm::vec3(1.f, 0.f, 0.f)); glm::mat4 View = glm::rotate(ViewRotateX, Window.RotationCurrent.x, glm::vec3(0.f, 1.f, 0.f)); glm::mat4 Model = glm::mat4(1.0f); *Pointer = Projection * View * Model; // Make sure the uniform buffer is uploaded glUnmapBuffer(GL_UNIFORM_BUFFER); glm::mat4 Ortho = glm::ortho(0.0f, 0.0f, float(Window.Size.x), float(Window.Size.y)); for(GLsizei i = 0; i < VertexCount; ++i) { glm::vec3 Projected = glm::project( glm::vec3(VertexData[i].Position, 0.0f), View * Model, Projection, glm::ivec4(0, 0, Window.Size.x, Window.Size.y)); MinScissor = glm::min(MinScissor, glm::vec3(Projected)); MaxScissor = glm::max(MaxScissor, glm::vec3(Projected)); } } glViewport(0, 0, Window.Size.x, Window.Size.y); glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]); glScissor(GLint(MinScissor.x), GLint(MinScissor.y), GLsizei(MaxScissor.x - MinScissor.x), GLsizei(MaxScissor.y - MinScissor.y)); glEnable(GL_SCISSOR_TEST); glClearBufferfv(GL_COLOR, 0, &glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)[0]); // Bind the program for use glUseProgram(ProgramName); glUniform1i(UniformDiffuse, 0); glUniformBlockBinding(ProgramName, UniformTransform, glf::semantic::uniform::TRANSFORM0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, TextureName); glBindBufferBase(GL_UNIFORM_BUFFER, glf::semantic::uniform::TRANSFORM0, BufferName[buffer::TRANSFORM]); glBindVertexArray(VertexArrayName); glDrawArraysInstanced(GL_TRIANGLES, 0, VertexCount, 1); glDisable(GL_SCISSOR_TEST); glf::checkError("display"); glf::swapBuffers(); }