opengl_learn

Step-by-step introduction to OpenGL
git clone https://0xdd.org/code/opengl_learn.git
Log | Files | Refs | README | LICENSE

commit 6d0b1f454360a1c91091f453598e702af56de31d
parent 6455b97b23ccf06f3dd8d4f42fd5e6cf83bda2e9
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date:   Wed, 27 Jun 2018 07:56:15 -0400

03: renamed shader variables to be more clear

Diffstat:
M03-texture.c | 60+++++++++++++++++++++++++++++++-----------------------------
1 file changed, 31 insertions(+), 29 deletions(-)

diff --git a/03-texture.c b/03-texture.c @@ -25,37 +25,39 @@ int main() { GLFWwindow * window = ogl_init(400, 240, "03 - texture"); + /* vu: vertex uniform, va: vertex attribute, fu: fragment uniform, fv: fragment varying */ const char * program_vertex_source = "#version 100" "\n" "precision highp float;" "\n" - "uniform mat4 uniform_MVP;" "\n" /* uniforms are inputs to vertex shaders that don't change per-vertex */ - "attribute vec3 attribute_position;" "\n" /* attributes are inputs to vertex shaders */ - "attribute vec3 attribute_color;" "\n" - "attribute vec2 attribute_UV;" "\n" - "varying vec4 varying_color;" "\n" /* varyings are outputs of vertex shaders and inputs to fragment shaders */ - "varying vec2 varying_UV;" "\n" + "uniform mat4 vu_vertex_MVP;" "\n" /* uniforms are inputs to vertex and fragment shaders that don't change per-vertex */ + "attribute vec3 va_vertex_position;" "\n" /* attributes are inputs to vertex shaders */ + "attribute vec3 va_vertex_color;" "\n" + "attribute vec2 va_texture_UV;" "\n" + "varying vec4 fv_vertex_color;" "\n" /* varyings are outputs of vertex shaders and inputs to fragment shaders */ + "varying vec2 fv_texture_UV;" "\n" "void main(){" "\n" - " gl_Position = uniform_MVP * vec4(attribute_position,1);" "\n" - " varying_color = vec4(attribute_color,1);" "\n" - " varying_UV = attribute_UV;" "\n" + " gl_Position = vu_vertex_MVP * vec4(va_vertex_position,1);" "\n" + "\n" + " fv_vertex_color = vec4(va_vertex_color,1);" "\n" + " fv_texture_UV = va_texture_UV;" "\n" "}" "\n" ; const char * program_fragment_source = "#version 100" "\n" "precision lowp float;" "\n" - "uniform sampler2D uniform_texturesampler;" "\n" - "varying vec4 varying_color;" "\n" - "varying vec2 varying_UV;" "\n" + "uniform sampler2D fu_texture_sampler;" "\n" + "varying vec2 fv_texture_UV;" "\n" + "varying vec4 fv_vertex_color;" "\n" "void main() {" "\n" - " vec4 texturesampler_color = texture2D(uniform_texturesampler, varying_UV);" "\n" - " gl_FragColor = mix(texturesampler_color, varying_color, 1.0-texturesampler_color.a);" "\n" + " vec4 texel = texture2D(fu_texture_sampler, fv_texture_UV);" "\n" + " gl_FragColor = mix(texel, fv_vertex_color, 1.0-texel.a);" "\n" "}" "\n" ; GLuint program_ID = ogl_program_build(program_vertex_source, program_fragment_source); - GLint program_uniform_texturesampler_ID = glGetUniformLocation(program_ID, "uniform_texturesampler"); - GLint program_attribute_position_ID = glGetAttribLocation( program_ID, "attribute_position"); - GLint program_attribute_color_ID = glGetAttribLocation( program_ID, "attribute_color"); - GLint program_attribute_UV_ID = glGetAttribLocation( program_ID, "attribute_UV"); + GLint program_va_vertex_position_ID = glGetAttribLocation( program_ID, "va_vertex_position"); + GLint program_va_vertex_color_ID = glGetAttribLocation( program_ID, "va_vertex_color"); + GLint program_va_texture_UV_ID = glGetAttribLocation( program_ID, "va_texture_UV"); + GLint program_fu_texture_sampler_ID = glGetUniformLocation(program_ID, "fu_texture_sampler"); struct ogl_mat4f MVP_projection; ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection); @@ -147,29 +149,29 @@ main() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(program_ID); - ogl_program_uniform_set_mat4f(program_ID, "uniform_MVP", MVP); + ogl_program_uniform_set_mat4f(program_ID, "vu_vertex_MVP", MVP); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, cube_texture_ID); - glUniform1i(program_uniform_texturesampler_ID, 0); + glUniform1i(program_fu_texture_sampler_ID, 0); - glEnableVertexAttribArray(program_attribute_position_ID); + glEnableVertexAttribArray(program_va_vertex_position_ID); glBindBuffer(GL_ARRAY_BUFFER, cube_vertexbuffer_ID); - glVertexAttribPointer(program_attribute_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); + glVertexAttribPointer(program_va_vertex_position_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); - glEnableVertexAttribArray(program_attribute_UV_ID); + glEnableVertexAttribArray(program_va_texture_UV_ID); glBindBuffer(GL_ARRAY_BUFFER, cube_texture_UVbuffer_ID); - glVertexAttribPointer(program_attribute_UV_ID, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); + glVertexAttribPointer(program_va_texture_UV_ID, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); - glEnableVertexAttribArray(program_attribute_color_ID); + glEnableVertexAttribArray(program_va_vertex_color_ID); glBindBuffer(GL_ARRAY_BUFFER, cube_colorbuffer_ID); - glVertexAttribPointer(program_attribute_color_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); + glVertexAttribPointer(program_va_vertex_color_ID, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0); glDrawArrays(GL_TRIANGLES, 0, cube_vertexbuffer_data_vertexes); glBindBuffer(GL_ARRAY_BUFFER, 0); - glDisableVertexAttribArray(program_attribute_position_ID); - glDisableVertexAttribArray(program_attribute_UV_ID); - //glDisableVertexAttribArray(program_attribute_color_ID); + glDisableVertexAttribArray(program_va_vertex_position_ID); + glDisableVertexAttribArray(program_va_texture_UV_ID); + //glDisableVertexAttribArray(program_va_vertex_color_ID); glfwSwapBuffers(window); glfwPollEvents();