feat(parse): read uvs and normals indices

This commit is contained in:
gbrochar 2024-04-24 20:27:43 +02:00
parent 6823554e63
commit 7bc350942d
10 changed files with 8084 additions and 23 deletions

View File

@ -36,7 +36,6 @@ struct s_persp_tool
t_mat4 mat4_transpose(t_mat4 m);
t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs);
t_mat4 mat4_multiply_tmp(t_mat4 m, t_mat4 rhs);
t_mat4 mat4_perspective(
double fov, double aspect, double near, double far);
@ -65,3 +64,4 @@ t_mat4 mat4_translate(t_mat4 m, t_vec3 t);
t_mat4 mat4_scale(t_mat4 m, t_vec3 s);
#endif

View File

@ -86,7 +86,9 @@ struct s_obj
t_buf_d vertices;
t_buf_d uvs;
t_buf_d normals;
t_buf_ui indices;
t_buf_ui vertices_indices;
t_buf_ui uvs_indices;
t_buf_ui normals_indices;
};
struct s_env
@ -131,3 +133,4 @@ void init_env(t_env *e);
int run(t_env *e);
#endif

7985
inc/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

BIN
resources/fox.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

View File

@ -18,7 +18,9 @@ void free_env(t_env *e)
free(e->object.vertices.data);
free(e->object.uvs.data);
free(e->object.normals.data);
free(e->object.indices.data);
free(e->object.vertices_indices.data);
free(e->object.uvs_indices.data);
free(e->object.normals_indices.data);
}
void init_window(t_env *e)
@ -40,16 +42,24 @@ void init_object(t_env *e)
e->object.vertices.data = (double *)malloc(sizeof(double) * BUFFER_SIZE);
e->object.uvs.data = (double *)malloc(sizeof(double) * BUFFER_SIZE);
e->object.normals.data = (double *)malloc(sizeof(double) * BUFFER_SIZE);
e->object.indices.data = (unsigned int *)malloc(
e->object.vertices_indices.data = (unsigned int *)malloc(
sizeof(unsigned int) * BUFFER_SIZE);
e->object.uvs_indices.data = (unsigned int *)malloc(
sizeof(unsigned int) * BUFFER_SIZE);
e->object.normals_indices.data = (unsigned int *)malloc(
sizeof(unsigned int) * BUFFER_SIZE);
e->object.vertices.ptr = 0;
e->object.uvs.ptr = 0;
e->object.normals.ptr = 0;
e->object.indices.ptr = 0;
e->object.vertices_indices.ptr = 0;
e->object.uvs_indices.ptr = 0;
e->object.normals_indices.ptr = 0;
e->object.vertices.len = BUFFER_SIZE;
e->object.uvs.len = BUFFER_SIZE;
e->object.normals.len = BUFFER_SIZE;
e->object.indices.len = BUFFER_SIZE;
e->object.vertices_indices.len = BUFFER_SIZE;
e->object.uvs_indices.len = BUFFER_SIZE;
e->object.normals_indices.len = BUFFER_SIZE;
}
void init_transformations(t_env *e)
@ -72,3 +82,4 @@ void init_env(t_env *e)
init_object(e);
init_transformations(e);
}

View File

@ -12,7 +12,7 @@
#include "mat4.h"
t_mat4 mat4_multiply_tmp(t_mat4 m, t_mat4 rhs)
t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs)
{
t_mat4 out;
@ -34,14 +34,6 @@ t_mat4 mat4_multiply_tmp(t_mat4 m, t_mat4 rhs)
+ rhs.data[6] * m.data[11] + rhs.data[7] * m.data[15];
out.data[8] = rhs.data[8] * m.data[0] + rhs.data[9] * m.data[4]
+ rhs.data[10] * m.data[8] + rhs.data[11] * m.data[12];
return (out);
}
t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs)
{
t_mat4 out;
out = mat4_multiply_tmp(m, rhs);
out.data[9] = rhs.data[8] * m.data[1] + rhs.data[9] * m.data[5]
+ rhs.data[10] * m.data[9] + rhs.data[11] * m.data[13];
out.data[10] = rhs.data[8] * m.data[2] + rhs.data[9] * m.data[6]
@ -58,3 +50,4 @@ t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs)
+ rhs.data[14] * m.data[11] + rhs.data[15] * m.data[15];
return (out);
}

View File

@ -66,6 +66,12 @@ void parse_triangulate(t_buf_ui *indices, int vertex_count)
free(data_tmp);
}
void parse_triangulate_dispatcher(t_env *e, int vertex_count) {
parse_triangulate(&(e->object.vertices_indices), vertex_count);
parse_triangulate(&(e->object.uvs_indices), vertex_count);
parse_triangulate(&(e->object.normals_indices), vertex_count);
}
int parse_line(t_env *e, char *line)
{
char *token;
@ -86,6 +92,7 @@ int parse_line(t_env *e, char *line)
token_count++;
}
if (gl_buf_type == INDEX && token_count > 4)
parse_triangulate(&(e->object.indices), token_count - 1);
parse_triangulate_dispatcher(e, token_count - 1);
return (SUCCESS);
}

View File

@ -34,10 +34,31 @@ int parse_append_data_ui(t_buf_ui *buffer, char *token)
buffer->len += BUFFER_SIZE;
}
buffer->data[buffer->ptr] = atoi(token) - 1;
printf("token %s buffer->data[ptr] %d\n", token, buffer->data[buffer->ptr]);
buffer->ptr++;
return (SUCCESS);
}
int parse_append_data_index(t_env *e, char *phrase) {
char *token;
size_t token_count = 0;
int ret;
while ((token = strtok_r(phrase, "/", &phrase))) {
if (token_count == 0) {
ret = parse_append_data_ui(&(e->object.vertices_indices), token);
}
else if (token_count == 1) {
ret = parse_append_data_ui(&(e->object.uvs_indices), token);
}
else if (token_count == 2) {
ret = parse_append_data_ui(&(e->object.normals_indices), token);
}
token_count++;
}
return ret;
}
int parse_token(
t_env *e,
char *token,
@ -54,6 +75,7 @@ int parse_token(
if (gl_buf_type == NORMAL)
ret = parse_append_data_d(&(e->object.normals), token);
if (gl_buf_type == INDEX)
ret = parse_append_data_ui(&(e->object.indices), token);
ret = parse_append_data_index(e, token);
return (ret);
}

View File

@ -14,6 +14,9 @@
#include "mat4.h"
#include "vec3.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
GLuint VBO;
GLuint IBO;
GLuint gScaleLocation;
@ -24,6 +27,9 @@ GLuint gModelLocation;
GLuint gCountLocation;
GLuint index_count;
GLuint gSamplerLocation;
GLuint texture;
const char* pVSFileName = "src/shader.vs";
const char* pFSFileName = "src/shader.fs";
@ -49,7 +55,7 @@ static void RenderSceneCB(t_env *e)
target.x = 0;
target.y = 0;
target.z = 9;
t_mat4 proj = mat4_perspective(45.0 * M_PI / 180.0, 800.0 / 600.0, 0.001, 1000);
t_mat4 view = mat4_inverse(mat4_lookat(eye, up, target));
// t_mat4 model = mat4_rotatey(mat4_rotatex(mat4_identity(), Scale), Scale / 1.3);
@ -62,11 +68,17 @@ static void RenderSceneCB(t_env *e)
glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data);
glUniformMatrix4fv(gModelLocation, 1, GL_FALSE, (GLfloat *)model.data);
glUniform1i(gCountLocation, index_count);
glActiveTexture(GL_TEXTURE0);
glUniform1i(gSamplerLocation, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
//layout text
//glVertexAttribPointer(2, 2, GL_DOUBLE, GL_FALSE, 0, 0);
//glBindTexture(GL_TEXTURE_2D, texture);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0);
@ -76,12 +88,19 @@ static void RenderSceneCB(t_env *e)
static void CreateVertexBuffer(t_env *e)
{
printf("status of different buffers:\n");
printf("vertices len %ld\n", e->object.vertices.ptr);
printf("uv len %ld\n", e->object.uvs.ptr);
printf("normals len %ld\n", e->object.normals.ptr);
printf("vertices indices len %ld\n", e->object.vertices_indices.ptr);
printf("uvs indices len %ld\n", e->object.uvs_indices.ptr);
printf("normals indices len %ld\n", e->object.normals_indices.ptr);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, e->object.vertices.ptr * sizeof(double), e->object.vertices.data, GL_STATIC_DRAW);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->object.indices.ptr * sizeof(int), e->object.indices.data, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->object.vertices_indices.ptr * sizeof(int), e->object.vertices_indices.data, GL_STATIC_DRAW);
}
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType, GLuint *shader)
@ -175,6 +194,7 @@ static void CompileShaders(t_env *e)
gProjLocation = glGetUniformLocation(e->shader_program, "proj");
gViewLocation = glGetUniformLocation(e->shader_program, "view");
gModelLocation = glGetUniformLocation(e->shader_program, "model");
gSamplerLocation = glGetUniformLocation(e->shader_program, "ourTexture");
// assert(gScaleLocation != 0xFFFFFFFF);
assert(gScaleIntLocation != 0xFFFFFFFF);
assert(gProjLocation != 0xFFFFFFFF);
@ -258,9 +278,9 @@ int run(t_env *e)
glfwMakeContextCurrent(window);
index_count = e->object.indices.ptr;
index_count = e->object.vertices_indices.ptr;
printf("%ld\n", e->object.vertices.ptr);
printf("%ld\n", e->object.indices.ptr);
printf("%ld\n", e->object.vertices_indices.ptr);
GLenum res = glewInit();
if (res != GLEW_OK)
{
@ -270,6 +290,21 @@ int run(t_env *e)
printf("GL version: %s\n", glGetString(GL_VERSION));
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer(e);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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);
int tex_width, tex_height, nrChannels;
unsigned char *tex_data = stbi_load("resources/fox.jpg", &tex_width, &tex_height, &nrChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(tex_data);
CompileShaders(e);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
@ -287,4 +322,5 @@ int run(t_env *e)
free_shaders(e);
glfwTerminate();
return (SUCCESS);
}
}

View File

@ -1,10 +1,12 @@
#version 330
uniform float time;
uniform sampler2D ourTexture;
out vec4 FragColor;
flat in int vID;
float rand(float n)
{
return fract(sin(n) * 43758.5453123);
@ -13,5 +15,7 @@ float rand(float n)
void main()
{
vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time))));
FragColor = vec4(color, 1.0);
FragColor = vec4(color * 0.001 + texture(ourTexture, gl_FragCoord.xy / vec2(480., 360.)).rgb, 1.0);
//FragColor = vec4(color * 0.1 + texture(ourTexture, gl_FragCoord.xy).rgb, 1.0);
//FragColor = vec4(color * 0.01 + texture2D(ourTexture, gl_FragCoord.xy / vec2(480., 360.)).rgb, 1.0);
}