feat(center): normalize object size

This commit is contained in:
gbrochar 2024-04-25 10:20:20 +02:00
parent 71e157e76f
commit 51bfbd2214
5 changed files with 74 additions and 26 deletions

View File

@ -6,6 +6,7 @@
# include <stdlib.h>
# include <assert.h>
# include <math.h>
# include <float.h>
# include <GL/glew.h>
# include <GLFW/glfw3.h>

BIN
resources/blue.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 KiB

View File

@ -16,26 +16,54 @@
void center_vertices(t_buf_d *vertices)
{
t_vec3 average;
t_vec3 max = { .x = DBL_MIN, .y = DBL_MIN, .z = DBL_MIN };
t_vec3 min = { .x = DBL_MAX, .y = DBL_MAX, .z = DBL_MAX };
for (size_t i = 0; i < vertices->ptr; i++)
{
if (i % 3 == 0)
if (i % 3 == 0) {
if (vertices->data[i] > max.x)
max.x = vertices->data[i];
if (vertices->data[i] < min.x)
min.x = vertices->data[i];
average.x += vertices->data[i];
else if (i % 3 == 1)
}
else if (i % 3 == 1) {
if (vertices->data[i] > max.y)
max.y = vertices->data[i];
if (vertices->data[i] < min.y)
min.y = vertices->data[i];
average.y += vertices->data[i];
else if (i % 3 == 2)
}
else if (i % 3 == 2) {
if (vertices->data[i] > max.z)
max.z = vertices->data[i];
if (vertices->data[i] < min.z)
min.z = vertices->data[i];
average.z += vertices->data[i];
}
}
average.x /= (vertices->ptr / 3);
average.y /= (vertices->ptr / 3);
average.z /= (vertices->ptr / 3);
double max_k = max.x - min.x;
max_k = max_k > max.y - min.y ? max_k : max.y - min.y;
max_k = max_k > max.z - min.z ? max_k : max.z - min.z;
for (size_t i = 0; i < vertices->ptr; i++)
{
if (i % 3 == 0)
if (i % 3 == 0) {
vertices->data[i] -= average.x;
else if (i % 3 == 1)
vertices->data[i] /= max_k;
}
else if (i % 3 == 1) {
vertices->data[i] -= average.y;
else if (i % 3 == 2)
vertices->data[i] /= max_k;
}
else if (i % 3 == 2) {
vertices->data[i] -= average.z;
vertices->data[i] /= max_k;
}
}
}
}

View File

@ -28,6 +28,8 @@ GLuint has_texture_location;
//GLuint gCountLocation;
GLuint index_count;
int is_good;
GLuint gSamplerLocation;
GLuint texture;
@ -49,15 +51,15 @@ static void RenderSceneCB(t_env *e)
eye.x = 0;
eye.y = 0;
eye.z = 10;
eye.z = 2;
up.x = 0;
up.y = 1;
up.z = 0;
target.x = 0;
target.y = 0;
target.z = 9;
target.z = 0;
t_mat4 proj = mat4_perspective(45.0 * M_PI / 180.0, 800.0 / 600.0, 0.001, 1000);
t_mat4 proj = mat4_perspective(45.0 * M_PI / 180.0, 800.0 / 600.0, 0.001, 100);
t_mat4 view = mat4_inverse(mat4_lookat(eye, up, target));
// t_mat4 model = mat4_rotatey(mat4_rotatex(mat4_identity(), Scale), Scale / 1.3);
//t_mat4 model = mat4_rotatey(mat4_identity(), Scale);
@ -71,16 +73,19 @@ static void RenderSceneCB(t_env *e)
//glUniform1i(gCountLocation, index_count);
glActiveTexture(GL_TEXTURE0);
glUniform1i(gSamplerLocation, 0);
glUniform1i(has_texture_location, is_good);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), 0);
if (is_good) {
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), (void *)(3 * sizeof(double)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), (void *)(5 * sizeof(double)));
}
//glBindTexture(GL_TEXTURE_2D, texture);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
@ -99,17 +104,25 @@ static void CreateVertexBuffer(t_env *e)
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);
is_good = 1;
e->vbo_data = (double *)malloc(e->object.vertices_indices.ptr * 8 * sizeof(double));
for (size_t i = 0; i < e->object.vertices_indices.ptr * 8; i += 8) {
e->vbo_data[i] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3] * 0.3;
e->vbo_data[i + 1] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3 + 1] * 0.3;
e->vbo_data[i + 2] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3 + 2] * 0.3;
e->vbo_data[i + 3] = e->object.uvs.data[e->object.uvs_indices.data[i / 8] * 2];
e->vbo_data[i + 4] = e->object.uvs.data[e->object.uvs_indices.data[i / 8] * 2 + 1];
e->vbo_data[i + 5] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3];
e->vbo_data[i + 6] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3 + 1];
e->vbo_data[i + 7] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3 + 2];
e->vbo_data[i] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3];
e->vbo_data[i + 1] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3 + 1];
e->vbo_data[i + 2] = e->object.vertices.data[e->object.vertices_indices.data[i / 8] * 3 + 2];
if (e->object.uvs_indices.ptr == e->object.vertices_indices.ptr && e->object.uvs_indices.data[i / 8] * 2 + 1 < e->object.uvs.ptr) {
e->vbo_data[i + 3] = e->object.uvs.data[e->object.uvs_indices.data[i / 8] * 2];
e->vbo_data[i + 4] = e->object.uvs.data[e->object.uvs_indices.data[i / 8] * 2 + 1];
} else {
is_good = 0;
}
if (e->object.normals_indices.ptr == e->object.vertices_indices.ptr && e->object.normals_indices.data[i / 8] * 3 + 2 < e->object.normals.ptr) {
e->vbo_data[i + 5] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3];
e->vbo_data[i + 6] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3 + 1];
e->vbo_data[i + 7] = e->object.normals.data[e->object.normals_indices.data[i / 8] * 3 + 2];
} else {
is_good = 0;
}
}
glGenBuffers(1, &VBO);
@ -217,6 +230,7 @@ static void CompileShaders(t_env *e)
gViewLocation = glGetUniformLocation(e->shader_program, "view");
gModelLocation = glGetUniformLocation(e->shader_program, "model");
gSamplerLocation = glGetUniformLocation(e->shader_program, "ourTexture");
has_texture_location = glGetUniformLocation(e->shader_program, "has_texture");
// assert(gScaleLocation != 0xFFFFFFFF);
assert(gTimeLocation != 0xFFFFFFFF);
assert(gProjLocation != 0xFFFFFFFF);
@ -322,7 +336,7 @@ int run(t_env *e)
int tex_width, tex_height, nrChannels;
unsigned char *tex_data = stbi_load("resources/racer.jpg", &tex_width, &tex_height, &nrChannels, 0);
unsigned char *tex_data = stbi_load("resources/blue.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);

View File

@ -2,6 +2,7 @@
uniform float time;
uniform sampler2D ourTexture;
uniform int has_texture;
out vec4 FragColor;
flat in int vID;
@ -18,11 +19,15 @@ float rand(float n)
void main()
{
vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time))));
vec3 n = normalize(vec3(50. * cos(time), 50., 50. * sin(time)) - position);
//vec3 n = normalize(vec3(0., 50., 0.) - position);
vec3 n = normalize(vec3(10. * cos(time), 10., 10. * sin(time)) - position);
float phong = 0.2 + 0.8 * max(0., dot(n, normalize(normal)));
float specular = pow(max(dot(reflect(n, normalize(normal)), normalize(vec3(0, 0, -9) - position)), 0.), 10.);
FragColor = vec4(color * 0.001 + texture(ourTexture, texCoord).rgb * phong + vec3(1.) * specular, 1.);
//FragColor = vec4(color * 0.001 + vec3(0.5) * phong + vec3(1.) * specular, 1.);
//FragColor = vec4(color * 0.001 + normal, 1.);
float specular = pow(max(dot(reflect(n, normalize(normal)), normalize(vec3(0, 0, -2) - position)), 0.), 10.);
if (has_texture == 1) {
FragColor = vec4(texture(ourTexture, texCoord).rgb * phong + vec3(1.) * specular, 1.);
//FragColor = vec4(texCoord.xyx / 2. * phong + vec3(1.) * specular, 1.);
}
else {
FragColor = vec4(color, 1.);
}
//FragColor = vec4(color * 0.001 + normal, 1.);
}