Rendering of a coordinate frame works

master
Martin Felis 2020-10-19 22:16:42 +02:00
parent b4729fada4
commit ce88204c02
4 changed files with 97 additions and 85 deletions

View File

@ -5,5 +5,5 @@ smooth in vec4 ioFragColor;
void main()
{
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
fragColor = ioFragColor;
}

View File

@ -15,5 +15,4 @@ void main()
{
ioFragColor = inColor;
gl_Position = (uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord);
}

View File

@ -55,8 +55,9 @@ typedef struct srmeshdata {
int nvertices;
int nindices;
GLuint vertex_buffer_id;
GLuint index_buffer_id;
GLuint vao_id;
GLuint vb_id;
GLuint idb_id;
} srmeshdata;
static srmeshdata gCoordFrameMesh = {0};
@ -70,7 +71,8 @@ typedef struct srshader {
GLuint frag_shader_id;
} 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) {
glShaderSource(shader_id, 1, &shader_src, 0);
@ -94,7 +96,10 @@ bool srshader_compile (GLuint shader_id, const char* shader_src) {
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);
if (!srshader_compile(shader->vert_shader_id, vert_src)) {
gLog("Error compiling vertex shader!");
@ -206,6 +211,49 @@ void init_shaders() {
void init_debug_meshes() {
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;
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, 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(
GL_ARRAY_BUFFER,
sizeof(coord_frame_vertices),
@ -228,14 +274,16 @@ void init_debug_meshes() {
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &gCoordFrameMesh.index_buffer_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.index_buffer_id);
glGenBuffers(1, &gCoordFrameMesh.idb_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id);
glBufferData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(coord_frame_indices),
coord_frame_indices,
GL_STATIC_DRAW);
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) {
glBindBuffer(GL_ARRAY_BUFFER, gCoordFrameMesh.vertex_buffer_id);
glBindVertexArray(gCoordFrameMesh.vao_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)
);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.index_buffer_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id);
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) {
@ -423,15 +433,18 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(gDefaultShader.program_id);
GLint view_mat_loc = glGetUniformLocation(gDefaultShader.program_id, "uViewMatrix");
GLint view_mat_loc =
glGetUniformLocation(gDefaultShader.program_id, "uViewMatrix");
assert(view_mat_loc != -1);
glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, &sview->view);
GLint proj_mat_loc = glGetUniformLocation(gDefaultShader.program_id, "uProjectionMatrix");
GLint proj_mat_loc =
glGetUniformLocation(gDefaultShader.program_id, "uProjectionMatrix");
assert(proj_mat_loc != -1);
glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, &sview->proj);
GLint model_mat_loc = glGetUniformLocation(gDefaultShader.program_id, "uModelMatrix");
GLint model_mat_loc =
glGetUniformLocation(gDefaultShader.program_id, "uModelMatrix");
assert(model_mat_loc != -1);
for (int i = 0; i < scmdbuf->ncmds; i++) {

View File

@ -164,7 +164,7 @@ void DoRender() {
srview_set_size(gView, content_avail.x, content_avail.y);
simd4x4f view;
simd4x4f proj;
simd4x4f_translation(&view, 0.1f, 0.1f, -0.1f);
simd4x4f_translation(&view, 0.1f, 0.1f, -0.5f);
simd4x4f_lookat(
&view,
simd4f_create(1.f, 1.f, 1.f, 1.f),