Browse Source

move shader interface functions

main
cinnaboot 5 years ago
parent
commit
1aed361072
  1. 221
      src/shader.cpp

221
src/shader.cpp

@ -146,6 +146,90 @@ renderVAO(GLmesh* glmesh)
glBindVertexArray(0);
}
void
initTransforms(memory_arena* arena,
transforms* xforms,
gl_buffer* xform_ubo,
GLContext* gl_ctx,
float fov,
float near_clip_plane,
float aspect_ratio,
glm::vec3 cam_pos,
glm::vec3 look_pos)
{
xforms->view_xform = glm::lookAt(cam_pos, look_pos, glm::vec3(0, 1, 0));
xforms->proj_xform = glm::infinitePerspective(
glm::radians(fov), aspect_ratio, near_clip_plane);
glGenBuffers(1, &xform_ubo->id);
xform_ubo->target = GL_UNIFORM_BUFFER;
xform_ubo->data_type = GL_FLOAT;
xform_ubo->data_size = sizeof(*xforms);
xform_ubo->name = arenaCopyCStr(arena, "matrices");
glBindBuffer(xform_ubo->target, xform_ubo->id);
glBufferData(xform_ubo->target, sizeof(*xforms), xforms,
GL_DYNAMIC_DRAW);
// bindbufferbase
xform_ubo->binding_idx = gl_ctx->binding_count++;
glBindBufferBase(xform_ubo->target, xform_ubo->binding_idx, xform_ubo->id);
glBindBuffer(xform_ubo->target, 0);
}
GLmesh
loadGLMesh(shader_program* s,
const mesh& m,
GLenum draw_mode,
const glm::vec3& pos)
{
GLmesh gm = {0};
gm.num_indices = m.num_indices;
gm.draw_mode = draw_mode;
// NOTE: okay to store shader_program pointer on GLmesh here because we
// won't ever delete the shader
gm.shader = s;
glUseProgram(s->prog_id);
glGenVertexArrays(1, &gm.vao_id);
glBindVertexArray(gm.vao_id);
gm.model_xform = (glm::mat4*) std::calloc(1, sizeof(glm::mat4));
*gm.model_xform = glm::translate(glm::mat4(1), pos);
glUniformMatrix4fv(
s->model_xform_id, 1, GL_FALSE, (float*) gm.model_xform);
glGenBuffers(1, &gm.vert_buf_id);
glBindBuffer(GL_ARRAY_BUFFER, gm.vert_buf_id);
glBufferData(GL_ARRAY_BUFFER,
m.num_vertices * 3 * sizeof(GLfloat),
m.vertices,
GL_STATIC_DRAW);
glVertexAttribPointer(POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(POSITION);
if (m.normals != nullptr) {
glGenBuffers(1, &gm.norm_buf_id);
glBindBuffer(GL_ARRAY_BUFFER, gm.norm_buf_id);
glBufferData(GL_ARRAY_BUFFER,
m.num_vertices * 3 * sizeof(GLfloat),
m.normals,
GL_STATIC_DRAW);
glVertexAttribPointer(NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(NORMAL);
}
glGenBuffers(1, &gm.idx_buf_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gm.idx_buf_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
m.num_indices * sizeof(u16),
m.indices,
GL_STATIC_DRAW);
glBindVertexArray(0);
glUseProgram(0);
return gm;
}
// NOTE: internal
@ -236,6 +320,32 @@ parseShaderUniforms(memory_arena* arena, shader_program* s, GLContext* gl_ctx)
return true;
}
void
initCTXSizes(GLContext* gl_ctx)
{
// NOTE: see https://docs.gl/gl3/glGet for other useful context info
if (gl_ctx->max_binding_points == 0) {
glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS,
&gl_ctx->max_binding_points);
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_BLOCKS, &gl_ctx->max_vertex_blocks);
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS,
&gl_ctx->max_fragment_blocks);
glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &gl_ctx->max_ublock_size);
#if 1
printf("%s(), context size info set\n", __FUNCTION__);
printf("GL_MAX_UNIFORM_BUFFER_BINDINGS: %d\n",
gl_ctx->max_binding_points);
printf("GL_MAX_VERTEX_UNIFORM_BLOCKS: %d\n",
gl_ctx->max_vertex_blocks);
printf("GL_MAX_FRAGMENT_UNIFORM_BLOCKS: %d\n",
gl_ctx->max_fragment_blocks);
printf("GL_MAX_UNIFORM_BLOCK_SIZE: %d\n",
gl_ctx->max_ublock_size);
#endif
}
}
i32
ctxGetUniformBlockBinding(GLContext* gl_ctx, const char* name)
{
@ -302,114 +412,3 @@ parseAttributes(memory_arena* arena, shader_program* s, GLContext* gl_ctx)
return true;
}
void
initTransforms(memory_arena* arena,
transforms* xforms,
gl_buffer* xform_ubo,
GLContext* gl_ctx,
float fov,
float near_clip_plane,
float aspect_ratio,
glm::vec3 cam_pos,
glm::vec3 look_pos)
{
xforms->view_xform = glm::lookAt(cam_pos, look_pos, glm::vec3(0, 1, 0));
xforms->proj_xform = glm::infinitePerspective(
glm::radians(fov), aspect_ratio, near_clip_plane);
glGenBuffers(1, &xform_ubo->id);
xform_ubo->target = GL_UNIFORM_BUFFER;
xform_ubo->data_type = GL_FLOAT;
xform_ubo->data_size = sizeof(*xforms);
xform_ubo->name = arenaCopyCStr(arena, "matrices");
glBindBuffer(xform_ubo->target, xform_ubo->id);
glBufferData(xform_ubo->target, sizeof(*xforms), xforms,
GL_DYNAMIC_DRAW);
// bindbufferbase
xform_ubo->binding_idx = gl_ctx->binding_count++;
glBindBufferBase(xform_ubo->target, xform_ubo->binding_idx, xform_ubo->id);
glBindBuffer(xform_ubo->target, 0);
}
void
initCTXSizes(GLContext* gl_ctx)
{
// NOTE: see https://docs.gl/gl3/glGet for other useful context info
if (gl_ctx->max_binding_points == 0) {
glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS,
&gl_ctx->max_binding_points);
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_BLOCKS, &gl_ctx->max_vertex_blocks);
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS,
&gl_ctx->max_fragment_blocks);
glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &gl_ctx->max_ublock_size);
#if 1
printf("%s(), context size info set\n", __FUNCTION__);
printf("GL_MAX_UNIFORM_BUFFER_BINDINGS: %d\n",
gl_ctx->max_binding_points);
printf("GL_MAX_VERTEX_UNIFORM_BLOCKS: %d\n",
gl_ctx->max_vertex_blocks);
printf("GL_MAX_FRAGMENT_UNIFORM_BLOCKS: %d\n",
gl_ctx->max_fragment_blocks);
printf("GL_MAX_UNIFORM_BLOCK_SIZE: %d\n",
gl_ctx->max_ublock_size);
#endif
}
}
GLmesh
loadGLMesh(shader_program* s,
const mesh& m,
GLenum draw_mode,
const glm::vec3& pos)
{
GLmesh gm = {0};
gm.num_indices = m.num_indices;
gm.draw_mode = draw_mode;
// NOTE: okay to store shader_program pointer on GLmesh here because we
// won't ever delete the shader
gm.shader = s;
glUseProgram(s->prog_id);
glGenVertexArrays(1, &gm.vao_id);
glBindVertexArray(gm.vao_id);
gm.model_xform = (glm::mat4*) std::calloc(1, sizeof(glm::mat4));
*gm.model_xform = glm::translate(glm::mat4(1), pos);
glUniformMatrix4fv(
s->model_xform_id, 1, GL_FALSE, (float*) gm.model_xform);
glGenBuffers(1, &gm.vert_buf_id);
glBindBuffer(GL_ARRAY_BUFFER, gm.vert_buf_id);
glBufferData(GL_ARRAY_BUFFER,
m.num_vertices * 3 * sizeof(GLfloat),
m.vertices,
GL_STATIC_DRAW);
glVertexAttribPointer(POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(POSITION);
if (m.normals != nullptr) {
glGenBuffers(1, &gm.norm_buf_id);
glBindBuffer(GL_ARRAY_BUFFER, gm.norm_buf_id);
glBufferData(GL_ARRAY_BUFFER,
m.num_vertices * 3 * sizeof(GLfloat),
m.normals,
GL_STATIC_DRAW);
glVertexAttribPointer(NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(NORMAL);
}
glGenBuffers(1, &gm.idx_buf_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gm.idx_buf_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
m.num_indices * sizeof(u16),
m.indices,
GL_STATIC_DRAW);
glBindVertexArray(0);
glUseProgram(0);
return gm;
}

Loading…
Cancel
Save