Fix ortho projection, sprite vertex to map from 0 to 1
This commit is contained in:
parent
68c53dad0a
commit
b75009d03e
32
src/Game.cpp
32
src/Game.cpp
@ -31,28 +31,21 @@ void Game::init()
|
|||||||
"awesomeface");
|
"awesomeface");
|
||||||
Dengine::AssetManager::loadTextureImage("data/textures/plain_terrain.jpg",
|
Dengine::AssetManager::loadTextureImage("data/textures/plain_terrain.jpg",
|
||||||
"plain_terrain");
|
"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 = Dengine::AssetManager::getShader("sprite");
|
||||||
this->shader->use();
|
this->shader->use();
|
||||||
glCheckError();
|
|
||||||
//shader->uniformSetMat4fv("projection", projection);
|
glm::mat4 projection =
|
||||||
glCheckError();
|
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");
|
GLuint projectionLoc = glGetUniformLocation(this->shader->id, "projection");
|
||||||
glCheckError();
|
|
||||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||||
glCheckError();
|
|
||||||
|
|
||||||
/* Init game state */
|
/* Init game state */
|
||||||
this->state = GAME_ACTIVE;
|
this->state = GAME_ACTIVE;
|
||||||
glCheckError();
|
|
||||||
this->renderer = new Dengine::Renderer(this->shader);
|
this->renderer = new Dengine::Renderer(this->shader);
|
||||||
glCheckError();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::processInput(const f32 dt) {}
|
void Game::processInput(const f32 dt) {}
|
||||||
@ -61,14 +54,13 @@ void Game::render()
|
|||||||
{
|
{
|
||||||
const Dengine::Texture *tex =
|
const Dengine::Texture *tex =
|
||||||
Dengine::AssetManager::getTexture("plain_terrain");
|
Dengine::AssetManager::getTexture("plain_terrain");
|
||||||
glm::vec2 pos = glm::vec2(200, 200);
|
glm::vec2 pos = glm::vec2(0, 0);
|
||||||
glm::vec2 size = glm::vec2(640, 360);
|
glm::vec2 size = glm::vec2(1280.0f, 720.0f);
|
||||||
GLfloat rotation = 0;
|
this->renderer->drawSprite(tex, pos, size);
|
||||||
glm::vec3 color = glm::vec3(1.0f);
|
|
||||||
this->renderer->drawSprite(tex, pos, size, rotation, color);
|
|
||||||
|
|
||||||
this->renderer->drawSprite(Dengine::AssetManager::getTexture("awesomeface"),
|
tex = Dengine::AssetManager::getTexture("container");
|
||||||
glm::vec2(200, 200), glm::vec2(300, 400), 45.0f,
|
pos = glm::vec2(200, 200);
|
||||||
glm::vec3(0.0f, 1.0f, 0.0f));
|
size = glm::vec2(250.0f, 250.0f);
|
||||||
|
this->renderer->drawSprite(tex, pos, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <Dengine/Renderer.h>
|
#include <Dengine/Renderer.h>
|
||||||
#include <Dengine/OpenGL.h>
|
#include <Dengine/OpenGL.h>
|
||||||
|
|
||||||
|
#include <glm/gtx/transform.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
namespace Dengine
|
namespace Dengine
|
||||||
@ -19,21 +20,14 @@ void Renderer::drawSprite(const Texture *texture, glm::vec2 position,
|
|||||||
glm::vec2 size, GLfloat rotate, glm::vec3 color)
|
glm::vec2 size, GLfloat rotate, glm::vec3 color)
|
||||||
{
|
{
|
||||||
this->shader->use();
|
this->shader->use();
|
||||||
glm::mat4 model;
|
glm::mat4 transMatrix = glm::translate(glm::vec3(position, 0.0f));
|
||||||
// First translate (transformations are: scale happens first, then rotation
|
glm::mat4 rotateMatrix = glm::rotate(rotate, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||||
// and then finall translation happens; reversed order)
|
|
||||||
model = glm::translate(model, glm::vec3(position, 0.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
|
// NOTE(doyle): We draw everything as a unit square in OGL. Scale it to size
|
||||||
model = glm::rotate(model, rotate, glm::vec3(0.0f, 0.0f, 1.0f));
|
glm::mat4 scaleMatrix = glm::scale(glm::vec3(size, 1.0f));
|
||||||
|
|
||||||
// Move origin back
|
glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix;
|
||||||
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
|
|
||||||
|
|
||||||
this->shader->uniformSetMat4fv("model", model);
|
this->shader->uniformSetMat4fv("model", model);
|
||||||
|
|
||||||
@ -51,12 +45,14 @@ void Renderer::drawSprite(const Texture *texture, glm::vec2 position,
|
|||||||
|
|
||||||
void Renderer::initRenderData()
|
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[] = {
|
glm::vec4 vertices[] = {
|
||||||
// x y s t
|
// x y s t
|
||||||
{+1.0f, +1.0f, 1.0f, 1.0f}, // Top right
|
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left
|
||||||
{+1.0f, -1.0f, 1.0f, 0.0f}, // Bottom right
|
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left
|
||||||
{-1.0f, +1.0f, 0.0f, 1.0f}, // Top left
|
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right
|
||||||
{-1.0f, -1.0f, 0.0f, 0.0f}, // Bottom left
|
{1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right
|
||||||
};
|
};
|
||||||
|
|
||||||
GLuint VBO;
|
GLuint VBO;
|
||||||
|
@ -79,8 +79,6 @@ int main()
|
|||||||
// regardless of success. Catch it once by calling glGetError
|
// regardless of success. Catch it once by calling glGetError
|
||||||
glGetError();
|
glGetError();
|
||||||
|
|
||||||
glCheckError();
|
|
||||||
|
|
||||||
glm::ivec2 frameBufferSize;
|
glm::ivec2 frameBufferSize;
|
||||||
glfwGetFramebufferSize(window, &frameBufferSize.x, &frameBufferSize.y);
|
glfwGetFramebufferSize(window, &frameBufferSize.x, &frameBufferSize.y);
|
||||||
glViewport(0, 0, frameBufferSize.x, frameBufferSize.y);
|
glViewport(0, 0, frameBufferSize.x, frameBufferSize.y);
|
||||||
@ -91,22 +89,15 @@ int main()
|
|||||||
|
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glCheckError();
|
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glCheckError();
|
|
||||||
|
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glCheckError();
|
|
||||||
glCullFace(GL_BACK);
|
glCullFace(GL_BACK);
|
||||||
glCheckError();
|
|
||||||
|
|
||||||
Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y);
|
Breakout::Game game = Breakout::Game(frameBufferSize.x, frameBufferSize.y);
|
||||||
glCheckError();
|
|
||||||
game.init();
|
game.init();
|
||||||
glCheckError();
|
|
||||||
|
|
||||||
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
|
glfwSetWindowUserPointer(window, static_cast<void *>(&game));
|
||||||
glCheckError();
|
|
||||||
|
|
||||||
f32 deltaTime = 0.0f; // Time between current frame and last frame
|
f32 deltaTime = 0.0f; // Time between current frame and last frame
|
||||||
f32 lastFrame = 0.0f; // Time of last frame
|
f32 lastFrame = 0.0f; // Time of last frame
|
||||||
|
Loading…
Reference in New Issue
Block a user