Sprite uses unique vao for rendering

Remove notation of "m" in member variables.
This commit is contained in:
Doyle Thai 2016-06-08 01:35:00 +10:00
parent 761609e1f9
commit 6f7f9fa50a
6 changed files with 41 additions and 47 deletions

View File

@ -3,9 +3,9 @@ in vec2 texCoord;
out vec4 color; out vec4 color;
uniform sampler2D texture; uniform sampler2D tex;
void main() void main()
{ {
color = texture(texture, texCoord); color = texture(tex, texCoord);
} }

View File

@ -6,7 +6,7 @@
namespace Dengine namespace Dengine
{ {
Shader::Shader() Shader::Shader()
: mProgram(0) : program(0)
{ {
} }
@ -14,20 +14,20 @@ Shader::~Shader() {}
i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader) i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader)
{ {
mProgram = glCreateProgram(); this->program = glCreateProgram();
glAttachShader(mProgram, vertexShader); glAttachShader(this->program, vertexShader);
glAttachShader(mProgram, fragmentShader); glAttachShader(this->program, fragmentShader);
glLinkProgram(mProgram); glLinkProgram(this->program);
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
glDeleteShader(vertexShader); glDeleteShader(vertexShader);
GLint success; GLint success;
GLchar infoLog[512]; GLchar infoLog[512];
glGetProgramiv(mProgram, GL_LINK_STATUS, &success); glGetProgramiv(this->program, GL_LINK_STATUS, &success);
if (!success) if (!success)
{ {
glGetProgramInfoLog(mProgram, 512, NULL, infoLog); glGetProgramInfoLog(this->program, 512, NULL, infoLog);
std::cout << "glLinkProgram failed: " << infoLog << std::endl; std::cout << "glLinkProgram failed: " << infoLog << std::endl;
return -1; return -1;
} }
@ -36,5 +36,5 @@ i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader)
} }
void Shader::use() { glUseProgram(mProgram); } void Shader::use() { glUseProgram(this->program); }
} }

View File

@ -5,8 +5,8 @@ namespace Dengine
{ {
Sprite::Sprite() Sprite::Sprite()
: mPos(0, 0), : pos(0, 0),
mTex(nullptr) tex(nullptr)
{ {
} }
@ -16,8 +16,8 @@ b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos)
{ {
if (!tex) return -1; if (!tex) return -1;
mTex = tex; this->tex = tex;
mPos = pos; this->pos = pos;
// NOTE(doyle): We encode in a vec4 (vec2)pos, (vec2)texCoords // NOTE(doyle): We encode in a vec4 (vec2)pos, (vec2)texCoords
glm::vec4 spriteVertex[] = { glm::vec4 spriteVertex[] = {
@ -28,21 +28,17 @@ b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos)
{-0.5f, -0.5f, 0.0f, 0.0f}, // Bottom left {-0.5f, -0.5f, 0.0f, 0.0f}, // Bottom left
}; };
glGenBuffers(1, &mVbo); /* Create and bind buffers */
glBindBuffer(GL_ARRAY_BUFFER, mVbo); glGenBuffers(1, &this->vbo);
glGenVertexArrays(1, &this->vao);
glBindVertexArray(this->vao);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
/* Configure VBO */
glBufferData(GL_ARRAY_BUFFER, sizeof(spriteVertex), spriteVertex, glBufferData(GL_ARRAY_BUFFER, sizeof(spriteVertex), spriteVertex,
GL_DYNAMIC_DRAW); GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return 0;
}
void Sprite::initVertexArrayObject(GLuint vao)
{
// NOTE(doyle): Set the VAO object attributes to match a sprite's
// vertex composition
glBindVertexArray(vao);
/* Configure VAO */
i32 numElementsInVertex = 4; i32 numElementsInVertex = 4;
i32 vertexSize = sizeof(glm::vec4); i32 vertexSize = sizeof(glm::vec4);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
@ -50,26 +46,26 @@ void Sprite::initVertexArrayObject(GLuint vao)
vertexSize, (GLvoid *)(0)); vertexSize, (GLvoid *)(0));
/* Unbind */ /* Unbind */
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
return 0;
} }
void Sprite::render(Shader *shader, GLuint spriteVAO) void Sprite::render(Shader *shader)
{ {
// NOTE(doyle): Associate the VAO with this sprites VBO // NOTE(doyle): Associate the VAO with this sprites VBO
glBindVertexArray(spriteVAO); glBindVertexArray(this->vao);
glBindBuffer(GL_ARRAY_BUFFER, mVbo);
/* Set texture */ /* Set texture */
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
mTex->bind(); this->tex->bind();
glUniform1i(glGetUniformLocation(shader->mProgram, "texture"), 0); glUniform1i(glGetUniformLocation(shader->program, "tex"), 0);
/* Render */ /* Render */
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Unbind // Unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
} }

View File

@ -151,23 +151,21 @@ int main()
"wall"); "wall");
if (result) return result; if (result) return result;
result = assetManager.loadTextureImage("data/textures/awesomeface.png" result = assetManager.loadTextureImage("data/textures/awesomeface.png",
, "awesomeface"); "awesomeface");
if (result) return result; if (result) return result;
Dengine::Texture *containerTex = assetManager.getTexture("container"); Dengine::Texture *containerTex = assetManager.getTexture("container");
Dengine::Texture *wallTex = assetManager.getTexture("wall"); Dengine::Texture *wallTex = assetManager.getTexture("wall");
Dengine::Texture *awesomeTex = assetManager.getTexture("awesomeface");
Dengine::Shader *shader = assetManager.getShader("sprite"); Dengine::Shader *shader = assetManager.getShader("sprite");
Dengine::Texture *awesomeTex = assetManager.getTexture("awesomeface");
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
GLuint spriteVao;
glGenVertexArrays(1, &spriteVao);
Dengine::Sprite sprite = Dengine::Sprite(); Dengine::Sprite sprite = Dengine::Sprite();
sprite.loadSprite(awesomeTex, glm::vec2(0, 0)); sprite.loadSprite(wallTex, glm::vec2(0, 0));
sprite.initVertexArrayObject(spriteVao);
/* Main game loop */ /* Main game loop */
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
@ -189,7 +187,7 @@ int main()
shader->use(); shader->use();
game.render(); game.render();
sprite.render(shader, spriteVao); sprite.render(shader);
/* Swap the buffers */ /* Swap the buffers */
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@ -10,7 +10,7 @@ namespace Dengine
class Shader class Shader
{ {
public: public:
GLuint mProgram; GLuint program;
Shader(); Shader();
~Shader(); ~Shader();

View File

@ -15,12 +15,12 @@ public:
b32 loadSprite(Texture *tex, glm::vec2 pos); b32 loadSprite(Texture *tex, glm::vec2 pos);
void initVertexArrayObject(GLuint vao); void render(Shader *shader);
void render(Shader *shader, GLuint shaderVao);
private: private:
glm::vec2 mPos; glm::vec2 pos;
Texture *mTex; Texture *tex;
GLuint mVbo; GLuint vbo;
GLuint vao;
}; };
} }