Add sprite functionality, doesn't render yet

This commit is contained in:
Doyle Thai 2016-06-07 23:54:14 +10:00
parent 864dca6f3d
commit 761609e1f9
7 changed files with 155 additions and 4 deletions

View File

@ -124,12 +124,15 @@
<ClCompile Include="src\dengine.cpp" /> <ClCompile Include="src\dengine.cpp" />
<ClCompile Include="src\Game.cpp" /> <ClCompile Include="src\Game.cpp" />
<ClCompile Include="src\Shader.cpp" /> <ClCompile Include="src\Shader.cpp" />
<ClCompile Include="src\Sprite.cpp" />
<ClCompile Include="src\Texture.cpp" /> <ClCompile Include="src\Texture.cpp" />
<ClCompile Include="src\Tutorial.cpp" /> <ClCompile Include="src\Tutorial.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="data\shaders\default.frag.glsl" /> <None Include="data\shaders\default.frag.glsl" />
<None Include="data\shaders\default.vert.glsl" /> <None Include="data\shaders\default.vert.glsl" />
<None Include="data\shaders\sprite.frag.glsl" />
<None Include="data\shaders\sprite.vert.glsl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Dengine\AssetManager.h" /> <ClInclude Include="src\include\Dengine\AssetManager.h" />
@ -137,6 +140,7 @@
<ClInclude Include="src\include\Breakout\Game.h" /> <ClInclude Include="src\include\Breakout\Game.h" />
<ClInclude Include="src\include\Dengine\OpenGL.h" /> <ClInclude Include="src\include\Dengine\OpenGL.h" />
<ClInclude Include="src\include\Dengine\Shader.h" /> <ClInclude Include="src\include\Dengine\Shader.h" />
<ClInclude Include="src\include\Dengine\Sprite.h" />
<ClInclude Include="src\include\Dengine\Texture.h" /> <ClInclude Include="src\include\Dengine\Texture.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -33,10 +33,15 @@
<ClCompile Include="src\Tutorial.cpp"> <ClCompile Include="src\Tutorial.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Sprite.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="data\shaders\default.vert.glsl" /> <None Include="data\shaders\default.vert.glsl" />
<None Include="data\shaders\default.frag.glsl" /> <None Include="data\shaders\default.frag.glsl" />
<None Include="data\shaders\sprite.vert.glsl" />
<None Include="data\shaders\sprite.frag.glsl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Dengine\Common.h"> <ClInclude Include="src\include\Dengine\Common.h">
@ -57,5 +62,8 @@
<ClInclude Include="src\include\Breakout\Game.h"> <ClInclude Include="src\include\Breakout\Game.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\Dengine\Sprite.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,11 @@
#version 330 core
in vec2 texCoord;
out vec4 color;
uniform sampler2D texture;
void main()
{
color = texture(texture, texCoord);
}

View File

@ -0,0 +1,10 @@
#version 330 core
layout (location = 0) in vec4 data; // (vec2)pos, (vec2)texCoord
out vec2 texCoord;
void main()
{
gl_Position = vec4(data.xy, 0.0f, 1.0f);
texCoord = data.zw;
}

76
src/Sprite.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <Dengine\Sprite.h>
#include <Dengine\OpenGL.h>
namespace Dengine
{
Sprite::Sprite()
: mPos(0, 0),
mTex(nullptr)
{
}
Sprite::~Sprite() {}
b32 Sprite::loadSprite(Texture *tex, glm::vec2 pos)
{
if (!tex) return -1;
mTex = tex;
mPos = pos;
// NOTE(doyle): We encode in a vec4 (vec2)pos, (vec2)texCoords
glm::vec4 spriteVertex[] = {
// x y s t
{+0.5f, +0.5f, 1.0f, 1.0f}, // Top right
{+0.5f, -0.5f, 1.0f, 0.0f}, // Bottom right
{-0.5f, +0.5f, 0.0f, 1.0f}, // Top left
{-0.5f, -0.5f, 0.0f, 0.0f}, // Bottom left
};
glGenBuffers(1, &mVbo);
glBindBuffer(GL_ARRAY_BUFFER, mVbo);
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);
i32 numElementsInVertex = 4;
i32 vertexSize = sizeof(glm::vec4);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, numElementsInVertex, GL_FLOAT, GL_FALSE,
vertexSize, (GLvoid *)(0));
/* Unbind */
glBindVertexArray(0);
}
void Sprite::render(Shader *shader, GLuint spriteVAO)
{
// NOTE(doyle): Associate the VAO with this sprites VBO
glBindVertexArray(spriteVAO);
glBindBuffer(GL_ARRAY_BUFFER, mVbo);
/* Set texture */
glActiveTexture(GL_TEXTURE0);
mTex->bind();
glUniform1i(glGetUniformLocation(shader->mProgram, "texture"), 0);
/* Render */
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
}

View File

@ -3,6 +3,7 @@
#include <Dengine/Common.h> #include <Dengine/Common.h>
#include <Dengine/Shader.h> #include <Dengine/Shader.h>
#include <Dengine/AssetManager.h> #include <Dengine/AssetManager.h>
#include <Dengine/Sprite.h>
#include <Breakout/Game.h> #include <Breakout/Game.h>
@ -124,6 +125,7 @@ int main()
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glEnable(GL_CULL_FACE | GL_BLEND); glEnable(GL_CULL_FACE | GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK); glCullFace(GL_BACK);
@ -135,9 +137,9 @@ int main()
Dengine::AssetManager assetManager; Dengine::AssetManager assetManager;
i32 result = 0; i32 result = 0;
result = assetManager.loadShaderFiles("data/shaders/default.vert.glsl", result = assetManager.loadShaderFiles("data/shaders/sprite.vert.glsl",
"data/shaders/default.frag.glsl", "data/shaders/sprite.frag.glsl",
"default"); "sprite");
if (result) return result; if (result) return result;
/* Load a texture */ /* Load a texture */
@ -149,13 +151,24 @@ int main()
"wall"); "wall");
if (result) return result; if (result) return result;
result = assetManager.loadTextureImage("data/textures/awesomeface.png"
, "awesomeface");
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::Shader *shader = assetManager.getShader("default"); 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();
sprite.loadSprite(awesomeTex, glm::vec2(0, 0));
sprite.initVertexArrayObject(spriteVao);
/* Main game loop */ /* Main game loop */
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
@ -176,6 +189,8 @@ int main()
shader->use(); shader->use();
game.render(); game.render();
sprite.render(shader, spriteVao);
/* Swap the buffers */ /* Swap the buffers */
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }

View File

@ -0,0 +1,27 @@
#ifndef DENGINE_SPRITE_H
#define DENGINE_SPRITE_H
#include <Dengine\Common.h>
#include <Dengine\Texture.h>
#include <Dengine\Shader.h>
#include <GLM\glm.hpp>
namespace Dengine {
class Sprite
{
public:
Sprite();
~Sprite();
b32 loadSprite(Texture *tex, glm::vec2 pos);
void initVertexArrayObject(GLuint vao);
void render(Shader *shader, GLuint shaderVao);
private:
glm::vec2 mPos;
Texture *mTex;
GLuint mVbo;
};
}
#endif