added texture class, changed shader extension names

This commit is contained in:
Claire Schwarzer 2026-01-30 00:14:20 +01:00
parent 95be5f081e
commit 6fb3ffcfed
5 changed files with 58 additions and 53 deletions

View file

@ -166,17 +166,18 @@
<ItemGroup> <ItemGroup>
<Content Include="placeholders\awesomeface.png" /> <Content Include="placeholders\awesomeface.png" />
<Content Include="placeholders\wall.jpg" /> <Content Include="placeholders\wall.jpg" />
<Content Include="shaders\basicVertex.vs" /> <Content Include="shaders\basicVertex.vert" />
<Content Include="vendor\glfw\glfw3.lib" /> <Content Include="vendor\glfw\glfw3.lib" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="engine\shader.h" /> <ClInclude Include="engine\shader.h" />
<ClInclude Include="engine\texture.h" />
<ClInclude Include="vendor\glad\glad.h" /> <ClInclude Include="vendor\glad\glad.h" />
<ClInclude Include="vendor\KHR\khrplatform.h" /> <ClInclude Include="vendor\KHR\khrplatform.h" />
<ClInclude Include="vendor\stb_image\stb_image.h" /> <ClInclude Include="vendor\stb_image\stb_image.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="shaders\basicFragment.fs" /> <Compile Include="shaders\basicFragment.frag" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

47
Engine/engine/texture.h Normal file
View file

@ -0,0 +1,47 @@
#pragma once
#include <iostream>
#include <glad/glad.h>
#include <stb_image/stb_image.h>
class Texture
{
public:
unsigned int ID;
int width, height, nrChannels;
Texture(const char* texturePath, bool flip = false)
{
// Create the Texture and set our ID
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Grab the texture from our path
if (flip)
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0);
if (data)
{
GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
if (flip)
stbi_set_flip_vertically_on_load(false);
stbi_image_free(data);
}
};

View file

@ -5,6 +5,7 @@
#include <stb_image/stb_image.h> #include <stb_image/stb_image.h>
#include <shader.h> #include <shader.h>
#include <texture.h>
const int SCREEN_WIDTH = 800; const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600; const int SCREEN_HEIGHT = 600;
@ -71,7 +72,7 @@ int main(int argc, char* argv[])
glfwSetFramebufferSizeCallback(window, window_resize_callback); glfwSetFramebufferSizeCallback(window, window_resize_callback);
// Load Shader // Load Shader
Shader shaderTest("shaders/basicVertex.vs", "shaders/basicFragment.fs"); Shader shaderTest("shaders/basicVertex.vert", "shaders/basicFragment.frag");
float vertices[] = { float vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
@ -112,56 +113,12 @@ int main(int argc, char* argv[])
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
unsigned int texture1, texture2; Texture texture1("placeholders/wall.jpg");
glGenTextures(1, &texture1); Texture texture2("placeholders/awesomeface.png", true);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load("placeholders/wall.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// texture 2
// ---------
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
stbi_set_flip_vertically_on_load(true);
data = stbi_load("placeholders/awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
// note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
shaderTest.Use(); // don't forget to activate/use the shader before setting uniforms! shaderTest.Use(); // don't forget to activate/use the shader before setting uniforms!
shaderTest.setInt("ourTexture", 0); shaderTest.setInt("ourTexture", texture1.ID);
shaderTest.setInt("decal", 1); shaderTest.setInt("decal", texture2.ID);
// Render Loop // Render Loop
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
@ -176,9 +133,9 @@ int main(int argc, char* argv[])
shaderTest.Use(); shaderTest.Use();
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1); glBindTexture(GL_TEXTURE_2D, texture1.ID);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2); glBindTexture(GL_TEXTURE_2D, texture2.ID);
// Draw our triangle // Draw our triangle
glBindVertexArray(VAO); glBindVertexArray(VAO);