Rendering of a coordinate frame works
parent
b4729fada4
commit
ce88204c02
|
@ -5,5 +5,5 @@ smooth in vec4 ioFragColor;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
fragColor = ioFragColor;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,5 +15,4 @@ void main()
|
||||||
{
|
{
|
||||||
ioFragColor = inColor;
|
ioFragColor = inColor;
|
||||||
gl_Position = (uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord);
|
gl_Position = (uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord);
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
175
src/srender.c
175
src/srender.c
|
@ -55,8 +55,9 @@ typedef struct srmeshdata {
|
||||||
int nvertices;
|
int nvertices;
|
||||||
int nindices;
|
int nindices;
|
||||||
|
|
||||||
GLuint vertex_buffer_id;
|
GLuint vao_id;
|
||||||
GLuint index_buffer_id;
|
GLuint vb_id;
|
||||||
|
GLuint idb_id;
|
||||||
} srmeshdata;
|
} srmeshdata;
|
||||||
|
|
||||||
static srmeshdata gCoordFrameMesh = {0};
|
static srmeshdata gCoordFrameMesh = {0};
|
||||||
|
@ -70,23 +71,24 @@ typedef struct srshader {
|
||||||
GLuint frag_shader_id;
|
GLuint frag_shader_id;
|
||||||
} srshader;
|
} srshader;
|
||||||
|
|
||||||
static srshader gDefaultShader = {"default_vert.glsl", "default_frag.glsl", 0, 0, 0};
|
static srshader gDefaultShader =
|
||||||
|
{"default_vert.glsl", "default_frag.glsl", 0, 0, 0};
|
||||||
|
|
||||||
bool srshader_compile (GLuint shader_id, const char* shader_src) {
|
bool srshader_compile(GLuint shader_id, const char* shader_src) {
|
||||||
glShaderSource (shader_id, 1, &shader_src, 0);
|
glShaderSource(shader_id, 1, &shader_src, 0);
|
||||||
glCompileShader (shader_id);
|
glCompileShader(shader_id);
|
||||||
|
|
||||||
// Check Vertex Shader
|
// Check Vertex Shader
|
||||||
GLint result = GL_FALSE;
|
GLint result = GL_FALSE;
|
||||||
int log_length = 0;
|
int log_length = 0;
|
||||||
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
|
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
|
||||||
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
|
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||||
if ( log_length > 0 ){
|
if (log_length > 0) {
|
||||||
char* err_msg = calloc (log_length, sizeof(char));
|
char* err_msg = calloc(log_length, sizeof(char));
|
||||||
assert (err_msg);
|
assert(err_msg);
|
||||||
glGetShaderInfoLog(shader_id, log_length, NULL, err_msg);
|
glGetShaderInfoLog(shader_id, log_length, NULL, err_msg);
|
||||||
gLog("%s", err_msg);
|
gLog("%s", err_msg);
|
||||||
free (err_msg);
|
free(err_msg);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -94,7 +96,10 @@ bool srshader_compile (GLuint shader_id, const char* shader_src) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool srshader_load (srshader* shader, const char* vert_src, const char* frag_src) {
|
bool srshader_load(
|
||||||
|
srshader* shader,
|
||||||
|
const char* vert_src,
|
||||||
|
const char* frag_src) {
|
||||||
shader->vert_shader_id = glCreateShader(GL_VERTEX_SHADER);
|
shader->vert_shader_id = glCreateShader(GL_VERTEX_SHADER);
|
||||||
if (!srshader_compile(shader->vert_shader_id, vert_src)) {
|
if (!srshader_compile(shader->vert_shader_id, vert_src)) {
|
||||||
gLog("Error compiling vertex shader!");
|
gLog("Error compiling vertex shader!");
|
||||||
|
@ -123,12 +128,12 @@ bool srshader_load (srshader* shader, const char* vert_src, const char* frag_src
|
||||||
int log_length = 0;
|
int log_length = 0;
|
||||||
glGetProgramiv(shader->program_id, GL_LINK_STATUS, &result);
|
glGetProgramiv(shader->program_id, GL_LINK_STATUS, &result);
|
||||||
glGetProgramiv(shader->program_id, GL_INFO_LOG_LENGTH, &log_length);
|
glGetProgramiv(shader->program_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||||
if ( log_length > 0 ){
|
if (log_length > 0) {
|
||||||
char* err_msg = calloc (log_length, sizeof(char));
|
char* err_msg = calloc(log_length, sizeof(char));
|
||||||
assert (err_msg);
|
assert(err_msg);
|
||||||
glGetProgramInfoLog(shader->program_id, log_length, NULL, err_msg);
|
glGetProgramInfoLog(shader->program_id, log_length, NULL, err_msg);
|
||||||
gLog("%s", err_msg);
|
gLog("%s", err_msg);
|
||||||
free (err_msg);
|
free(err_msg);
|
||||||
gLog("Error linking shader program!");
|
gLog("Error linking shader program!");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -137,12 +142,12 @@ bool srshader_load (srshader* shader, const char* vert_src, const char* frag_src
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool try_read_file (const char* dir, const char* filename, char** buf) {
|
bool try_read_file(const char* dir, const char* filename, char** buf) {
|
||||||
assert (*buf == NULL);
|
assert(*buf == NULL);
|
||||||
|
|
||||||
int path_len = strlen(dir) + strlen(filename) + 1;
|
int path_len = strlen(dir) + strlen(filename) + 1;
|
||||||
char* path_buffer = calloc (path_len, sizeof(char));
|
char* path_buffer = calloc(path_len, sizeof(char));
|
||||||
snprintf (path_buffer, path_len, "%s%s", dir, filename);
|
snprintf(path_buffer, path_len, "%s%s", dir, filename);
|
||||||
long file_length = 0;
|
long file_length = 0;
|
||||||
|
|
||||||
FILE* file = NULL;
|
FILE* file = NULL;
|
||||||
|
@ -152,8 +157,8 @@ bool try_read_file (const char* dir, const char* filename, char** buf) {
|
||||||
file_length = ftell(file);
|
file_length = ftell(file);
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
*buf = calloc(file_length, sizeof(char));
|
*buf = calloc(file_length, sizeof(char));
|
||||||
assert (*buf);
|
assert(*buf);
|
||||||
fread (*buf, 1, file_length, file);
|
fread(*buf, 1, file_length, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -179,18 +184,18 @@ void init_shaders() {
|
||||||
long length;
|
long length;
|
||||||
|
|
||||||
if (vert_buffer == NULL) {
|
if (vert_buffer == NULL) {
|
||||||
try_read_file (dir, gDefaultShader.vert_fname, &vert_buffer);
|
try_read_file(dir, gDefaultShader.vert_fname, &vert_buffer);
|
||||||
}
|
}
|
||||||
if (frag_buffer == NULL) {
|
if (frag_buffer == NULL) {
|
||||||
try_read_file (dir, gDefaultShader.frag_fname, &frag_buffer);
|
try_read_file(dir, gDefaultShader.frag_fname, &frag_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((vert_buffer != NULL) && (frag_buffer != NULL)) {
|
if ((vert_buffer != NULL) && (frag_buffer != NULL)) {
|
||||||
srshader_load (&gDefaultShader, vert_buffer, frag_buffer);
|
srshader_load(&gDefaultShader, vert_buffer, frag_buffer);
|
||||||
|
|
||||||
free (vert_buffer);
|
free(vert_buffer);
|
||||||
vert_buffer = NULL;
|
vert_buffer = NULL;
|
||||||
free (frag_buffer);
|
free(frag_buffer);
|
||||||
frag_buffer = NULL;
|
frag_buffer = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -199,13 +204,56 @@ void init_shaders() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gDefaultShader.program_id == 0) {
|
if (gDefaultShader.program_id == 0) {
|
||||||
gLog ("Error: could not load default shader!");
|
gLog("Error: could not load default shader!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_debug_meshes() {
|
void init_debug_meshes() {
|
||||||
assert(gCoordFrameMesh.nvertices == 0);
|
assert(gCoordFrameMesh.nvertices == 0);
|
||||||
|
|
||||||
|
glGenVertexArrays(1, &gCoordFrameMesh.vao_id);
|
||||||
|
glBindVertexArray(gCoordFrameMesh.vao_id);
|
||||||
|
|
||||||
|
glGenBuffers(1, &gCoordFrameMesh.vb_id);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, gCoordFrameMesh.vb_id);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
0,
|
||||||
|
4,
|
||||||
|
GL_FLOAT,
|
||||||
|
GL_FALSE,
|
||||||
|
(sizeof(srvrtxdata)),
|
||||||
|
(void*)0);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
1,
|
||||||
|
3,
|
||||||
|
GL_FLOAT,
|
||||||
|
GL_FALSE,
|
||||||
|
(sizeof(srvrtxdata)),
|
||||||
|
(void*)(sizeof(float) * 4));
|
||||||
|
|
||||||
|
// Attribute 2: texture coordinates
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
2,
|
||||||
|
2,
|
||||||
|
GL_FLOAT,
|
||||||
|
GL_FALSE,
|
||||||
|
(sizeof(srvrtxdata)),
|
||||||
|
(void*)(sizeof(float) * 7));
|
||||||
|
// Attribute 3: color
|
||||||
|
glEnableVertexAttribArray(3);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
GL_UNSIGNED_BYTE,
|
||||||
|
GL_TRUE,
|
||||||
|
(sizeof(srvrtxdata)),
|
||||||
|
(void*)(sizeof(float) * 9));
|
||||||
|
|
||||||
gCoordFrameMesh.nvertices = 6;
|
gCoordFrameMesh.nvertices = 6;
|
||||||
|
|
||||||
srvrtxdata coord_frame_vertices[] = {
|
srvrtxdata coord_frame_vertices[] = {
|
||||||
|
@ -217,10 +265,8 @@ void init_debug_meshes() {
|
||||||
|
|
||||||
{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255},
|
{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255},
|
||||||
{0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255}};
|
{0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255}};
|
||||||
GLuint coord_frame_indices[] = {0, 1, 1, 2, 3, 4};
|
GLuint coord_frame_indices[] = {0, 1, 2, 3, 4, 5};
|
||||||
|
|
||||||
glGenBuffers(1, &gCoordFrameMesh.vertex_buffer_id);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, gCoordFrameMesh.vertex_buffer_id);
|
|
||||||
glBufferData(
|
glBufferData(
|
||||||
GL_ARRAY_BUFFER,
|
GL_ARRAY_BUFFER,
|
||||||
sizeof(coord_frame_vertices),
|
sizeof(coord_frame_vertices),
|
||||||
|
@ -228,14 +274,16 @@ void init_debug_meshes() {
|
||||||
GL_STATIC_DRAW);
|
GL_STATIC_DRAW);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
glGenBuffers(1, &gCoordFrameMesh.index_buffer_id);
|
glGenBuffers(1, &gCoordFrameMesh.idb_id);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.index_buffer_id);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id);
|
||||||
glBufferData(
|
glBufferData(
|
||||||
GL_ELEMENT_ARRAY_BUFFER,
|
GL_ELEMENT_ARRAY_BUFFER,
|
||||||
sizeof(coord_frame_indices),
|
sizeof(coord_frame_indices),
|
||||||
coord_frame_indices,
|
coord_frame_indices,
|
||||||
GL_STATIC_DRAW);
|
GL_STATIC_DRAW);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -368,51 +416,13 @@ void srview_destroy(srview* sv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void srndr_run_frame_command(const srcmd* cmd) {
|
void srndr_run_frame_command(const srcmd* cmd) {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, gCoordFrameMesh.vertex_buffer_id);
|
glBindVertexArray(gCoordFrameMesh.vao_id);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id);
|
||||||
glVertexAttribPointer(
|
|
||||||
0,
|
|
||||||
4,
|
|
||||||
GL_FLOAT,
|
|
||||||
GL_FALSE,
|
|
||||||
(sizeof(srvrtxdata)),
|
|
||||||
(void*)0
|
|
||||||
);
|
|
||||||
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glVertexAttribPointer(
|
|
||||||
1,
|
|
||||||
3,
|
|
||||||
GL_FLOAT,
|
|
||||||
GL_FALSE,
|
|
||||||
(sizeof(srvrtxdata)),
|
|
||||||
(void*)(sizeof(float) * 4)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Attribute 2: texture coordinates
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
glVertexAttribPointer(
|
|
||||||
2,
|
|
||||||
2,
|
|
||||||
GL_FLOAT,
|
|
||||||
GL_FALSE,
|
|
||||||
(sizeof(srvrtxdata)),
|
|
||||||
(void*)(sizeof(float) * 7)
|
|
||||||
);
|
|
||||||
// Attribute 3: color
|
|
||||||
glEnableVertexAttribArray(3);
|
|
||||||
glVertexAttribPointer(
|
|
||||||
3,
|
|
||||||
4,
|
|
||||||
GL_UNSIGNED_BYTE,
|
|
||||||
GL_TRUE,
|
|
||||||
(sizeof(srvrtxdata)),
|
|
||||||
(void*)(sizeof(float) * 9)
|
|
||||||
);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.index_buffer_id);
|
|
||||||
glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, (void*)0);
|
glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, (void*)0);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
|
void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
|
||||||
|
@ -423,16 +433,19 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glUseProgram(gDefaultShader.program_id);
|
glUseProgram(gDefaultShader.program_id);
|
||||||
GLint view_mat_loc = glGetUniformLocation(gDefaultShader.program_id, "uViewMatrix");
|
GLint view_mat_loc =
|
||||||
assert (view_mat_loc != -1);
|
glGetUniformLocation(gDefaultShader.program_id, "uViewMatrix");
|
||||||
|
assert(view_mat_loc != -1);
|
||||||
glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, &sview->view);
|
glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, &sview->view);
|
||||||
|
|
||||||
GLint proj_mat_loc = glGetUniformLocation(gDefaultShader.program_id, "uProjectionMatrix");
|
GLint proj_mat_loc =
|
||||||
assert (proj_mat_loc != -1);
|
glGetUniformLocation(gDefaultShader.program_id, "uProjectionMatrix");
|
||||||
|
assert(proj_mat_loc != -1);
|
||||||
glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, &sview->proj);
|
glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, &sview->proj);
|
||||||
|
|
||||||
GLint model_mat_loc = glGetUniformLocation(gDefaultShader.program_id, "uModelMatrix");
|
GLint model_mat_loc =
|
||||||
assert (model_mat_loc != -1);
|
glGetUniformLocation(gDefaultShader.program_id, "uModelMatrix");
|
||||||
|
assert(model_mat_loc != -1);
|
||||||
|
|
||||||
for (int i = 0; i < scmdbuf->ncmds; i++) {
|
for (int i = 0; i < scmdbuf->ncmds; i++) {
|
||||||
const srcmd* cmd = &scmdbuf->cmds[i];
|
const srcmd* cmd = &scmdbuf->cmds[i];
|
||||||
|
|
|
@ -164,7 +164,7 @@ void DoRender() {
|
||||||
srview_set_size(gView, content_avail.x, content_avail.y);
|
srview_set_size(gView, content_avail.x, content_avail.y);
|
||||||
simd4x4f view;
|
simd4x4f view;
|
||||||
simd4x4f proj;
|
simd4x4f proj;
|
||||||
simd4x4f_translation(&view, 0.1f, 0.1f, -0.1f);
|
simd4x4f_translation(&view, 0.1f, 0.1f, -0.5f);
|
||||||
simd4x4f_lookat(
|
simd4x4f_lookat(
|
||||||
&view,
|
&view,
|
||||||
simd4f_create(1.f, 1.f, 1.f, 1.f),
|
simd4f_create(1.f, 1.f, 1.f, 1.f),
|
||||||
|
|
Loading…
Reference in New Issue