Compare commits
2 Commits
7bc350942d
...
71e157e76f
Author | SHA1 | Date |
---|---|---|
gbrochar | 71e157e76f | |
gbrochar | dadd6998cb |
78
inc/mat4.h
78
inc/mat4.h
|
@ -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
|
#ifndef MAT4_H
|
||||||
# define MAT4_H
|
# define MAT4_H
|
||||||
|
|
||||||
|
@ -17,51 +5,37 @@
|
||||||
# include "vec3.h"
|
# include "vec3.h"
|
||||||
# include <math.h>
|
# include <math.h>
|
||||||
|
|
||||||
typedef struct s_mat4 t_mat4;
|
typedef struct s_mat4 t_mat4;
|
||||||
typedef struct s_persp_tool t_persp_tool;
|
typedef struct s_persp_tool t_persp_tool;
|
||||||
|
|
||||||
struct s_mat4
|
struct s_mat4 {
|
||||||
{
|
float data[16];
|
||||||
float data[16];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_persp_tool
|
struct s_persp_tool {
|
||||||
{
|
double xmin;
|
||||||
double xmin;
|
double xmax;
|
||||||
double xmax;
|
double ymin;
|
||||||
double ymin;
|
double ymax;
|
||||||
double ymax;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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_perspective(double fov, double aspect, double near, double far);
|
||||||
|
t_mat4 mat4_frustrum(t_persp_tool tool, double near, double far);
|
||||||
t_mat4 mat4_perspective(
|
t_mat4 mat4_lookat(t_vec3 eye, t_vec3 up, t_vec3 target);
|
||||||
double fov, double aspect, double near, double far);
|
t_mat4 mat4_lookat_make_mat(t_vec3 x, t_vec3 y, t_vec3 z, t_vec3 eye);
|
||||||
t_mat4 mat4_frustrum(
|
t_vec3 mat4_lookat_make_y(t_vec3 x, t_vec3 z);
|
||||||
t_persp_tool tool, double near, double far);
|
t_vec3 mat4_lookat_make_x(t_vec3 z, t_vec3 up);
|
||||||
t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool);
|
t_vec3 mat4_lookat_make_z(t_vec3 eye, t_vec3 target);
|
||||||
|
t_mat4 mat4_identity(void);
|
||||||
t_mat4 mat4_lookat(t_vec3 eye, t_vec3 up, t_vec3 target);
|
t_mat4 mat4_inverse(t_mat4 m);
|
||||||
t_mat4 mat4_lookat_make_mat(
|
t_mat4 mat4_rotatex(t_mat4 m, double theta);
|
||||||
t_vec3 x, t_vec3 y, t_vec3 z, t_vec3 eye);
|
t_mat4 mat4_rotatey(t_mat4 m, double theta);
|
||||||
t_vec3 mat4_lookat_make_y(t_vec3 x, t_vec3 z);
|
t_mat4 mat4_rotatez(t_mat4 m, double theta);
|
||||||
t_vec3 mat4_lookat_make_x(t_vec3 z, t_vec3 up);
|
t_mat4 mat4_rotatexyz(t_mat4 m, t_vec3 theta);
|
||||||
t_vec3 mat4_lookat_make_z(t_vec3 eye, t_vec3 target);
|
t_mat4 mat4_translate(t_mat4 m, t_vec3 t);
|
||||||
|
t_mat4 mat4_scale(t_mat4 m, t_vec3 s);
|
||||||
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
|
#endif
|
||||||
|
|
||||||
|
|
144
inc/scop.h
144
inc/scop.h
|
@ -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
|
#ifndef SCOP_H
|
||||||
# define SCOP_H
|
# define SCOP_H
|
||||||
|
|
||||||
|
@ -34,10 +22,9 @@
|
||||||
|
|
||||||
# define EPSILON 0.000001
|
# 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,
|
VERTEX,
|
||||||
UV,
|
UV,
|
||||||
NORMAL,
|
NORMAL,
|
||||||
|
@ -45,92 +32,83 @@ enum e_gl_buf_type
|
||||||
OTHER
|
OTHER
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct s_cam t_cam;
|
typedef struct s_cam t_cam;
|
||||||
typedef struct s_window t_window;
|
typedef struct s_window t_window;
|
||||||
typedef struct s_buf_d t_buf_d;
|
typedef struct s_buf_d t_buf_d;
|
||||||
typedef struct s_buf_ui t_buf_ui;
|
typedef struct s_buf_ui t_buf_ui;
|
||||||
typedef struct s_buf_s t_buf_s;
|
typedef struct s_buf_s t_buf_s;
|
||||||
typedef struct s_obj t_obj;
|
typedef struct s_obj t_obj;
|
||||||
typedef struct s_env t_env;
|
typedef struct s_env t_env;
|
||||||
|
|
||||||
struct s_cam
|
struct s_cam {
|
||||||
{
|
double fov;
|
||||||
double fov;
|
double aspect;
|
||||||
double aspect;
|
double near;
|
||||||
double near;
|
double far;
|
||||||
double far;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_window
|
struct s_window {
|
||||||
{
|
int width;
|
||||||
int width;
|
int height;
|
||||||
int height;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_buf_d
|
struct s_buf_d {
|
||||||
{
|
double *data;
|
||||||
double *data;
|
size_t ptr;
|
||||||
size_t ptr;
|
size_t len;
|
||||||
size_t len;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_buf_ui
|
struct s_buf_ui {
|
||||||
{
|
unsigned int *data;
|
||||||
unsigned int *data;
|
size_t ptr;
|
||||||
size_t ptr;
|
size_t len;
|
||||||
size_t len;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_obj
|
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 vertices_indices;
|
||||||
t_buf_ui vertices_indices;
|
t_buf_ui uvs_indices;
|
||||||
t_buf_ui uvs_indices;
|
t_buf_ui normals_indices;
|
||||||
t_buf_ui normals_indices;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_env
|
struct s_env {
|
||||||
{
|
char *file_name;
|
||||||
char *file_name;
|
t_cam camera;
|
||||||
t_cam camera;
|
t_window window;
|
||||||
t_window window;
|
t_obj object;
|
||||||
t_obj object;
|
double *vbo_data;
|
||||||
GLuint shader_program;
|
GLuint shader_program;
|
||||||
GLuint vertex_shader;
|
GLuint vertex_shader;
|
||||||
GLuint fragment_shader;
|
GLuint fragment_shader;
|
||||||
t_vec3 translate;
|
t_vec3 translate;
|
||||||
t_vec3 rotate;
|
t_vec3 rotate;
|
||||||
t_vec3 scale;
|
t_vec3 scale;
|
||||||
};
|
};
|
||||||
|
|
||||||
int parse(t_env *e, int argc, char **argv);
|
int parse(t_env *e, int argc, char **argv);
|
||||||
void set_params(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_file(t_env *e);
|
||||||
|
|
||||||
int parse_line(t_env *e, char *line);
|
int parse_line(t_env *e, char *line);
|
||||||
t_gl_buf_type parse_gl_buf_type(char *token);
|
t_gl_buf_type parse_gl_buf_type(char *token);
|
||||||
void parse_triangulate(
|
void parse_triangulate(t_buf_ui *indices, int token_count);
|
||||||
t_buf_ui *indices, int token_count);
|
void parse_append_data_tmp(t_buf_ui *indices, int vertex_count, unsigned int *data_tmp);
|
||||||
void parse_append_data_tmp(
|
|
||||||
t_buf_ui *indices, int vertex_count, unsigned int *data_tmp);
|
|
||||||
|
|
||||||
int parse_token(
|
int parse_token(t_env *e, char *token, t_gl_buf_type gl_buf_type);
|
||||||
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_d(t_buf_d *buffer, char *token);
|
int parse_append_data_ui(t_buf_ui *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);
|
int run(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);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
24
inc/vec3.h
24
inc/vec3.h
|
@ -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
|
#ifndef VEC3_H
|
||||||
# define VEC3_H
|
# define VEC3_H
|
||||||
|
|
||||||
typedef struct s_vec3 t_vec3;
|
typedef struct s_vec3 t_vec3;
|
||||||
|
|
||||||
struct s_vec3
|
struct s_vec3 {
|
||||||
{
|
double x;
|
||||||
double x;
|
double y;
|
||||||
double y;
|
double z;
|
||||||
double z;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
25
inc/vec4.h
25
inc/vec4.h
|
@ -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
|
#ifndef VEC4_H
|
||||||
# define 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 x;
|
||||||
double y;
|
double y;
|
||||||
double z;
|
double z;
|
||||||
double w;
|
double w;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 204 KiB |
|
@ -13,34 +13,12 @@
|
||||||
#include "mat4.h"
|
#include "mat4.h"
|
||||||
#include "vec4.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)
|
t_mat4 mat4_frustrum(t_persp_tool tool, double near, double far)
|
||||||
{
|
{
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
t_vec4 tool2;
|
t_vec4 tool2;
|
||||||
|
t_mat4 frustrum;
|
||||||
|
|
||||||
x = (2.0 * near) / (tool.xmax - tool.xmin);
|
x = (2.0 * near) / (tool.xmax - tool.xmin);
|
||||||
y = (2.0 * near) / (tool.ymax - tool.ymin);
|
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.y = (tool.ymax + tool.ymin) / (tool.ymax - tool.ymin);
|
||||||
tool2.z = -(far + near) / (far - near);
|
tool2.z = -(far + near) / (far - near);
|
||||||
tool2.w = (-2.0 * 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(
|
t_mat4 mat4_perspective(
|
||||||
|
@ -60,5 +54,6 @@ t_mat4 mat4_perspective(
|
||||||
tool.ymin = -tool.ymax;
|
tool.ymin = -tool.ymax;
|
||||||
tool.xmin = tool.ymin * aspect;
|
tool.xmin = tool.ymin * aspect;
|
||||||
tool.xmax = tool.ymax * aspect;
|
tool.xmax = tool.ymax * aspect;
|
||||||
return (mat4_frustrum(tool, near, far));
|
return mat4_frustrum(tool, near, far);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
64
src/run.c
64
src/run.c
|
@ -18,13 +18,14 @@
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
|
||||||
GLuint VBO;
|
GLuint VBO;
|
||||||
GLuint IBO;
|
//GLuint IBO;
|
||||||
GLuint gScaleLocation;
|
GLuint gScaleLocation;
|
||||||
GLuint gScaleIntLocation;
|
GLuint gTimeLocation;
|
||||||
GLuint gProjLocation;
|
GLuint gProjLocation;
|
||||||
GLuint gViewLocation;
|
GLuint gViewLocation;
|
||||||
GLuint gModelLocation;
|
GLuint gModelLocation;
|
||||||
GLuint gCountLocation;
|
GLuint has_texture_location;
|
||||||
|
//GLuint gCountLocation;
|
||||||
GLuint index_count;
|
GLuint index_count;
|
||||||
|
|
||||||
GLuint gSamplerLocation;
|
GLuint gSamplerLocation;
|
||||||
|
@ -38,9 +39,9 @@ static void RenderSceneCB(t_env *e)
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
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 eye;
|
||||||
t_vec3 up;
|
t_vec3 up;
|
||||||
|
@ -63,25 +64,28 @@ static void RenderSceneCB(t_env *e)
|
||||||
t_mat4 model = mat4_scale(
|
t_mat4 model = mat4_scale(
|
||||||
mat4_rotatexyz(
|
mat4_rotatexyz(
|
||||||
mat4_translate(mat4_identity(), e->translate), e->rotate), e->scale);
|
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(gProjLocation, 1, GL_FALSE, (GLfloat *)proj.data);
|
||||||
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);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glUniform1i(gSamplerLocation, 0);
|
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, 8 * sizeof(double), 0);
|
||||||
|
|
||||||
//layout text
|
glEnableVertexAttribArray(1);
|
||||||
//glVertexAttribPointer(2, 2, GL_DOUBLE, GL_FALSE, 0, 0);
|
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);
|
//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);
|
glDisableVertexAttribArray(0);
|
||||||
}
|
}
|
||||||
|
@ -95,12 +99,30 @@ static void CreateVertexBuffer(t_env *e)
|
||||||
printf("vertices indices len %ld\n", e->object.vertices_indices.ptr);
|
printf("vertices indices len %ld\n", e->object.vertices_indices.ptr);
|
||||||
printf("uvs indices len %ld\n", e->object.uvs_indices.ptr);
|
printf("uvs indices len %ld\n", e->object.uvs_indices.ptr);
|
||||||
printf("normals indices len %ld\n", e->object.normals_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);
|
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);
|
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.vertices_indices.ptr * sizeof(int), e->object.vertices_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)
|
||||||
|
@ -190,13 +212,13 @@ static void CompileShaders(t_env *e)
|
||||||
glUseProgram(e->shader_program);
|
glUseProgram(e->shader_program);
|
||||||
|
|
||||||
// gScaleLocation = glGetUniformLocation(e->shader_program, "gScale");
|
// gScaleLocation = glGetUniformLocation(e->shader_program, "gScale");
|
||||||
gScaleIntLocation = glGetUniformLocation(e->shader_program, "time");
|
gTimeLocation = glGetUniformLocation(e->shader_program, "time");
|
||||||
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");
|
gSamplerLocation = glGetUniformLocation(e->shader_program, "ourTexture");
|
||||||
// assert(gScaleLocation != 0xFFFFFFFF);
|
// assert(gScaleLocation != 0xFFFFFFFF);
|
||||||
assert(gScaleIntLocation != 0xFFFFFFFF);
|
assert(gTimeLocation != 0xFFFFFFFF);
|
||||||
assert(gProjLocation != 0xFFFFFFFF);
|
assert(gProjLocation != 0xFFFFFFFF);
|
||||||
assert(gViewLocation != 0xFFFFFFFF);
|
assert(gViewLocation != 0xFFFFFFFF);
|
||||||
assert(gModelLocation != 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))
|
if (key == GLFW_KEY_H && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||||
e->rotate.z -= 0.03;
|
e->rotate.z -= 0.03;
|
||||||
if (key == GLFW_KEY_U && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
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))
|
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))
|
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))
|
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))
|
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))
|
if (key == GLFW_KEY_L && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||||
e->scale.z -= 0.03;
|
e->scale.z *= 0.97;
|
||||||
(void)scancode;
|
(void)scancode;
|
||||||
(void)mods;
|
(void)mods;
|
||||||
}
|
}
|
||||||
|
@ -300,7 +322,7 @@ int run(t_env *e)
|
||||||
|
|
||||||
int tex_width, tex_height, nrChannels;
|
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);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data);
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
stbi_image_free(tex_data);
|
stbi_image_free(tex_data);
|
||||||
|
|
|
@ -5,6 +5,9 @@ uniform sampler2D ourTexture;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
flat in int vID;
|
flat in int vID;
|
||||||
|
in vec2 texCoord;
|
||||||
|
in vec3 position;
|
||||||
|
in vec3 normal;
|
||||||
|
|
||||||
|
|
||||||
float rand(float n)
|
float rand(float n)
|
||||||
|
@ -15,7 +18,11 @@ 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 * 0.001 + texture(ourTexture, gl_FragCoord.xy / vec2(480., 360.)).rgb, 1.0);
|
vec3 n = normalize(vec3(50. * cos(time), 50., 50. * sin(time)) - position);
|
||||||
//FragColor = vec4(color * 0.1 + texture(ourTexture, gl_FragCoord.xy).rgb, 1.0);
|
//vec3 n = normalize(vec3(0., 50., 0.) - position);
|
||||||
//FragColor = vec4(color * 0.01 + texture2D(ourTexture, gl_FragCoord.xy / vec2(480., 360.)).rgb, 1.0);
|
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.);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
layout (location = 0) in vec3 Position;
|
layout (location = 0) in vec3 Position;
|
||||||
|
layout (location = 1) in vec2 Uvs;
|
||||||
|
layout (location = 2) in vec3 Normal;
|
||||||
|
|
||||||
uniform float gScale;
|
uniform float gScale;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
|
@ -8,10 +10,20 @@ uniform mat4 proj;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
|
||||||
flat out int vID;
|
flat out int vID;
|
||||||
|
out vec2 texCoord;
|
||||||
|
out vec3 position;
|
||||||
|
out vec3 normal;
|
||||||
|
|
||||||
void main()
|
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);
|
// gl_Position = vec4(gScale * Position.x,gScale * Position.y, gScale * Position.z, 1.0);
|
||||||
vID = gl_VertexID;
|
vID = gl_VertexID;
|
||||||
|
texCoord = Uvs;
|
||||||
|
|
||||||
|
mat4 normalMat = transpose(inverse(view * model));
|
||||||
|
|
||||||
|
position = Position;
|
||||||
|
normal = (normalMat * vec4(Normal, 1.)).xyz;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue