feat(parse): read uvs and normals indices
This commit is contained in:
parent
6823554e63
commit
7bc350942d
|
@ -36,7 +36,6 @@ struct s_persp_tool
|
||||||
t_mat4 mat4_transpose(t_mat4 m);
|
t_mat4 mat4_transpose(t_mat4 m);
|
||||||
|
|
||||||
t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs);
|
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(
|
t_mat4 mat4_perspective(
|
||||||
double fov, double aspect, double near, double far);
|
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);
|
t_mat4 mat4_scale(t_mat4 m, t_vec3 s);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,9 @@ struct s_obj
|
||||||
t_buf_d vertices;
|
t_buf_d vertices;
|
||||||
t_buf_d uvs;
|
t_buf_d uvs;
|
||||||
t_buf_d normals;
|
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
|
struct s_env
|
||||||
|
@ -131,3 +133,4 @@ void init_env(t_env *e);
|
||||||
int run(t_env *e);
|
int run(t_env *e);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
After Width: | Height: | Size: 132 KiB |
19
src/env.c
19
src/env.c
|
@ -18,7 +18,9 @@ void free_env(t_env *e)
|
||||||
free(e->object.vertices.data);
|
free(e->object.vertices.data);
|
||||||
free(e->object.uvs.data);
|
free(e->object.uvs.data);
|
||||||
free(e->object.normals.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)
|
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.vertices.data = (double *)malloc(sizeof(double) * BUFFER_SIZE);
|
||||||
e->object.uvs.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.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);
|
sizeof(unsigned int) * BUFFER_SIZE);
|
||||||
e->object.vertices.ptr = 0;
|
e->object.vertices.ptr = 0;
|
||||||
e->object.uvs.ptr = 0;
|
e->object.uvs.ptr = 0;
|
||||||
e->object.normals.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.vertices.len = BUFFER_SIZE;
|
||||||
e->object.uvs.len = BUFFER_SIZE;
|
e->object.uvs.len = BUFFER_SIZE;
|
||||||
e->object.normals.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)
|
void init_transformations(t_env *e)
|
||||||
|
@ -72,3 +82,4 @@ void init_env(t_env *e)
|
||||||
init_object(e);
|
init_object(e);
|
||||||
init_transformations(e);
|
init_transformations(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
#include "mat4.h"
|
#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;
|
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];
|
+ 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]
|
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];
|
+ 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]
|
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];
|
+ 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]
|
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];
|
+ rhs.data[14] * m.data[11] + rhs.data[15] * m.data[15];
|
||||||
return (out);
|
return (out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,12 @@ void parse_triangulate(t_buf_ui *indices, int vertex_count)
|
||||||
free(data_tmp);
|
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)
|
int parse_line(t_env *e, char *line)
|
||||||
{
|
{
|
||||||
char *token;
|
char *token;
|
||||||
|
@ -86,6 +92,7 @@ int parse_line(t_env *e, char *line)
|
||||||
token_count++;
|
token_count++;
|
||||||
}
|
}
|
||||||
if (gl_buf_type == INDEX && token_count > 4)
|
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);
|
return (SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,10 +34,31 @@ int parse_append_data_ui(t_buf_ui *buffer, char *token)
|
||||||
buffer->len += BUFFER_SIZE;
|
buffer->len += BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
buffer->data[buffer->ptr] = atoi(token) - 1;
|
buffer->data[buffer->ptr] = atoi(token) - 1;
|
||||||
|
printf("token %s buffer->data[ptr] %d\n", token, buffer->data[buffer->ptr]);
|
||||||
buffer->ptr++;
|
buffer->ptr++;
|
||||||
return (SUCCESS);
|
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(
|
int parse_token(
|
||||||
t_env *e,
|
t_env *e,
|
||||||
char *token,
|
char *token,
|
||||||
|
@ -54,6 +75,7 @@ int parse_token(
|
||||||
if (gl_buf_type == NORMAL)
|
if (gl_buf_type == NORMAL)
|
||||||
ret = parse_append_data_d(&(e->object.normals), token);
|
ret = parse_append_data_d(&(e->object.normals), token);
|
||||||
if (gl_buf_type == INDEX)
|
if (gl_buf_type == INDEX)
|
||||||
ret = parse_append_data_ui(&(e->object.indices), token);
|
ret = parse_append_data_index(e, token);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
42
src/run.c
42
src/run.c
|
@ -14,6 +14,9 @@
|
||||||
#include "mat4.h"
|
#include "mat4.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
GLuint VBO;
|
GLuint VBO;
|
||||||
GLuint IBO;
|
GLuint IBO;
|
||||||
GLuint gScaleLocation;
|
GLuint gScaleLocation;
|
||||||
|
@ -24,6 +27,9 @@ GLuint gModelLocation;
|
||||||
GLuint gCountLocation;
|
GLuint gCountLocation;
|
||||||
GLuint index_count;
|
GLuint index_count;
|
||||||
|
|
||||||
|
GLuint gSamplerLocation;
|
||||||
|
GLuint texture;
|
||||||
|
|
||||||
const char* pVSFileName = "src/shader.vs";
|
const char* pVSFileName = "src/shader.vs";
|
||||||
const char* pFSFileName = "src/shader.fs";
|
const char* pFSFileName = "src/shader.fs";
|
||||||
|
|
||||||
|
@ -62,11 +68,17 @@ static void RenderSceneCB(t_env *e)
|
||||||
glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data);
|
glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data);
|
||||||
glUniformMatrix4fv(gModelLocation, 1, GL_FALSE, (GLfloat *)model.data);
|
glUniformMatrix4fv(gModelLocation, 1, GL_FALSE, (GLfloat *)model.data);
|
||||||
glUniform1i(gCountLocation, index_count);
|
glUniform1i(gCountLocation, index_count);
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glUniform1i(gSamplerLocation, 0);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
|
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);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||||
|
|
||||||
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0);
|
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)
|
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);
|
glGenBuffers(1, &VBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, e->object.vertices.ptr * sizeof(double), e->object.vertices.data, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, e->object.vertices.ptr * sizeof(double), e->object.vertices.data, GL_STATIC_DRAW);
|
||||||
glGenBuffers(1, &IBO);
|
glGenBuffers(1, &IBO);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 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)
|
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");
|
gProjLocation = glGetUniformLocation(e->shader_program, "proj");
|
||||||
gViewLocation = glGetUniformLocation(e->shader_program, "view");
|
gViewLocation = glGetUniformLocation(e->shader_program, "view");
|
||||||
gModelLocation = glGetUniformLocation(e->shader_program, "model");
|
gModelLocation = glGetUniformLocation(e->shader_program, "model");
|
||||||
|
gSamplerLocation = glGetUniformLocation(e->shader_program, "ourTexture");
|
||||||
// assert(gScaleLocation != 0xFFFFFFFF);
|
// assert(gScaleLocation != 0xFFFFFFFF);
|
||||||
assert(gScaleIntLocation != 0xFFFFFFFF);
|
assert(gScaleIntLocation != 0xFFFFFFFF);
|
||||||
assert(gProjLocation != 0xFFFFFFFF);
|
assert(gProjLocation != 0xFFFFFFFF);
|
||||||
|
@ -258,9 +278,9 @@ int run(t_env *e)
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
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.vertices.ptr);
|
||||||
printf("%ld\n", e->object.indices.ptr);
|
printf("%ld\n", e->object.vertices_indices.ptr);
|
||||||
GLenum res = glewInit();
|
GLenum res = glewInit();
|
||||||
if (res != GLEW_OK)
|
if (res != GLEW_OK)
|
||||||
{
|
{
|
||||||
|
@ -270,6 +290,21 @@ int run(t_env *e)
|
||||||
printf("GL version: %s\n", glGetString(GL_VERSION));
|
printf("GL version: %s\n", glGetString(GL_VERSION));
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
CreateVertexBuffer(e);
|
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);
|
CompileShaders(e);
|
||||||
/* Loop until the user closes the window */
|
/* Loop until the user closes the window */
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
|
@ -288,3 +323,4 @@ int run(t_env *e)
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
uniform float time;
|
uniform float time;
|
||||||
|
uniform sampler2D ourTexture;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
flat in int vID;
|
flat in int vID;
|
||||||
|
|
||||||
|
|
||||||
float rand(float n)
|
float rand(float n)
|
||||||
{
|
{
|
||||||
return fract(sin(n) * 43758.5453123);
|
return fract(sin(n) * 43758.5453123);
|
||||||
|
@ -13,5 +15,7 @@ float rand(float n)
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time))));
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue