Sprite uses unique vao for rendering
Remove notation of "m" in member variables.
This commit is contained in:
parent
761609e1f9
commit
6f7f9fa50a
@ -3,9 +3,9 @@ in vec2 texCoord;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
uniform sampler2D texture;
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = texture(texture, texCoord);
|
||||
color = texture(tex, texCoord);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
namespace Dengine
|
||||
{
|
||||
Shader::Shader()
|
||||
: mProgram(0)
|
||||
: program(0)
|
||||
{
|
||||
}
|
||||
|
||||
@ -14,20 +14,20 @@ Shader::~Shader() {}
|
||||
|
||||
i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader)
|
||||
{
|
||||
mProgram = glCreateProgram();
|
||||
glAttachShader(mProgram, vertexShader);
|
||||
glAttachShader(mProgram, fragmentShader);
|
||||
glLinkProgram(mProgram);
|
||||
this->program = glCreateProgram();
|
||||
glAttachShader(this->program, vertexShader);
|
||||
glAttachShader(this->program, fragmentShader);
|
||||
glLinkProgram(this->program);
|
||||
|
||||
glDeleteShader(fragmentShader);
|
||||
glDeleteShader(vertexShader);
|
||||
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
glGetProgramiv(mProgram, GL_LINK_STATUS, &success);
|
||||
glGetProgramiv(this->program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(mProgram, 512, NULL, infoLog);
|
||||
glGetProgramInfoLog(this->program, 512, NULL, infoLog);
|
||||
std::cout << "glLinkProgram failed: " << infoLog << std::endl;
|
||||
return -1;
|
||||
}
|
||||
@ -36,5 +36,5 @@ i32 Shader::loadProgram(GLuint vertexShader, GLuint fragmentShader)
|
||||
|
||||
}
|
||||
|
||||
void Shader::use() { glUseProgram(mProgram); }
|
||||
void Shader::use() { glUseProgram(this->program); }
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ namespace Dengine
|
||||
{
|
||||
|
||||
Sprite::Sprite()
|
||||
: mPos(0, 0),
|
||||
mTex(nullptr)
|
||||
: pos(0, 0),
|
||||
tex(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
@ -16,8 +16,8 @@ b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos)
|
||||
{
|
||||
if (!tex) return -1;
|
||||
|
||||
mTex = tex;
|
||||
mPos = pos;
|
||||
this->tex = tex;
|
||||
this->pos = pos;
|
||||
|
||||
// NOTE(doyle): We encode in a vec4 (vec2)pos, (vec2)texCoords
|
||||
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
|
||||
};
|
||||
|
||||
glGenBuffers(1, &mVbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mVbo);
|
||||
/* Create and bind buffers */
|
||||
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,
|
||||
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 vertexSize = sizeof(glm::vec4);
|
||||
glEnableVertexAttribArray(0);
|
||||
@ -50,26 +46,26 @@ void Sprite::initVertexArrayObject(GLuint vao)
|
||||
vertexSize, (GLvoid *)(0));
|
||||
|
||||
/* Unbind */
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 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
|
||||
glBindVertexArray(spriteVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mVbo);
|
||||
glBindVertexArray(this->vao);
|
||||
|
||||
/* Set texture */
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
mTex->bind();
|
||||
glUniform1i(glGetUniformLocation(shader->mProgram, "texture"), 0);
|
||||
this->tex->bind();
|
||||
glUniform1i(glGetUniformLocation(shader->program, "tex"), 0);
|
||||
|
||||
/* Render */
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
// Unbind
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
@ -151,23 +151,21 @@ int main()
|
||||
"wall");
|
||||
if (result) return result;
|
||||
|
||||
result = assetManager.loadTextureImage("data/textures/awesomeface.png"
|
||||
, "awesomeface");
|
||||
result = assetManager.loadTextureImage("data/textures/awesomeface.png",
|
||||
"awesomeface");
|
||||
if (result) return result;
|
||||
|
||||
Dengine::Texture *containerTex = assetManager.getTexture("container");
|
||||
Dengine::Texture *wallTex = assetManager.getTexture("wall");
|
||||
Dengine::Shader *shader = assetManager.getShader("sprite");
|
||||
Dengine::Texture *awesomeTex = assetManager.getTexture("awesomeface");
|
||||
|
||||
Dengine::Shader *shader = assetManager.getShader("sprite");
|
||||
|
||||
f32 deltaTime = 0.0f; // Time between current frame and last frame
|
||||
f32 lastFrame = 0.0f; // Time of last frame
|
||||
|
||||
GLuint spriteVao;
|
||||
glGenVertexArrays(1, &spriteVao);
|
||||
Dengine::Sprite sprite = Dengine::Sprite();
|
||||
sprite.loadSprite(awesomeTex, glm::vec2(0, 0));
|
||||
sprite.initVertexArrayObject(spriteVao);
|
||||
sprite.loadSprite(wallTex, glm::vec2(0, 0));
|
||||
|
||||
/* Main game loop */
|
||||
while (!glfwWindowShouldClose(window))
|
||||
@ -189,7 +187,7 @@ int main()
|
||||
shader->use();
|
||||
game.render();
|
||||
|
||||
sprite.render(shader, spriteVao);
|
||||
sprite.render(shader);
|
||||
|
||||
/* Swap the buffers */
|
||||
glfwSwapBuffers(window);
|
||||
|
@ -10,7 +10,7 @@ namespace Dengine
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
GLuint mProgram;
|
||||
GLuint program;
|
||||
|
||||
Shader();
|
||||
~Shader();
|
||||
|
@ -15,12 +15,12 @@ public:
|
||||
|
||||
b32 loadSprite(Texture *tex, glm::vec2 pos);
|
||||
|
||||
void initVertexArrayObject(GLuint vao);
|
||||
void render(Shader *shader, GLuint shaderVao);
|
||||
void render(Shader *shader);
|
||||
private:
|
||||
glm::vec2 mPos;
|
||||
Texture *mTex;
|
||||
GLuint mVbo;
|
||||
glm::vec2 pos;
|
||||
Texture *tex;
|
||||
GLuint vbo;
|
||||
GLuint vao;
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user