first GL renders, need proj view model matrices
This commit is contained in:
parent
7d9758728b
commit
01a06b09a1
18
inc/scop.h
18
inc/scop.h
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/23 20:37:01 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/23 21:16:08 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
|||
# include <string.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
# include <math.h>
|
||||
|
||||
# include <GL/glew.h>
|
||||
# include <GL/freeglut.h>
|
||||
|
||||
|
@ -44,7 +46,7 @@ enum e_gl_buf_type
|
|||
typedef struct s_cam t_cam;
|
||||
typedef struct s_window t_window;
|
||||
typedef struct s_buf_d t_buf_d;
|
||||
typedef struct s_buf_i t_buf_i;
|
||||
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;
|
||||
|
@ -70,9 +72,9 @@ struct s_buf_d
|
|||
size_t len;
|
||||
};
|
||||
|
||||
struct s_buf_i
|
||||
struct s_buf_ui
|
||||
{
|
||||
int *data;
|
||||
unsigned int *data;
|
||||
size_t ptr;
|
||||
size_t len;
|
||||
};
|
||||
|
@ -82,7 +84,7 @@ struct s_obj
|
|||
t_buf_d vertices;
|
||||
t_buf_d uvs;
|
||||
t_buf_d normals;
|
||||
t_buf_i indices;
|
||||
t_buf_ui indices;
|
||||
};
|
||||
|
||||
struct s_env
|
||||
|
@ -100,14 +102,14 @@ 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_i *indices, int token_count);
|
||||
t_buf_ui *indices, int token_count);
|
||||
void parse_append_data_tmp(
|
||||
t_buf_i *indices, int vertex_count, int *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_i(t_buf_i *buffer, char *token);
|
||||
int parse_append_data_ui(t_buf_ui *buffer, char *token);
|
||||
|
||||
void free_env(t_env *e);
|
||||
void init_window(t_env *e);
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/22 18:56:21 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/22 20:15:14 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/23 21:14:28 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -40,7 +40,8 @@ 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 = (int *)malloc(sizeof(int) * BUFFER_SIZE);
|
||||
e->object.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;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/23 20:31:41 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/23 21:15:54 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -26,7 +26,7 @@ t_gl_buf_type parse_gl_buf_type(char *token)
|
|||
}
|
||||
|
||||
void parse_append_data_tmp(
|
||||
t_buf_i *indices, int vertex_count, int *data_tmp)
|
||||
t_buf_ui *indices, int vertex_count, unsigned int *data_tmp)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -35,21 +35,22 @@ void parse_append_data_tmp(
|
|||
{
|
||||
if (indices->ptr == indices->len)
|
||||
{
|
||||
indices->data = (int *)realloc(
|
||||
indices->data, sizeof(int) * (indices->len + BUFFER_SIZE));
|
||||
indices->data = (unsigned int *)realloc(
|
||||
indices->data, sizeof(unsigned int) * (indices->len + BUFFER_SIZE));
|
||||
indices->len += BUFFER_SIZE;
|
||||
}
|
||||
indices->data[indices->ptr++] = data_tmp[i++];
|
||||
}
|
||||
}
|
||||
|
||||
void parse_triangulate(t_buf_i *indices, int vertex_count)
|
||||
void parse_triangulate(t_buf_ui *indices, int vertex_count)
|
||||
{
|
||||
int *data_tmp;
|
||||
unsigned int *data_tmp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
data_tmp = (int *)malloc(sizeof(int) * 3 * (vertex_count - 2));
|
||||
data_tmp = (unsigned int *)malloc(
|
||||
sizeof(unsigned int) * 3 * (vertex_count - 2));
|
||||
indices->ptr -= vertex_count;
|
||||
while (i < 3 * (vertex_count - 2))
|
||||
{
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/23 17:33:33 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/23 17:38:10 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/23 21:23:38 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -25,15 +25,15 @@ int parse_append_data_d(t_buf_d *buffer, char *token)
|
|||
return (SUCCESS);
|
||||
}
|
||||
|
||||
int parse_append_data_i(t_buf_i *buffer, char *token)
|
||||
int parse_append_data_ui(t_buf_ui *buffer, char *token)
|
||||
{
|
||||
if (buffer->ptr == buffer->len)
|
||||
{
|
||||
buffer->data = (int *)realloc(
|
||||
buffer->data, sizeof(int) * (buffer->len + BUFFER_SIZE));
|
||||
buffer->data = (unsigned int *)realloc(
|
||||
buffer->data, sizeof(unsigned int) * (buffer->len + BUFFER_SIZE));
|
||||
buffer->len += BUFFER_SIZE;
|
||||
}
|
||||
buffer->data[buffer->ptr] = atoi(token);
|
||||
buffer->data[buffer->ptr] = atoi(token) - 1;
|
||||
buffer->ptr++;
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
@ -54,6 +54,6 @@ 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_i(&(e->object.indices), token);
|
||||
ret = parse_append_data_ui(&(e->object.indices), token);
|
||||
return (ret);
|
||||
}
|
||||
|
|
168
src/run.c
168
src/run.c
|
@ -6,13 +6,177 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/23 20:28:50 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/23 21:25:26 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "scop.h"
|
||||
|
||||
|
||||
GLuint VBO;
|
||||
GLuint IBO;
|
||||
GLuint gScaleLocation;
|
||||
|
||||
const char* pVSFileName = "src/shader.vs";
|
||||
const char* pFSFileName = "src/shader.fs";
|
||||
|
||||
static void RenderSceneCB()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
static float Scale = 0.0f;
|
||||
|
||||
Scale += 0.01f;
|
||||
|
||||
glUniform1f(gScaleLocation, sinf(Scale));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||
// first GL_POINT or GL_TRIANGLES
|
||||
// second start of draw
|
||||
// third number of vertices to draw
|
||||
glDrawElements(GL_TRIANGLES, 228, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glDisableVertexAttribArray(0);
|
||||
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
static void InitializeGlutCallbacks(void)
|
||||
{
|
||||
glutDisplayFunc(RenderSceneCB);
|
||||
glutIdleFunc(RenderSceneCB);
|
||||
}
|
||||
|
||||
static void CreateVertexBuffer(t_env *e)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
|
||||
{
|
||||
GLuint ShaderObj = glCreateShader(ShaderType);
|
||||
|
||||
if (ShaderObj == 0)
|
||||
{
|
||||
fprintf(stderr, "Error creating shader type %d\n", ShaderType);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
const GLchar* p[1];
|
||||
p[0] = pShaderText;
|
||||
GLint Lengths[1];
|
||||
Lengths[0] = strlen(pShaderText);
|
||||
glShaderSource(ShaderObj, 1, p, Lengths);
|
||||
glCompileShader(ShaderObj);
|
||||
GLint success;
|
||||
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
GLchar InfoLog[1024];
|
||||
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
|
||||
fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
glAttachShader(ShaderProgram, ShaderObj);
|
||||
}
|
||||
|
||||
static void CompileShaders()
|
||||
{
|
||||
GLuint ShaderProgram = glCreateProgram();
|
||||
|
||||
if (ShaderProgram == 0)
|
||||
{
|
||||
fprintf(stderr, "Error creating shader program\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *vs = (char *)malloc(BUFFER_SIZE * sizeof(char));
|
||||
char *fs = (char *)malloc(BUFFER_SIZE * sizeof(char));
|
||||
char *buffer = (char *)malloc(BUFFER_SIZE * sizeof(char));
|
||||
FILE *fp;
|
||||
|
||||
vs[0] = '\0';
|
||||
fs[0] = '\0';
|
||||
fp = fopen(pVSFileName, "r");
|
||||
while (fgets(buffer, 4096, fp))
|
||||
{
|
||||
buffer[strcspn(buffer, "\n") + 1] = '\0';
|
||||
strcat(vs, buffer);
|
||||
}
|
||||
fclose(fp);
|
||||
fp = fopen(pFSFileName, "r");
|
||||
while (fgets(buffer, 4096, fp))
|
||||
{
|
||||
buffer[strcspn(buffer, "\n") + 1] = '\0';
|
||||
strcat(fs, buffer);
|
||||
}
|
||||
fclose(fp);
|
||||
printf("%s", vs);
|
||||
printf("%s", fs);
|
||||
AddShader(ShaderProgram, vs, GL_VERTEX_SHADER);
|
||||
AddShader(ShaderProgram, fs, GL_FRAGMENT_SHADER);
|
||||
|
||||
GLint Success = 0;
|
||||
GLchar ErrorLog[1024] = { 0 };
|
||||
|
||||
glLinkProgram(ShaderProgram);
|
||||
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
|
||||
if (Success == 0)
|
||||
{
|
||||
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
|
||||
fprintf(stderr, "Error linking shader program : '%s'\n", ErrorLog);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
glValidateProgram(ShaderProgram);
|
||||
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
|
||||
if (!Success)
|
||||
{
|
||||
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
|
||||
fprintf(stderr, "Invalid shader program '%s'\n", ErrorLog);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
glUseProgram(ShaderProgram);
|
||||
|
||||
gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale");
|
||||
// assert(gScaleLocation != 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
void run(t_env *e)
|
||||
{
|
||||
(void)e;
|
||||
int argc = 1;
|
||||
char *argv[1];
|
||||
|
||||
argv[0] = strdup("scop");
|
||||
glutInit(&argc, argv);
|
||||
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
|
||||
glutInitWindowSize(e->window.width, e->window.height);
|
||||
glutInitWindowPosition(100, 100);
|
||||
glutCreateWindow("scop");
|
||||
|
||||
printf("%ld", e->object.indices.ptr);
|
||||
InitializeGlutCallbacks();
|
||||
// Must be done after glut is initialized !
|
||||
GLenum res = glewInit();
|
||||
if (res != GLEW_OK)
|
||||
{
|
||||
fprintf(stderr, "Error : '%s'\n", glewGetErrorString(res));
|
||||
exit (1);
|
||||
}
|
||||
printf("GL version: %s\n", glGetString(GL_VERSION));
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
CreateVertexBuffer(e);
|
||||
CompileShaders();
|
||||
glutMainLoop();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#version 330
|
||||
|
||||
out vec4 FragColor;
|
||||
flat in int vID;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(float(vID) / 42.0, float(vID) / 42.0, float(vID) / 42.0, 1.0);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#version 330
|
||||
|
||||
layout (location = 0) in vec3 Position;
|
||||
|
||||
uniform float gScale;
|
||||
|
||||
flat out int vID;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(gScale * Position.x,gScale * Position.y, gScale * Position.z, 1.0);
|
||||
vID = gl_VertexID;
|
||||
}
|
Loading…
Reference in New Issue