Compare commits

...

2 Commits

Author SHA1 Message Date
gbrochar 71e157e76f feat(engine): normals and texture 2024-04-25 09:01:05 +02:00
gbrochar dadd6998cb chore: remove norm from headers 2024-04-24 20:40:56 +02:00
9 changed files with 186 additions and 221 deletions

View File

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mat4.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 20:35:00 by gbrochar #+# #+# */
/* Updated: 2020/12/26 09:16:18 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAT4_H
# define MAT4_H
@ -17,51 +5,37 @@
# include "vec3.h"
# include <math.h>
typedef struct s_mat4 t_mat4;
typedef struct s_persp_tool t_persp_tool;
typedef struct s_mat4 t_mat4;
typedef struct s_persp_tool t_persp_tool;
struct s_mat4
{
float data[16];
struct s_mat4 {
float data[16];
};
struct s_persp_tool
{
double xmin;
double xmax;
double ymin;
double ymax;
struct s_persp_tool {
double xmin;
double xmax;
double ymin;
double ymax;
};
t_mat4 mat4_transpose(t_mat4 m);
t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs);
t_mat4 mat4_perspective(
double fov, double aspect, double near, double far);
t_mat4 mat4_frustrum(
t_persp_tool tool, double near, double far);
t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool);
t_mat4 mat4_lookat(t_vec3 eye, t_vec3 up, t_vec3 target);
t_mat4 mat4_lookat_make_mat(
t_vec3 x, t_vec3 y, t_vec3 z, t_vec3 eye);
t_vec3 mat4_lookat_make_y(t_vec3 x, t_vec3 z);
t_vec3 mat4_lookat_make_x(t_vec3 z, t_vec3 up);
t_vec3 mat4_lookat_make_z(t_vec3 eye, t_vec3 target);
t_mat4 mat4_identity(void);
t_mat4 mat4_inverse(t_mat4 m);
t_mat4 mat4_rotatex(t_mat4 m, double theta);
t_mat4 mat4_rotatey(t_mat4 m, double theta);
t_mat4 mat4_rotatez(t_mat4 m, double theta);
t_mat4 mat4_rotatexyz(t_mat4 m, t_vec3 theta);
t_mat4 mat4_translate(t_mat4 m, t_vec3 t);
t_mat4 mat4_scale(t_mat4 m, t_vec3 s);
t_mat4 mat4_transpose(t_mat4 m);
t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs);
t_mat4 mat4_perspective(double fov, double aspect, double near, double far);
t_mat4 mat4_frustrum(t_persp_tool tool, double near, double far);
t_mat4 mat4_lookat(t_vec3 eye, t_vec3 up, t_vec3 target);
t_mat4 mat4_lookat_make_mat(t_vec3 x, t_vec3 y, t_vec3 z, t_vec3 eye);
t_vec3 mat4_lookat_make_y(t_vec3 x, t_vec3 z);
t_vec3 mat4_lookat_make_x(t_vec3 z, t_vec3 up);
t_vec3 mat4_lookat_make_z(t_vec3 eye, t_vec3 target);
t_mat4 mat4_identity(void);
t_mat4 mat4_inverse(t_mat4 m);
t_mat4 mat4_rotatex(t_mat4 m, double theta);
t_mat4 mat4_rotatey(t_mat4 m, double theta);
t_mat4 mat4_rotatez(t_mat4 m, double theta);
t_mat4 mat4_rotatexyz(t_mat4 m, t_vec3 theta);
t_mat4 mat4_translate(t_mat4 m, t_vec3 t);
t_mat4 mat4_scale(t_mat4 m, t_vec3 s);
#endif

View File

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* scop.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
/* Updated: 2020/12/26 09:16:30 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SCOP_H
# define SCOP_H
@ -34,10 +22,9 @@
# define EPSILON 0.000001
typedef enum e_gl_buf_type t_gl_buf_type;
typedef enum e_gl_buf_type t_gl_buf_type;
enum e_gl_buf_type
{
enum e_gl_buf_type {
VERTEX,
UV,
NORMAL,
@ -45,92 +32,83 @@ enum e_gl_buf_type
OTHER
};
typedef struct s_cam t_cam;
typedef struct s_window t_window;
typedef struct s_buf_d t_buf_d;
typedef struct s_buf_ui t_buf_ui;
typedef struct s_buf_s t_buf_s;
typedef struct s_obj t_obj;
typedef struct s_env t_env;
typedef struct s_cam t_cam;
typedef struct s_window t_window;
typedef struct s_buf_d t_buf_d;
typedef struct s_buf_ui t_buf_ui;
typedef struct s_buf_s t_buf_s;
typedef struct s_obj t_obj;
typedef struct s_env t_env;
struct s_cam
{
double fov;
double aspect;
double near;
double far;
struct s_cam {
double fov;
double aspect;
double near;
double far;
};
struct s_window
{
int width;
int height;
struct s_window {
int width;
int height;
};
struct s_buf_d
{
double *data;
size_t ptr;
size_t len;
struct s_buf_d {
double *data;
size_t ptr;
size_t len;
};
struct s_buf_ui
{
unsigned int *data;
size_t ptr;
size_t len;
struct s_buf_ui {
unsigned int *data;
size_t ptr;
size_t len;
};
struct s_obj
{
t_buf_d vertices;
t_buf_d uvs;
t_buf_d normals;
t_buf_ui vertices_indices;
t_buf_ui uvs_indices;
t_buf_ui normals_indices;
struct s_obj {
t_buf_d vertices;
t_buf_d uvs;
t_buf_d normals;
t_buf_ui vertices_indices;
t_buf_ui uvs_indices;
t_buf_ui normals_indices;
};
struct s_env
{
char *file_name;
t_cam camera;
t_window window;
t_obj object;
GLuint shader_program;
GLuint vertex_shader;
GLuint fragment_shader;
t_vec3 translate;
t_vec3 rotate;
t_vec3 scale;
struct s_env {
char *file_name;
t_cam camera;
t_window window;
t_obj object;
double *vbo_data;
GLuint shader_program;
GLuint vertex_shader;
GLuint fragment_shader;
t_vec3 translate;
t_vec3 rotate;
t_vec3 scale;
};
int parse(t_env *e, int argc, char **argv);
void set_params(t_env *e, int argc, char **argv);
int parse_file(t_env *e);
int parse(t_env *e, int argc, char **argv);
void set_params(t_env *e, int argc, char **argv);
int parse_file(t_env *e);
int parse_line(t_env *e, char *line);
t_gl_buf_type parse_gl_buf_type(char *token);
void parse_triangulate(
t_buf_ui *indices, int token_count);
void parse_append_data_tmp(
t_buf_ui *indices, int vertex_count, unsigned int *data_tmp);
int parse_line(t_env *e, char *line);
t_gl_buf_type parse_gl_buf_type(char *token);
void parse_triangulate(t_buf_ui *indices, int token_count);
void parse_append_data_tmp(t_buf_ui *indices, int vertex_count, unsigned int *data_tmp);
int parse_token(
t_env *e, char *token, t_gl_buf_type gl_buf_type);
int parse_append_data_d(t_buf_d *buffer, char *token);
int parse_append_data_ui(t_buf_ui *buffer, char *token);
int parse_token(t_env *e, char *token, t_gl_buf_type gl_buf_type);
int parse_append_data_d(t_buf_d *buffer, char *token);
int parse_append_data_ui(t_buf_ui *buffer, char *token);
void center_vertices(t_buf_d *vertices);
void center_vertices(t_buf_d *vertices);
void free_env(t_env *e);
void init_window(t_env *e);
void init_camera(t_env *e);
void init_object(t_env *e);
void init_env(t_env *e);
void free_env(t_env *e);
void init_window(t_env *e);
void init_camera(t_env *e);
void init_object(t_env *e);
void init_env(t_env *e);
int run(t_env *e);
int run(t_env *e);
#endif

View File

@ -1,25 +1,13 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec3.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/23 16:14:13 by gbrochar #+# #+# */
/* Updated: 2020/12/23 16:14:28 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VEC3_H
# define VEC3_H
typedef struct s_vec3 t_vec3;
typedef struct s_vec3 t_vec3;
struct s_vec3
{
double x;
double y;
double z;
struct s_vec3 {
double x;
double y;
double z;
};
#endif

View File

@ -1,26 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec4.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/23 15:57:23 by gbrochar #+# #+# */
/* Updated: 2020/12/23 16:00:12 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VEC4_H
# define VEC4_H
typedef struct s_vec4 t_vec4;
typedef struct s_vec4 t_vec4;
struct s_vec4
struct s_vec4
{
double x;
double y;
double z;
double w;
double x;
double y;
double z;
double w;
};
#endif

BIN
resources/racer.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

View File

@ -13,34 +13,12 @@
#include "mat4.h"
#include "vec4.h"
t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool)
{
t_mat4 ret;
ret.data[0] = x;
ret.data[4] = 0;
ret.data[8] = tool.x;
ret.data[12] = 0;
ret.data[1] = 0;
ret.data[5] = y;
ret.data[9] = tool.y;
ret.data[13] = 0;
ret.data[2] = 0;
ret.data[6] = 0;
ret.data[10] = tool.z;
ret.data[14] = tool.w;
ret.data[3] = 0;
ret.data[7] = 0;
ret.data[11] = -1;
ret.data[15] = 0;
return (ret);
}
t_mat4 mat4_frustrum(t_persp_tool tool, double near, double far)
{
double x;
double y;
t_vec4 tool2;
t_mat4 frustrum;
x = (2.0 * near) / (tool.xmax - tool.xmin);
y = (2.0 * near) / (tool.ymax - tool.ymin);
@ -48,7 +26,23 @@ t_mat4 mat4_frustrum(t_persp_tool tool, double near, double far)
tool2.y = (tool.ymax + tool.ymin) / (tool.ymax - tool.ymin);
tool2.z = -(far + near) / (far - near);
tool2.w = (-2.0 * far * near) / (far - near);
return (mat4_frustrum_tmp(x, y, tool2));
frustrum.data[0] = x;
frustrum.data[4] = 0;
frustrum.data[8] = tool2.x;
frustrum.data[12] = 0;
frustrum.data[1] = 0;
frustrum.data[5] = y;
frustrum.data[9] = tool2.y;
frustrum.data[13] = 0;
frustrum.data[2] = 0;
frustrum.data[6] = 0;
frustrum.data[10] = tool2.z;
frustrum.data[14] = tool2.w;
frustrum.data[3] = 0;
frustrum.data[7] = 0;
frustrum.data[11] = -1;
frustrum.data[15] = 0;
return frustrum;
}
t_mat4 mat4_perspective(
@ -60,5 +54,6 @@ t_mat4 mat4_perspective(
tool.ymin = -tool.ymax;
tool.xmin = tool.ymin * aspect;
tool.xmax = tool.ymax * aspect;
return (mat4_frustrum(tool, near, far));
return mat4_frustrum(tool, near, far);
}

View File

@ -18,13 +18,14 @@
#include "stb_image.h"
GLuint VBO;
GLuint IBO;
//GLuint IBO;
GLuint gScaleLocation;
GLuint gScaleIntLocation;
GLuint gTimeLocation;
GLuint gProjLocation;
GLuint gViewLocation;
GLuint gModelLocation;
GLuint gCountLocation;
GLuint has_texture_location;
//GLuint gCountLocation;
GLuint index_count;
GLuint gSamplerLocation;
@ -38,9 +39,9 @@ static void RenderSceneCB(t_env *e)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
static float Scale = 0.0f;
static float time = 0.0f;
Scale += 0.01f;
time += 0.01f;
t_vec3 eye;
t_vec3 up;
@ -63,25 +64,28 @@ static void RenderSceneCB(t_env *e)
t_mat4 model = mat4_scale(
mat4_rotatexyz(
mat4_translate(mat4_identity(), e->translate), e->rotate), e->scale);
glUniform1f(gScaleIntLocation, Scale);
glUniform1f(gTimeLocation, time);
glUniformMatrix4fv(gProjLocation, 1, GL_FALSE, (GLfloat *)proj.data);
glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.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);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, 0);
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 8 * sizeof(double), 0);
//layout text
//glVertexAttribPointer(2, 2, GL_DOUBLE, GL_FALSE, 0, 0);
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);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0);
//glDrawElements(GL_TRIANGLES, index_count * 3, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, index_count);
glDisableVertexAttribArray(0);
}
@ -95,12 +99,30 @@ 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);
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];
}
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, e->object.vertices_indices.ptr * 8 * sizeof(double), e->vbo_data, GL_STATIC_DRAW);
printf("Buffer created with success !\n");
/* 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.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)
@ -190,13 +212,13 @@ static void CompileShaders(t_env *e)
glUseProgram(e->shader_program);
// gScaleLocation = glGetUniformLocation(e->shader_program, "gScale");
gScaleIntLocation = glGetUniformLocation(e->shader_program, "time");
gTimeLocation = glGetUniformLocation(e->shader_program, "time");
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(gTimeLocation != 0xFFFFFFFF);
assert(gProjLocation != 0xFFFFFFFF);
assert(gViewLocation != 0xFFFFFFFF);
assert(gModelLocation != 0xFFFFFFFF);
@ -243,17 +265,17 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, int m
if (key == GLFW_KEY_H && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->rotate.z -= 0.03;
if (key == GLFW_KEY_U && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->scale.x += 0.03;
e->scale.x *= 1.03;
if (key == GLFW_KEY_J && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->scale.x -= 0.03;
e->scale.x *= 0.97;
if (key == GLFW_KEY_I && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->scale.y += 0.03;
e->scale.y *= 1.03;
if (key == GLFW_KEY_K && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->scale.y -= 0.03;
e->scale.y *= 0.97;
if (key == GLFW_KEY_O && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->scale.z += 0.03;
e->scale.z *= 1.03;
if (key == GLFW_KEY_L && (action == GLFW_PRESS || action == GLFW_REPEAT))
e->scale.z -= 0.03;
e->scale.z *= 0.97;
(void)scancode;
(void)mods;
}
@ -300,7 +322,7 @@ int run(t_env *e)
int tex_width, tex_height, nrChannels;
unsigned char *tex_data = stbi_load("resources/fox.jpg", &tex_width, &tex_height, &nrChannels, 0);
unsigned char *tex_data = stbi_load("resources/racer.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

@ -5,6 +5,9 @@ uniform sampler2D ourTexture;
out vec4 FragColor;
flat in int vID;
in vec2 texCoord;
in vec3 position;
in vec3 normal;
float rand(float n)
@ -15,7 +18,11 @@ 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 * 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);
vec3 n = normalize(vec3(50. * cos(time), 50., 50. * sin(time)) - position);
//vec3 n = normalize(vec3(0., 50., 0.) - 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.);
}

View File

@ -1,6 +1,8 @@
#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 Uvs;
layout (location = 2) in vec3 Normal;
uniform float gScale;
uniform mat4 view;
@ -8,10 +10,20 @@ uniform mat4 proj;
uniform mat4 model;
flat out int vID;
out vec2 texCoord;
out vec3 position;
out vec3 normal;
void main()
{
gl_Position = proj * view * model * vec4(Position, 1.0);
vec4 test = proj * view * model * vec4(Position, 1.0);
gl_Position = test;
// gl_Position = vec4(gScale * Position.x,gScale * Position.y, gScale * Position.z, 1.0);
vID = gl_VertexID;
texCoord = Uvs;
mat4 normalMat = transpose(inverse(view * model));
position = Position;
normal = (normalMat * vec4(Normal, 1.)).xyz;
}