Added Textures
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,10 +1,2 @@
|
||||
IndexBuffer.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...
|
||||
Texture.cpp
|
||||
Window_practice.vcxproj -> C:\dev\Glfw_Practice\c++\Window_practice\Debug\Window_practice.exe
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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\|
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -149,6 +149,8 @@
|
||||
<ClCompile Include="src\Main.cpp" />
|
||||
<ClCompile Include="src\Renderer.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\VertexBuffer.cpp" />
|
||||
</ItemGroup>
|
||||
@@ -157,6 +159,8 @@
|
||||
<ClInclude Include="src\Main.h" />
|
||||
<ClInclude Include="src\Renderer.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\VertexBuffer.h" />
|
||||
<ClInclude Include="src\VertexBufferLayout.h" />
|
||||
@@ -164,6 +168,9 @@
|
||||
<ItemGroup>
|
||||
<None Include="res\shaders\Basic.shader" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Image Include="res\textures\ChernoLogo.png" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -33,6 +33,12 @@
|
||||
<ClCompile Include="src\Shader.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</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>
|
||||
<ClInclude Include="src\Main.h">
|
||||
@@ -56,8 +62,19 @@
|
||||
<ClInclude Include="src\Shader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</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>
|
||||
<None Include="res\shaders\Basic.shader" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Image Include="res\textures\ChernoLogo.png">
|
||||
<Filter>Resource Files</Filter>
|
||||
</Image>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -2,9 +2,13 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec4 position;
|
||||
layout(location = 1) in vec2 texCoord;
|
||||
|
||||
out vec2 v_TexCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = position;
|
||||
v_TexCoord = texCoord;
|
||||
};
|
||||
|
||||
|
||||
@@ -13,8 +17,12 @@ void main() {
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
in vec2 v_TexCoord;
|
||||
|
||||
uniform vec4 u_Color;
|
||||
uniform sampler2D u_Texture;
|
||||
|
||||
void main() {
|
||||
color = u_Color;
|
||||
vec4 texColor = texture(u_Texture, v_TexCoord);
|
||||
color = texColor;
|
||||
};
|
||||
BIN
Window_practice/res/textures/ChernoLogo.png
Normal file
BIN
Window_practice/res/textures/ChernoLogo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
@@ -9,6 +9,7 @@
|
||||
#include "IndexBuffer.h"
|
||||
#include "VertexArray.h"
|
||||
#include "Shader.h"
|
||||
#include "Texture.h"
|
||||
|
||||
int main(void){
|
||||
// GLFW init
|
||||
@@ -43,10 +44,10 @@ int main(void){
|
||||
{
|
||||
//vertex positions
|
||||
float positions[] = {
|
||||
-0.5f, -0.5f,
|
||||
0.5f, -0.5f,
|
||||
0.5f, 0.5f,
|
||||
-0.5f, 0.5f
|
||||
-0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
//index buffer indecies
|
||||
@@ -55,22 +56,32 @@ int main(void){
|
||||
2, 3, 0
|
||||
};
|
||||
|
||||
GLCall(glEnable(GL_BLEND));
|
||||
GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
||||
|
||||
//creating vertex array object and bindig it
|
||||
unsigned int vao;
|
||||
GLCall(glGenVertexArrays(1, &vao));
|
||||
GLCall(glBindVertexArray(vao));
|
||||
|
||||
VertexArray va;
|
||||
VertexBuffer vb(positions, 4 * 2 * sizeof(float));
|
||||
VertexBuffer vb(positions, 4 * 4 * sizeof(float));
|
||||
|
||||
VertexBufferLayout layout;
|
||||
layout.Push<float>(2);
|
||||
layout.Push<float>(2);
|
||||
va.AddBuffer(vb, layout);
|
||||
|
||||
IndexBuffer ib(indecies, 6);
|
||||
|
||||
Shader shader("res/shaders/Basic.shader");
|
||||
shader.Bind();
|
||||
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;
|
||||
|
||||
//unbinding the buffers
|
||||
|
||||
@@ -102,17 +102,27 @@ void Shader::Unbind() const
|
||||
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)
|
||||
{
|
||||
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())
|
||||
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)
|
||||
std::cout << "Warning: uniform '" << name << "' does not exist!" << std::endl;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ class Shader {
|
||||
private:
|
||||
std::string m_FilePath;
|
||||
unsigned int m_RendererID;
|
||||
std::unordered_map<std::string, unsigned int> m_UniformLocationCache;
|
||||
std::unordered_map<std::string, int> m_UniformLocationCache;
|
||||
public:
|
||||
Shader(const std::string& filepath);
|
||||
~Shader();
|
||||
@@ -21,10 +21,12 @@ public:
|
||||
void Unbind() const;
|
||||
|
||||
//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);
|
||||
private:
|
||||
ShaderProgramSource ParseShader(const std::string& filepath);
|
||||
unsigned int CompileShader(unsigned int type, const std::string& source);
|
||||
unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader);
|
||||
unsigned int GetUniformLocation(const std::string& name);
|
||||
int GetUniformLocation(const std::string& name);
|
||||
};
|
||||
39
Window_practice/src/Texture.cpp
Normal file
39
Window_practice/src/Texture.cpp
Normal 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));
|
||||
}
|
||||
20
Window_practice/src/Texture.h
Normal file
20
Window_practice/src/Texture.h
Normal 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; }
|
||||
};
|
||||
2
Window_practice/src/vendor/stb_image/stb_image.cpp
vendored
Normal file
2
Window_practice/src/vendor/stb_image/stb_image.cpp
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
7871
Window_practice/src/vendor/stb_image/stb_image.h
vendored
Normal file
7871
Window_practice/src/vendor/stb_image/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user