added a texture class

This commit is contained in:
Lukian 2025-01-29 12:51:09 +01:00
parent 78c7efd063
commit ed8237a601
8 changed files with 8076 additions and 9 deletions

7988
include/stb_image.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,15 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <string>
class Texture
{
public:
unsigned int ID;
Texture(const char* texturePath);
void use();
};
#endif

View file

@ -1,7 +1,12 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
FragColor = texture(ourTexture, TexCoord);
}

View file

@ -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;
}

View file

@ -2,6 +2,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <shaders/shader.hpp>
#include <textures/texture.hpp>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
@ -34,13 +35,16 @@ int main()
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
float vertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 2
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
unsigned int VBO;
@ -59,10 +63,18 @@ int main()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
Shader ourShader("shaders/shader.vs", "shaders/shader.fs");
Texture sataaAndagii("textures/saataa_andagii.png");
Texture ohMyGah("textures/oh_my_gah.png");
while(!glfwWindowShouldClose(window))
{
@ -70,8 +82,9 @@ int main()
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ourShader.use();
ohMyGah.use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

39
src/texture.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <glad/glad.h> // include glad to get all the required OpenGL headers
#include <textures/texture.hpp>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Texture::Texture(const char* texturePath)
{
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);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0);
if (data)
{
GLenum format = GL_RGB;
if (nrChannels == 1) format = GL_RED;
else if (nrChannels == 3) format = GL_RGB;
else if (nrChannels == 4) format = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
void Texture::use()
{
glBindTexture(GL_TEXTURE_2D, ID);
}

BIN
textures/oh_my_gah.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 KiB

BIN
textures/saataa_andagii.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB