-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
265 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "x64-Debug", | ||
"generator": "Ninja", | ||
"configurationType": "Debug", | ||
"inheritEnvironments": [ "msvc_x64_x64" ], | ||
"buildRoot": "${projectDir}\\out\\build\\${name}", | ||
"installRoot": "${projectDir}\\out\\install\\${name}", | ||
"cmakeCommandArgs": "", | ||
"buildCommandArgs": "", | ||
"ctestCommandArgs": "" | ||
}, | ||
{ | ||
"name": "x64-Release", | ||
"generator": "Ninja", | ||
"configurationType": "RelWithDebInfo", | ||
"buildRoot": "${projectDir}\\out\\build\\${name}", | ||
"installRoot": "${projectDir}\\out\\install\\${name}", | ||
"cmakeCommandArgs": "", | ||
"buildCommandArgs": "", | ||
"ctestCommandArgs": "", | ||
"inheritEnvironments": [ "msvc_x64_x64" ], | ||
"variables": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#version 330 core | ||
|
||
in vec2 TexCoords; | ||
out vec4 color; | ||
|
||
uniform sampler2D spriteTexture; | ||
|
||
void main() | ||
{ | ||
color = texture(spriteTexture, TexCoords); | ||
// You might want to handle alpha values (like alpha blending, discarding fragments, etc.) | ||
// Example: | ||
if(color.a < 0.1) | ||
discard; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
#version 330 core | ||
out vec4 FragColor; | ||
|
||
in vec3 ourColor; | ||
in vec2 TexCoord; | ||
|
||
uniform sampler2D ourTexture; | ||
|
||
void main() | ||
{ | ||
FragColor = vec4(0.5,0.5,0.1,1); | ||
} | ||
FragColor = texture(ourTexture, TexCoord); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
#version 330 core | ||
layout (location = 0) in vec3 aPos; | ||
layout (location = 1) in vec3 aColor; | ||
layout (location = 2) in vec2 aTexCoord; | ||
|
||
out vec3 ourColor; | ||
out vec2 TexCoord; | ||
|
||
void main() | ||
{ | ||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); | ||
gl_Position = vec4(aPos, 1.0); | ||
ourColor = aColor; | ||
TexCoord = aTexCoord; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#include "Sprite.h" | ||
#include "pch.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#pragma once | ||
#include "BufferLayout.h" | ||
#include "Shader.h" | ||
#include "Texture.h" | ||
#include "VertexArray.h" | ||
#include "VertexBuffer.h" | ||
|
||
class Sprite | ||
{ | ||
public: | ||
Sprite(const std::string& texturePath) | ||
: position(0.0f) | ||
, scale(1.0f) | ||
, mTexture(texturePath) | ||
, mShader{{ShaderType::VertexShader, "resources/Shaders/basic.vs"}, | ||
{ShaderType::FragmentShader, "resources/Shaders/basic.fs"}} | ||
{ | ||
float vertices[] = { | ||
// Positions // Texture Coords | ||
0.0f, 1.0f, 0.0f, 1.0f,// | ||
1.0f, 0.0f, 1.0f, 0.0f,// | ||
0.0f, 0.0f, 0.0f, 0.0f,// | ||
|
||
0.0f, 1.0f, 0.0f, 1.0f,// | ||
1.0f, 1.0f, 1.0f, 1.0f,// | ||
1.0f, 0.0f, 1.0f, 0.0f // | ||
}; | ||
|
||
unsigned int indices[] = { | ||
// note that we start from 0! | ||
0, 1, 3,// first Triangle | ||
1, 2, 3 // second Triangle | ||
}; | ||
|
||
// Initialize VAO and VBO (as before) | ||
mVBO.setBuffer(vertices, sizeof(vertices)); | ||
|
||
mVAO.bind(); | ||
mVBO.bind(); | ||
mBufferLayout.push<float>(2); | ||
mBufferLayout.push<float>(2); | ||
mVAO.setBuffer(mVBO, mBufferLayout); | ||
mVAO.unbind(); | ||
} | ||
|
||
void Draw() | ||
{ | ||
// Prepare transformations | ||
mShader.bind(); | ||
glm::mat4 model = glm::mat4(1.0f); | ||
model = glm::translate(model, glm::vec3(position, 0.0f)); | ||
model = glm::scale(model, glm::vec3(scale, 1.0f)); | ||
mShader.set("model", model); | ||
|
||
mTexture.bind(0); | ||
|
||
mVAO.bind(); | ||
glDrawArrays(GL_TRIANGLES, 0, 6); | ||
mVAO.unbind(); | ||
} | ||
|
||
void setPosition(const glm::vec2& newPosition) | ||
{ | ||
position = newPosition; | ||
} | ||
|
||
void setScale(const glm::vec2& newScale) | ||
{ | ||
scale = newScale; | ||
} | ||
|
||
private: | ||
VertexArray mVAO; | ||
VertexBuffer mVBO; | ||
Texture mTexture; | ||
Shader mShader; | ||
BufferLayout mBufferLayout; | ||
glm::vec2 position; | ||
glm::vec2 scale; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "Texture.h" | ||
|
||
#include "Renderer3D/OpenglUtils.h" | ||
|
||
#define STB_IMAGE_IMPLEMENTATION | ||
#include "stb_image.h" | ||
|
||
Texture::Texture(const std::string& path) | ||
: mTextureId(0) | ||
, mFilePath(path) | ||
, mData(nullptr) | ||
, mWidth(0) | ||
, mHeight(0) | ||
, nrChannels(0) | ||
{ | ||
stbi_set_flip_vertically_on_load(true); | ||
mData = stbi_load(path.c_str(), &mWidth, &mHeight, &nrChannels, 0); | ||
GLCall(glGenTextures(1, &mTextureId)); | ||
GLCall(glBindTexture(GL_TEXTURE_2D, mTextureId)); | ||
|
||
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); | ||
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); | ||
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); | ||
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); | ||
|
||
if (mData) | ||
{ | ||
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mWidth, mHeight, 0, GL_RGBA, | ||
GL_UNSIGNED_BYTE, mData)); | ||
GLCall(glGenerateMipmap(GL_TEXTURE_2D)); | ||
mAspectRatio = static_cast<float>(mWidth) / static_cast<float>(mHeight); | ||
unbind(); | ||
} | ||
else | ||
{ | ||
spdlog::error("Failed to load a texture: {}", path); | ||
} | ||
stbi_image_free(mData); | ||
}; | ||
|
||
Texture::~Texture() | ||
{ | ||
GLCall(glDeleteTextures(1, &mTextureId)); | ||
} | ||
|
||
void Texture::bind(unsigned int slot) const | ||
{ | ||
GLCall(glActiveTexture(GL_TEXTURE0 + slot)); | ||
GLCall(glBindTexture(GL_TEXTURE_2D, mTextureId)); | ||
} | ||
|
||
void Texture::unbind() const | ||
{ | ||
GLCall(glBindTexture(GL_TEXTURE_2D, 0)); | ||
} | ||
|
||
int Texture::width() const | ||
{ | ||
return mWidth; | ||
} | ||
|
||
int Texture::height() const | ||
{ | ||
return mHeight; | ||
} | ||
|
||
float Texture::aspectRatio() const | ||
{ | ||
return mAspectRatio; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,22 @@ | ||
#pragma once | ||
|
||
|
||
class Texture | ||
{ | ||
public: | ||
Texture(const std::string& path); | ||
~Texture(); | ||
|
||
void bind(unsigned int slot = 0) const; | ||
void unbind() const; | ||
|
||
inline int width() const; | ||
inline int height() const; | ||
inline float aspectRatio() const; | ||
|
||
private: | ||
unsigned int mTextureId; | ||
std::string mFilePath; | ||
unsigned char* mData; | ||
float mAspectRatio; | ||
int mWidth, mHeight, nrChannels; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
message(STATUS "Fetching stb...") | ||
|
||
FetchContent_Declare( | ||
stb | ||
GIT_REPOSITORY https://github.com/nothings/stb | ||
GIT_TAG beebb24b945efdea3b9bba23affb8eb3ba8982e7 | ||
) | ||
FetchContent_MakeAvailable(stb) | ||
|
||
message(STATUS "stb Fetched!") |