Added Textures

This commit is contained in:
Tom
2021-06-28 15:52:56 +02:00
parent 6a9ac4661c
commit f24aadb438
25 changed files with 7999 additions and 20 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,10 +1,2 @@
IndexBuffer.cpp Texture.cpp
Main.cpp
C:\dev\Glfw_Practice\c++\Window_practice\Window_practice\src\VertexBufferLayout.h(5,10): warning C4067: unexpected tokens following preprocessor directive - expected a newline
Renderer.cpp
Shader.cpp
VertexArray.cpp
C:\dev\Glfw_Practice\c++\Window_practice\Window_practice\src\VertexBufferLayout.h(5,10): warning C4067: unexpected tokens following preprocessor directive - expected a newline
VertexBuffer.cpp
Generating Code...
Window_practice.vcxproj -> C:\dev\Glfw_Practice\c++\Window_practice\Debug\Window_practice.exe Window_practice.vcxproj -> C:\dev\Glfw_Practice\c++\Window_practice\Debug\Window_practice.exe

View File

@@ -1,2 +1,2 @@
PlatformToolSet=v142:VCToolArchitecture=Native32Bit:VCToolsVersion=14.29.30037:TargetPlatformVersion=10.0.19041.0: PlatformToolSet=v142:VCToolArchitecture=Native32Bit:VCToolsVersion=14.29.30037:VCServicingVersionATL=14.29.30038:VCServicingVersionCrtHeaders=14.29.30038:VCServicingVersionCompilers=14.29.30038:TargetPlatformVersion=10.0.19041.0:
Debug|Win32|C:\dev\Glfw_Practice\c++\Window_practice\| Debug|Win32|C:\dev\Glfw_Practice\c++\Window_practice\|

Binary file not shown.

Binary file not shown.

View File

@@ -149,6 +149,8 @@
<ClCompile Include="src\Main.cpp" /> <ClCompile Include="src\Main.cpp" />
<ClCompile Include="src\Renderer.cpp" /> <ClCompile Include="src\Renderer.cpp" />
<ClCompile Include="src\Shader.cpp" /> <ClCompile Include="src\Shader.cpp" />
<ClCompile Include="src\Texture.cpp" />
<ClCompile Include="src\vendor\stb_image\stb_image.cpp" />
<ClCompile Include="src\VertexArray.cpp" /> <ClCompile Include="src\VertexArray.cpp" />
<ClCompile Include="src\VertexBuffer.cpp" /> <ClCompile Include="src\VertexBuffer.cpp" />
</ItemGroup> </ItemGroup>
@@ -157,6 +159,8 @@
<ClInclude Include="src\Main.h" /> <ClInclude Include="src\Main.h" />
<ClInclude Include="src\Renderer.h" /> <ClInclude Include="src\Renderer.h" />
<ClInclude Include="src\Shader.h" /> <ClInclude Include="src\Shader.h" />
<ClInclude Include="src\Texture.h" />
<ClInclude Include="src\vendor\stb_image\stb_image.h" />
<ClInclude Include="src\VertexArray.h" /> <ClInclude Include="src\VertexArray.h" />
<ClInclude Include="src\VertexBuffer.h" /> <ClInclude Include="src\VertexBuffer.h" />
<ClInclude Include="src\VertexBufferLayout.h" /> <ClInclude Include="src\VertexBufferLayout.h" />
@@ -164,6 +168,9 @@
<ItemGroup> <ItemGroup>
<None Include="res\shaders\Basic.shader" /> <None Include="res\shaders\Basic.shader" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Image Include="res\textures\ChernoLogo.png" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@@ -33,6 +33,12 @@
<ClCompile Include="src\Shader.cpp"> <ClCompile Include="src\Shader.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\vendor\stb_image\stb_image.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\Main.h"> <ClInclude Include="src\Main.h">
@@ -56,8 +62,19 @@
<ClInclude Include="src\Shader.h"> <ClInclude Include="src\Shader.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Texture.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\vendor\stb_image\stb_image.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="res\shaders\Basic.shader" /> <None Include="res\shaders\Basic.shader" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Image Include="res\textures\ChernoLogo.png">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project> </Project>

View File

@@ -2,9 +2,13 @@
#version 330 core #version 330 core
layout(location = 0) in vec4 position; layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoord;
out vec2 v_TexCoord;
void main() { void main() {
gl_Position = position; gl_Position = position;
v_TexCoord = texCoord;
}; };
@@ -13,8 +17,12 @@ void main() {
layout(location = 0) out vec4 color; layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
uniform vec4 u_Color; uniform vec4 u_Color;
uniform sampler2D u_Texture;
void main() { void main() {
color = u_Color; vec4 texColor = texture(u_Texture, v_TexCoord);
color = texColor;
}; };

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -9,6 +9,7 @@
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "VertexArray.h" #include "VertexArray.h"
#include "Shader.h" #include "Shader.h"
#include "Texture.h"
int main(void){ int main(void){
// GLFW init // GLFW init
@@ -43,10 +44,10 @@ int main(void){
{ {
//vertex positions //vertex positions
float positions[] = { float positions[] = {
-0.5f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f -0.5f, 0.5f, 0.0f, 1.0f
}; };
//index buffer indecies //index buffer indecies
@@ -55,22 +56,32 @@ int main(void){
2, 3, 0 2, 3, 0
}; };
GLCall(glEnable(GL_BLEND));
GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
//creating vertex array object and bindig it //creating vertex array object and bindig it
unsigned int vao; unsigned int vao;
GLCall(glGenVertexArrays(1, &vao)); GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao)); GLCall(glBindVertexArray(vao));
VertexArray va; VertexArray va;
VertexBuffer vb(positions, 4 * 2 * sizeof(float)); VertexBuffer vb(positions, 4 * 4 * sizeof(float));
VertexBufferLayout layout; VertexBufferLayout layout;
layout.Push<float>(2); layout.Push<float>(2);
layout.Push<float>(2);
va.AddBuffer(vb, layout); va.AddBuffer(vb, layout);
IndexBuffer ib(indecies, 6); IndexBuffer ib(indecies, 6);
Shader shader("res/shaders/Basic.shader"); Shader shader("res/shaders/Basic.shader");
shader.Bind(); shader.Bind();
shader.SetUniform4f("u_Color", 0.8f, 0.3f, 0.8f, 1.0f); shader.SetUniform4f("u_Color", 0.8f, 0.3f, 0.8f, 1.0f);
Texture texture("res/textures/ChernoLogo.png");
texture.Bind();
shader.SetUniform1i("u_Texture", 0);
Renderer renderer; Renderer renderer;
//unbinding the buffers //unbinding the buffers

View File

@@ -102,17 +102,27 @@ void Shader::Unbind() const
GLCall(glUseProgram(0)); GLCall(glUseProgram(0));
} }
void Shader::SetUniform1i(const std::string& name, int value)
{
GLCall(glUniform1i(GetUniformLocation(name), value));
}
void Shader::SetUniform1f(const std::string& name, float value)
{
GLCall(glUniform1f(GetUniformLocation(name), value));
}
void Shader::SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3) void Shader::SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3)
{ {
GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3)); GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3));
} }
unsigned int Shader::GetUniformLocation(const std::string& name) int Shader::GetUniformLocation(const std::string& name)
{ {
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end()) if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name]; return m_UniformLocationCache[name];
GLCall(unsigned int location = glGetUniformLocation(m_RendererID, name.c_str())); GLCall(int location = glGetUniformLocation(m_RendererID, name.c_str()));
if (location == -1) if (location == -1)
std::cout << "Warning: uniform '" << name << "' does not exist!" << std::endl; std::cout << "Warning: uniform '" << name << "' does not exist!" << std::endl;

View File

@@ -12,7 +12,7 @@ class Shader {
private: private:
std::string m_FilePath; std::string m_FilePath;
unsigned int m_RendererID; unsigned int m_RendererID;
std::unordered_map<std::string, unsigned int> m_UniformLocationCache; std::unordered_map<std::string, int> m_UniformLocationCache;
public: public:
Shader(const std::string& filepath); Shader(const std::string& filepath);
~Shader(); ~Shader();
@@ -21,10 +21,12 @@ public:
void Unbind() const; void Unbind() const;
//Set uniform //Set uniform
void SetUniform1i(const std::string& name, int value);
void SetUniform1f(const std::string& name, float value);
void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3); void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3);
private: private:
ShaderProgramSource ParseShader(const std::string& filepath); ShaderProgramSource ParseShader(const std::string& filepath);
unsigned int CompileShader(unsigned int type, const std::string& source); unsigned int CompileShader(unsigned int type, const std::string& source);
unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader);
unsigned int GetUniformLocation(const std::string& name); int GetUniformLocation(const std::string& name);
}; };

View File

@@ -0,0 +1,39 @@
#include "Texture.h"
#include "vendor\stb_image\stb_image.h"
Texture::Texture(const std::string& path)
: m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Widht(0), m_Height(0), m_BPP(0)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(path.c_str(), &m_Widht, &m_Height, &m_BPP, 4);
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
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));
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Widht, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer));
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
if (m_LocalBuffer)
stbi_image_free(m_LocalBuffer);
}
Texture::~Texture()
{
GLCall(glDeleteTextures(1, &m_RendererID));
}
void Texture::Bind(unsigned int slot) const
{
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
}
void Texture::Unbind() const
{
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "Renderer.h"
class Texture {
private:
unsigned int m_RendererID;
std::string m_FilePath;
unsigned char* m_LocalBuffer;
int m_Widht, m_Height, m_BPP;
public:
Texture(const std::string& path);
~Texture();
void Bind(unsigned int slot = 0) const;
void Unbind() const;
inline int GetWidth() const { return m_Widht; }
inline int GetHeight() const { return m_Height; }
};

View File

@@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

File diff suppressed because it is too large Load Diff