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

1
.gitignore vendored
View File

@@ -6,7 +6,6 @@
*.slo *.slo
*.lo *.lo
*.o *.o
*.obj
# Precompiled Headers # Precompiled Headers
*.gch *.gch

View File

@@ -0,0 +1,13 @@
# Blender 5.0.0 MTL File: 'Sci-fi Container Game.blend'
# www.blender.org
newmtl Материал
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Bump -bm 1.000000 Sci-fi_Container_Normal_OpenGL.png

File diff suppressed because it is too large Load Diff

36
assets/models/cube.obj Normal file
View File

@@ -0,0 +1,36 @@
# Blender 4.5.4 LTS
# www.blender.org
mtllib cube.mtl
o Cube
v -1.000000 -1.000000 1.000000 1.0000 1.0000 1.0000
v -1.000000 1.000000 1.000000 1.0000 1.0000 1.0000
v -1.000000 -1.000000 -1.000000 1.0000 1.0000 1.0000
v -1.000000 1.000000 -1.000000 1.0000 1.0000 1.0000
v 1.000000 -1.000000 1.000000 1.0000 1.0000 1.0000
v 1.000000 1.000000 1.000000 1.0000 1.0000 1.0000
v 1.000000 -1.000000 -1.000000 1.0000 1.0000 1.0000
v 1.000000 1.000000 -1.000000 1.0000 1.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -0.0000 -1.0000 -0.0000
vn -0.0000 1.0000 -0.0000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
s 0
usemtl Material.002
f 2/1/1 3/2/1 1/3/1
f 4/1/2 7/2/2 3/3/2
f 8/4/3 5/3/3 7/2/3
f 6/4/4 1/3/4 5/2/4
f 7/4/5 1/3/5 3/1/5
f 4/1/6 6/2/6 8/4/6
f 2/1/1 4/4/1 3/2/1
f 4/1/2 8/4/2 7/2/2
f 8/4/3 6/1/3 5/3/3
f 6/4/4 2/1/4 1/3/4
f 7/4/5 5/2/5 1/3/5
f 4/1/6 2/3/6 6/2/6

2976
assets/models/suzanne.obj Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 KiB

View File

@@ -7,6 +7,7 @@
#include "engine/buffers.hpp" #include "engine/buffers.hpp"
#include "engine/shaders.hpp" #include "engine/shaders.hpp"
#include "engine/camera.hpp" #include "engine/camera.hpp"
#include "engine/textures.hpp"
// OpenGL Context Class // OpenGL Context Class
class OpenGLContext { class OpenGLContext {

19
inc/engine/textures.hpp Normal file
View File

@@ -0,0 +1,19 @@
#ifndef TEXTURES_HPP
#define TEXTURES_HPP
// Includes
#include <SDL3/SDL_opengl.h>
// Texture Class
class Texture {
public:
Texture(const char* filePath);
~Texture();
void bind(GLenum textureUnit) const;
private:
GLuint textureID;
};
#endif

View File

@@ -1,8 +1,11 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
in vec3 color; // Input color from the vertex shader in vec3 Color; // Input color from the vertex shader
in vec2 TexCoord; // Input texture coordinate from the vertex shader
uniform sampler2D Texture1;
void main() { void main() {
FragColor = vec4(color, 1.0); // Apply the color directly FragColor = texture(Texture1, TexCoord);
} }

View File

@@ -3,7 +3,8 @@
layout(location = 0) in vec3 position; // Vertex position layout(location = 0) in vec3 position; // Vertex position
layout(location = 1) in vec3 normal; // Vertex normal layout(location = 1) in vec3 normal; // Vertex normal
layout(location = 2) in vec2 texCoord; // Vertex texture coordinate layout(location = 2) in vec2 texCoord; // Vertex texture coordinate
out vec3 color; // Output color to the fragment shader out vec3 Color; // Output color to the fragment shader
out vec2 TexCoord; // Output texture coordinate to the fragment shader
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@@ -11,5 +12,6 @@ uniform mat4 projection;
void main() { void main() {
gl_Position = projection * view * model * vec4(position, 1.0); // Apply transformation matrices gl_Position = projection * view * model * vec4(position, 1.0); // Apply transformation matrices
color = vec3(1.0, 0.5, 0.2); // Set output color (orange) Color = normal * 0.5 + 0.5; // Simple coloring based on normal
TexCoord = texCoord; // Pass through the texture coordinate
} }

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; } if (!window.isRunning()) { return -1; }
OpenGLContext glContext(window); OpenGLContext glContext(window);
if (!glContext.isInitialized()) { return -1; } if (!glContext.isInitialized()) { return -1; }
glEnable(GL_DEPTH_TEST);
Camera camera( Camera camera(
glm::vec3(2.0f, 1.0f, 3.0f), // Position glm::vec3(3.0f, 4.0f, 6.0f), // Position
glm::vec3(0.0f, 0.0f, 0.0f), // Target glm::vec3(0.0f, 1.0f, 0.0f), // Target
glm::vec3(0.0f, 1.0f, 0.0f) // Up vector 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"); 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()) { if (!objLoader.isLoaded()) {
SDL_Log("Failed to load OBJ model."); SDL_Log("Failed to load OBJ model.");
return -1; 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(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 VAO.setAttributePointer(2, 2, GL_FLOAT, false, 8 * sizeof(float), 6 * sizeof(float)); // texCoord
// Wireframe mode Texture texture("assets/textures/Sci-fi_Container_Base_Color.png");
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); texture.bind(GL_TEXTURE0);
window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
window.showWindow(); window.showWindow();