Fix ortho projection, sprite vertex to map from 0 to 1

This commit is contained in:
Doyle Thai 2016-06-14 00:51:14 +10:00
parent 68c53dad0a
commit b75009d03e
3 changed files with 25 additions and 46 deletions

View File

@ -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<GLfloat>(this->width), 0.0f,
static_cast<GLfloat>(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);
}
}

View File

@ -1,6 +1,7 @@
#include <Dengine/Renderer.h>
#include <Dengine/OpenGL.h>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
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
{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;

View File

@ -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<void *>(&game));
glCheckError();
f32 deltaTime = 0.0f; // Time between current frame and last frame
f32 lastFrame = 0.0f; // Time of last frame