Add texture loading
This commit is contained in:
parent
fbb4cd5101
commit
5adc6e80e6
BIN
assets/textures/clay_texture_1k.png
Normal file
BIN
assets/textures/clay_texture_1k.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 MiB |
BIN
assets/textures/plaster_texture_1k.png
Normal file
BIN
assets/textures/plaster_texture_1k.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 MiB |
7988
lib/stb_image.h
Normal file
7988
lib/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,12 @@
|
||||
#version 330
|
||||
|
||||
in vec4 vertex_color;
|
||||
in vec2 texture_coord;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
color = vertex_color;
|
||||
color = texture(tex, texture_coord);
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
#version 330
|
||||
|
||||
layout (location = 0) in vec3 pos;
|
||||
layout (location = 1) in vec2 tex;
|
||||
|
||||
out vec4 vertex_color;
|
||||
out vec2 texture_coord;
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 model;
|
||||
@ -11,4 +13,5 @@ uniform mat4 view;
|
||||
void main() {
|
||||
gl_Position = projection * view * model * vec4(pos, 1.0);
|
||||
vertex_color = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);
|
||||
texture_coord = tex;
|
||||
}
|
||||
|
||||
29
src/main.cpp
29
src/main.cpp
@ -1,7 +1,10 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
|
||||
#include "mesh.h"
|
||||
#include "shader.h"
|
||||
#include "window.h"
|
||||
#include "camera.h"
|
||||
#include "texture.h"
|
||||
|
||||
|
||||
static const char *vertex_shader_code = "shaders/shader.vert";
|
||||
@ -21,10 +24,11 @@ int main() {
|
||||
{
|
||||
// Create a mesh
|
||||
f32 vertices[] = {
|
||||
-1.0f, -1.0f, 0.0f,
|
||||
0.0f, -1.0f, 1.0f,
|
||||
1.0f, -1.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f
|
||||
// x y z u v
|
||||
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, -1.0f, 1.0f, 1.0f, 0.0f,
|
||||
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
u32 indices[] = {
|
||||
@ -35,11 +39,11 @@ int main() {
|
||||
};
|
||||
|
||||
Mesh *pyramid1 = new Mesh();
|
||||
create_mesh(pyramid1, vertices, indices, 12, 12);
|
||||
create_mesh(pyramid1, vertices, indices, 20, 12);
|
||||
meshes[0] = pyramid1;
|
||||
|
||||
Mesh *pyramid2 = new Mesh();
|
||||
create_mesh(pyramid2, vertices, indices, 12, 12);
|
||||
create_mesh(pyramid2, vertices, indices, 20, 12);
|
||||
meshes[1] = pyramid2;
|
||||
}
|
||||
|
||||
@ -50,12 +54,19 @@ int main() {
|
||||
shaders[0] = *shader;
|
||||
}
|
||||
|
||||
Texture clay_texture = Texture((char *)"assets/textures/clay_texture_1k.png");
|
||||
load_texture(&clay_texture);
|
||||
|
||||
Texture plaster_texture = Texture((char *)"assets/textures/plaster_texture_1k.png");
|
||||
load_texture(&plaster_texture);
|
||||
|
||||
Matrix4 projection = glm::perspective(
|
||||
45.0f, (GLfloat)window.buffer_width / (GLfloat)window.buffer_height, 0.1f,
|
||||
45.0f, (f32)window.buffer_width / (f32)window.buffer_height, 0.1f,
|
||||
100.0f);
|
||||
|
||||
Camera camera(Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f), 90.0f, 0.0f, 5.0f, 1.0f);
|
||||
|
||||
|
||||
// Loop until window is closed
|
||||
while (!glfwWindowShouldClose(window.gl_window)) {
|
||||
// Calculate delta
|
||||
@ -82,22 +93,22 @@ int main() {
|
||||
{
|
||||
Matrix4 model(1.0f);
|
||||
model = glm::translate(model, Vector3(0.0f, 0.0f, -2.5f));
|
||||
// model = glm::rotate(model, current_angle * RADIAN_FACTOR, Vector3(0.0f, 1.0f, 0.0f));
|
||||
model = glm::scale(model, Vector3(0.4f, 0.4f, 1.0f));
|
||||
|
||||
glUniformMatrix4fv(shaders[0].uniform_model, 1, GL_FALSE, glm::value_ptr(model));
|
||||
glUniformMatrix4fv(shaders[0].uniform_projection, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
use_texture(&clay_texture);
|
||||
render_mesh(meshes[0]);
|
||||
}
|
||||
|
||||
{
|
||||
Matrix4 model(1.0f);
|
||||
model = glm::translate(model, Vector3(0.0f, 0.0f, -3.5f));
|
||||
// model = glm::rotate(model, current_angle * RADIAN_FACTOR, Vector3(0.0f, 1.0f, 0.0f));
|
||||
model = glm::scale(model, Vector3(0.4f, 0.4f, 1.0f));
|
||||
|
||||
glUniformMatrix4fv(shaders[0].uniform_model, 1, GL_FALSE, glm::value_ptr(model));
|
||||
glUniformMatrix4fv(shaders[0].uniform_projection, 1, GL_FALSE, glm::value_ptr(projection));
|
||||
use_texture(&plaster_texture);
|
||||
render_mesh(meshes[1]);
|
||||
}
|
||||
|
||||
|
||||
@ -31,9 +31,14 @@ void create_mesh(
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices_count, vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
// Position vertices
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 5, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Texture coordinates
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 5, (void *)(sizeof(vertices[0]) * 3));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Unbinding
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
49
src/texture.cpp
Normal file
49
src/texture.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include "texture.h"
|
||||
|
||||
Texture::Texture(char *file_path) : file_path(file_path) {}
|
||||
|
||||
Texture::~Texture() {
|
||||
clear_texture(this);
|
||||
}
|
||||
|
||||
bool load_texture(Texture *texture) {
|
||||
u8 *texture_data = stbi_load(texture->file_path, &texture->width, &texture->height, &texture->bit_depth, 0);
|
||||
|
||||
if (!texture_data) {
|
||||
printf("Failed to find: %s\n", texture->file_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
glGenTextures(1, &texture->id);
|
||||
glBindTexture(GL_TEXTURE_2D, texture->id);
|
||||
|
||||
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);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// Maybe just set to GL_RGB
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
stbi_image_free(texture_data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void use_texture(Texture *texture) {
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture->id);
|
||||
}
|
||||
|
||||
void clear_texture(Texture *texture) {
|
||||
glDeleteTextures(1, &texture->id);
|
||||
texture->id = 0;
|
||||
texture->width = 0;
|
||||
texture->height = 0;
|
||||
texture->bit_depth = 0;
|
||||
texture->file_path = NULL;
|
||||
}
|
||||
|
||||
20
src/texture.h
Normal file
20
src/texture.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include "math.h"
|
||||
#include "../lib/stb_image.h"
|
||||
|
||||
struct Texture {
|
||||
u32 id = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int bit_depth = 0;
|
||||
char *file_path;
|
||||
|
||||
Texture(char *file_path);
|
||||
~Texture();
|
||||
};
|
||||
|
||||
bool load_texture(Texture *texture);
|
||||
void use_texture(Texture *texture);
|
||||
void clear_texture(Texture *texture);
|
||||
Loading…
x
Reference in New Issue
Block a user