basic textures

This commit is contained in:
2025-11-25 16:15:40 +00:00
parent 47a10623a1
commit bb7a3bafd2
17 changed files with 12154 additions and 10 deletions

42
src/engine/textures.cpp Normal file
View File

@@ -0,0 +1,42 @@
#define GL_GLEXT_PROTOTYPES
#include "engine/textures.hpp"
#include <SDL3_image/SDL_image.h>
// Texture Class Constructor
Texture::Texture(const char* filePath) {
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Load texture image
SDL_Surface* image = IMG_Load(filePath);
if (!image) {
SDL_Log("Failed to load texture image.");
return;
}
// Flip the image vertically
SDL_FlipSurface(image, SDL_FLIP_VERTICAL);
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);
// Determine the format
GLenum format = (image->format == SDL_PIXELFORMAT_RGBA32) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, image->w, image->h, 0, format, GL_UNSIGNED_BYTE, image->pixels);
glGenerateMipmap(GL_TEXTURE_2D);
SDL_DestroySurface(image);
}
// Texture Class Destructor
Texture::~Texture() {
glDeleteTextures(1, &textureID);
}
// Bind Texture Method
void Texture::bind(GLenum textureUnit) const {
glActiveTexture(textureUnit);
glBindTexture(GL_TEXTURE_2D, textureID);
}

View File

@@ -7,10 +7,11 @@ int main(int argc, char* argv[]) {
if (!window.isRunning()) { return -1; }
OpenGLContext glContext(window);
if (!glContext.isInitialized()) { return -1; }
glEnable(GL_DEPTH_TEST);
Camera camera(
glm::vec3(2.0f, 1.0f, 3.0f), // Position
glm::vec3(0.0f, 0.0f, 0.0f), // Target
glm::vec3(3.0f, 4.0f, 6.0f), // Position
glm::vec3(0.0f, 1.0f, 0.0f), // Target
glm::vec3(0.0f, 1.0f, 0.0f) // Up vector
);
@@ -23,7 +24,7 @@ int main(int argc, char* argv[]) {
Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag");
OBJLoader objLoader("assets/models/suzanne.obj");
OBJLoader objLoader("assets/models/Sci-fi_Container_Game.obj");
if (!objLoader.isLoaded()) {
SDL_Log("Failed to load OBJ model.");
return -1;
@@ -36,8 +37,8 @@ int main(int argc, char* argv[]) {
VAO.setAttributePointer(1, 3, GL_FLOAT, false, 8 * sizeof(float), 3 * sizeof(float)); // normal
VAO.setAttributePointer(2, 2, GL_FLOAT, false, 8 * sizeof(float), 6 * sizeof(float)); // texCoord
// Wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Texture texture("assets/textures/Sci-fi_Container_Base_Color.png");
texture.bind(GL_TEXTURE0);
window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
window.showWindow();