From b75009d03eeff53ff9c6a66b52e967f36f7ebfa4 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Tue, 14 Jun 2016 00:51:14 +1000 Subject: [PATCH] Fix ortho projection, sprite vertex to map from 0 to 1 --- src/Game.cpp | 32 ++++++++++++-------------------- src/Renderer.cpp | 30 +++++++++++++----------------- src/dengine.cpp | 9 --------- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 43000e9..709672a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -31,28 +31,21 @@ void Game::init() "awesomeface"); Dengine::AssetManager::loadTextureImage("data/textures/plain_terrain.jpg", "plain_terrain"); - glCheckError(); - - glm::mat4 projection= glm::ortho(0.0f, 1280.0f, - 720.0f, 0.0f, -1.0f, 1.0f); - glCheckError(); this->shader = Dengine::AssetManager::getShader("sprite"); this->shader->use(); - glCheckError(); - //shader->uniformSetMat4fv("projection", projection); - glCheckError(); + + glm::mat4 projection = + glm::ortho(0.0f, static_cast(this->width), 0.0f, + static_cast(this->height), 0.0f, 1.0f); + this->shader->uniformSetMat4fv("projection", projection); GLuint projectionLoc = glGetUniformLocation(this->shader->id, "projection"); - glCheckError(); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glCheckError(); /* Init game state */ this->state = GAME_ACTIVE; - glCheckError(); this->renderer = new Dengine::Renderer(this->shader); - glCheckError(); } void Game::processInput(const f32 dt) {} @@ -61,14 +54,13 @@ void Game::render() { const Dengine::Texture *tex = Dengine::AssetManager::getTexture("plain_terrain"); - glm::vec2 pos = glm::vec2(200, 200); - glm::vec2 size = glm::vec2(640, 360); - GLfloat rotation = 0; - glm::vec3 color = glm::vec3(1.0f); - this->renderer->drawSprite(tex, pos, size, rotation, color); + glm::vec2 pos = glm::vec2(0, 0); + glm::vec2 size = glm::vec2(1280.0f, 720.0f); + this->renderer->drawSprite(tex, pos, size); - this->renderer->drawSprite(Dengine::AssetManager::getTexture("awesomeface"), - glm::vec2(200, 200), glm::vec2(300, 400), 45.0f, - glm::vec3(0.0f, 1.0f, 0.0f)); + tex = Dengine::AssetManager::getTexture("container"); + pos = glm::vec2(200, 200); + size = glm::vec2(250.0f, 250.0f); + this->renderer->drawSprite(tex, pos, size); } } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 776ea1f..c52bda7 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,6 +1,7 @@ #include #include +#include #include namespace Dengine @@ -19,21 +20,14 @@ void Renderer::drawSprite(const Texture *texture, glm::vec2 position, glm::vec2 size, GLfloat rotate, glm::vec3 color) { this->shader->use(); - glm::mat4 model; - // First translate (transformations are: scale happens first, then rotation - // and then finall translation happens; reversed order) - model = glm::translate(model, glm::vec3(position, 0.0f)); + glm::mat4 transMatrix = glm::translate(glm::vec3(position, 0.0f)); + glm::mat4 rotateMatrix = glm::rotate(rotate, glm::vec3(0.0f, 0.0f, 1.0f)); - // Move origin of rotation to center of quad - model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f)); - // Then rotate - model = glm::rotate(model, rotate, glm::vec3(0.0f, 0.0f, 1.0f)); + // NOTE(doyle): We draw everything as a unit square in OGL. Scale it to size + glm::mat4 scaleMatrix = glm::scale(glm::vec3(size, 1.0f)); - // Move origin back - model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f)); - - model = glm::scale(model, glm::vec3(size, 1.0f)); // Last scale + glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix; this->shader->uniformSetMat4fv("model", model); @@ -51,12 +45,14 @@ void Renderer::drawSprite(const Texture *texture, glm::vec2 position, void Renderer::initRenderData() { + // NOTE(doyle): Draws a series of triangles (three-sided polygons) using + // vertices v0, v1, v2, then v2, v1, v3 (note the order) glm::vec4 vertices[] = { - // x y s t - {+1.0f, +1.0f, 1.0f, 1.0f}, // Top right - {+1.0f, -1.0f, 1.0f, 0.0f}, // Bottom right - {-1.0f, +1.0f, 0.0f, 1.0f}, // Top left - {-1.0f, -1.0f, 0.0f, 0.0f}, // Bottom left + // x y s t + {0.0f, 1.0f, 0.0f, 1.0f}, // Top left + {0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left + {1.0f, 1.0f, 1.0f, 1.0f}, // Top right + {1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right }; GLuint VBO; diff --git a/src/dengine.cpp b/src/dengine.cpp index 417ac74..3e2621a 100644 --- a/src/dengine.cpp +++ b/src/dengine.cpp @@ -79,8 +79,6 @@ int main() // regardless of success. Catch it once by calling glGetError glGetError(); - glCheckError(); - glm::ivec2 frameBufferSize; glfwGetFramebufferSize(window, &frameBufferSize.x, &frameBufferSize.y); glViewport(0, 0, frameBufferSize.x, frameBufferSize.y); @@ -91,22 +89,15 @@ int main() glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glEnable(GL_BLEND); - glCheckError(); glEnable(GL_CULL_FACE); - glCheckError(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glCheckError(); glCullFace(GL_BACK); - glCheckError(); Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y); - glCheckError(); game.init(); - glCheckError(); glfwSetWindowUserPointer(window, static_cast(&game)); - glCheckError(); f32 deltaTime = 0.0f; // Time between current frame and last frame f32 lastFrame = 0.0f; // Time of last frame