Changes between Version 16 and Version 17 of tutorial


Ignore:
Timestamp:
Jun 28, 2013, 9:20:20 AM (11 years ago)
Author:
leon
Comment:

Add pressure and temperature shaders

Legend:

Unmodified
Added
Removed
Modified
  • tutorial

    v16 v17  
    132132}}}
    133133 How can we add color to vertices? See [http://www.opengl.org/sdk/docs/man2/xhtml/glColorPointer.xml glColorPointer] and [http://www.opengl.org/sdk/docs/man2/xhtml/glEnableClientState.xml glEnableClientState].
    134  6. Change background to {{{glClearColor(0.9,1,1,1.0);}}}
     134 6. Change background to {{{glClearColor(0.9,1,1,1.0);}}} and suggest initial window in `main()`
     135  {{{
     136  #!c
     137  glutInitWindowSize(512, 512);
     138  glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-512)/2,
     139                         (glutGet(GLUT_SCREEN_HEIGHT)-512)/2);
     140  }}}
    135141 7. Add [http://www.opengl.org/resources/libraries/glut/spec3/node49.html keyboard event]
    136142  to quit the program when pressing ESCape key with keycode 27 by adding callback function
     
    143149   }
    144150  }}}
    145   and registering event within `main()` by `glutKeyboardFunc(keyboard);`
    146  
    147  
     151  and registering event within `main()` by `glutKeyboardFunc(keyboard);`. Some prefer `key == 'q'`, though.
    148152
    149153
     
    151155[[Image(OpenGL-pipeline.svg, 320px, right, title=OpenGL pipeline)]]
    152156We extend previous exercise with example that introduces OpenGL 3.x techniques:
    153  * OpenGL Shading Language where simple vertex and fragment shader are required.
    154  * Vertex Aray Objects (VAOs) stored in GPU
     157 * OpenGL Shading Language (GLSL 1.2) where simple vertex and fragment shader are required.
     158 * Vertex Aray Objects (VAOs) and vertex buffers (VBOs) stored in GPU.
    155159Create {{{triangle.c}}} and update {{{Makefile}}} with new target
    156160{{{
     
    257261      glGetShaderiv( v, GL_INFO_LOG_LENGTH, &len );
    258262      GLchar* log = malloc(sizeof(GLchar)*(len+1));
    259       printf("Shader compilation failed: %s\n", log);
     263      printf("Vertex Shader compilation failed: %s\n", log);
    260264      free(log);
    261265    }
     
    277281   }}}
    278282   Create some error to verify if it works.
    279   For general (core) OpenGL errors we can use the following utility at suspicious places.
    280   {{{
    281   #!c
    282      GLenum errCode;
    283      if ((errCode = glGetError()) != GL_NO_ERROR) {
    284         const GLubyte *errString = gluErrorString(errCode);
    285         fprintf (stderr, "OpenGL Error: %s\n", errString);
    286      }
    287   }}}
    288  2. Introduce vertex temperature with additional array
    289   {{{
    290   #!c
    291   GLfloat vertex_temperature[] = {0, 0.5, 1, 0.7, 0.2, 0.9};
    292   }}}
    293   and replace shaders with
     283  For general (core) OpenGL errors we can use the following `glcheck()` utility at suspicious places.
     284  {{{
     285  #!c
     286  #define glcheck() {GLenum s; if ((s=glGetError()) != GL_NO_ERROR) \
     287                  fprintf (stderr, "OpenGL Error: %s at line %d\n", \
     288                           gluErrorString(s),  __LINE__);}
     289  }}}
     290 2. Introduce vertex temperature with additional array and buffer at the end off `init()`
     291  [[Image(temperature.png,right)]]
     292  {{{
     293  #!c
     294  GLfloat vertex_temperature[] = {0, 1, 0.2, 0.1, 0.5, 0.9};
     295  glBindBuffer(GL_ARRAY_BUFFER, Buffers[TempBuffer]);
     296  glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_temperature),
     297               vertex_temperature, GL_STATIC_DRAW);
     298  glVertexAttribPointer(tPosition, 1, GL_FLOAT, GL_FALSE, 0, NULL);
     299  glEnableVertexAttribArray(tPosition);//toggle this
     300  }}}
     301  and adding corresponding IDs to `VAOs` and buffers. Replace shaders with
    294302  {{{
    295303  #!c
    296304static const GLchar * vertex_shader[] = {
    297 "attribute float VertexTemp;" // receive this custom attribute along with vertex position
    298 "varying float Temperature;"  // communicate between the vertex and the fragment shader
    299 "void main() { gl_position  = gl_ModelViewProjectionMatrix * gl_Vertex; }"
     305  ""
     306  "attribute float temperature;" // custom variable along with vertex position
     307  "varying float  t;" // communicate between the vertex and the fragment shader
     308  "void main()"
     309  "{"
     310  "  t = temperature;"
     311  "  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
     312  "}"
    300313};
    301314static const GLchar * fragment_shader[] = {
    302 "vec3 Cool = vec3(0, 0, 1);" // Red
    303 "vec3 Hot  = vec3(1, 0, 1);" // Blue
    304 "void main()
    305 "{"
    306 "  vec3 color = mix(Cool, Hot, Temperature);" // use the built-in mix() function
    307 "  gl_FragColor = vec4(color, 1.0);"          // append alpha channel
    308 "}"
    309 };
    310   }}}
     315  "vec3 Cool = vec3(0, 0, 1);" // Red
     316  "vec3 Hot  = vec3(1, 0, 0);" // Blue
     317  "varying float t;" // Interpolated by fragment
     318  "void main()"
     319  "{"
     320  "  vec3 color = mix(Cool, Hot, t);"  // use the built-in mix() function
     321  "  gl_FragColor = vec4(color, 1.0);" // append alpha channel
     322  "}"
     323};
     324  }}}
     325  What happens if we don't enable temperature vertex array? Confer [attachment:temperature.c] attached if having troubles.
     326 3. Add additional custom vertex array for the pressure. Change the temperature array to have values in Celsius for water boiling range [0-100]°C. Pressure should be in the range of [0-1] MPa. Scaling to color range [0-1] should be done in shaders. Toggle between them with the keyboard event by using the keys `'p'` and `'t`' that {{{glEnableVertexAttribArray()}}} and {{{glDisableVertexAttribArray()}}} corresponding vertex attribute arrays.
     327