Changes between Version 36 and Version 37 of tutorial


Ignore:
Timestamp:
Jun 30, 2013, 9:32:00 PM (11 years ago)
Author:
leon
Comment:

Fix temperature example

Legend:

Unmodified
Added
Removed
Modified
  • tutorial

    v36 v37  
    159159Create {{{triangle.c}}} and update {{{Makefile}}} with new target
    160160{{{
    161 #!sh
     161#!c
    162162#include <stdio.h>
    163163#include <stdlib.h>
     
    165165#include <GL/glut.h>
    166166
    167 static const GLchar * vertex_shader[] =
    168   {"void main()"
    169    "{"
    170    "  gl_Position = ftransform();"
    171    "}"
    172   };
     167GLuint program;
     168GLuint vbo_vertices;
     169GLint attribute_coord2d;
     170
     171static const GLchar * vertex_shader[] = {
     172  "attribute vec2 coord2d;" // input vertex position
     173  "void main()"
     174  "{"
     175  "  gl_Position = gl_ModelViewProjectionMatrix*vec4(coord2d, 0.0, 1.0);"
     176  "}"
     177};
    173178static const GLchar * fragment_shader[] =
    174179  {"void main()"
     
    178183  };
    179184
    180 void setShaders()
    181 {
    182   GLuint v, f, p;
     185void create_shaders()
     186{
     187  GLuint v, f;
    183188
    184189  v = glCreateShader(GL_VERTEX_SHADER);
     
    188193  glCompileShader(v);
    189194  glCompileShader(f);
    190   p = glCreateProgram();
    191   glAttachShader(p,f);
    192   glAttachShader(p,v);
    193   glLinkProgram(p);
    194   glUseProgram(p);
    195 }
    196 
    197 
    198 enum VAO_IDs { Triangles, NumVAOs };
    199 enum Buffer_IDs {ArrayBuffer,NumBuffers};
    200 enum Attrib_IDs { vPosition = 0 };
    201 GLuint VAOs[NumVAOs];
    202 GLuint Buffers[NumBuffers];
    203 #define NumVertices  6
    204 
    205 void init(void)
    206 {
    207   glGenVertexArrays(NumVAOs, VAOs);
    208   glBindVertexArray(VAOs[Triangles]);
    209   GLfloat vertices[NumVertices][2] = {
     195  program = glCreateProgram();
     196  glAttachShader(program, f);
     197  glAttachShader(program, v);
     198  glLinkProgram(program);
     199  glUseProgram(program);
     200
     201  attribute_coord2d = glGetAttribLocation(program, "coord2d");
     202  if (attribute_coord2d == -1) {
     203    fprintf(stderr, "Could not bind attribute coord2d\n");
     204  }
     205  glEnableVertexAttribArray(attribute_coord2d);
     206}
     207
     208void send_buffers_to_GPU(void)
     209{
     210  GLuint vertex_array_object;
     211  glGenVertexArrays(1, &vertex_array_object);
     212  glBindVertexArray(vertex_array_object);
     213 
     214  GLfloat vertices[][2] = {
    210215    { -0.90, -0.90 }, // Triangle 1
    211216    {  0.85, -0.90 },
     
    215220    { -0.85,  0.90 }
    216221  };
    217   glGenBuffers(NumBuffers, Buffers);
    218   glBindBuffer( GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
     222
     223  glGenBuffers(1, &vbo_vertices);
     224  glBindBuffer( GL_ARRAY_BUFFER, vbo_vertices);
    219225  glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    220   glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, (const void*)0);
    221   glEnableVertexAttribArray(vPosition);
    222226}
    223227
     
    226230{
    227231  glClear(GL_COLOR_BUFFER_BIT);
    228   glBindVertexArray(VAOs[Triangles]);
    229   glDrawArrays(GL_TRIANGLES, 0, NumVertices);
     232  glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
     233  glVertexAttribPointer(attribute_coord2d, 2, GL_FLOAT, GL_FALSE, 0, NULL);
     234  glDrawArrays(GL_TRIANGLES, 0, 6); // Draw 6 vertices
    230235  glutSwapBuffers();
    231236}
     
    244249   }
    245250  glClearColor(0.9,1.0,1.0,1.0);
    246   init();
    247   setShaders();
     251  send_buffers_to_GPU();
     252  create_shaders();
    248253  glutMainLoop();
    249   return 0;
     254  return EXIT_SUCCESS;
    250255}
    251256}}}
    252257[[Image(triangle.png,align=bottom,right,title=Triangles )]]
    253258=== Exercises #2 ===
    254  1. To be able to continue and not get lost introduce shader compiler logs in case of compilation errors by adding the following code into {{{setShaders()}}} right at after vertex shader compilation:
    255    {{{
    256    #!c
    257     GLint compiled;
    258     glGetShaderiv(v, GL_COMPILE_STATUS, &compiled );
    259     if ( !compiled ) {
    260       GLsizei len;
    261       glGetShaderiv( v, GL_INFO_LOG_LENGTH, &len );
    262       GLchar* log = malloc(sizeof(GLchar)*(len+1));
    263       printf("Vertex Shader compilation failed: %s\n", log);
    264       free(log);
    265     }
    266    }}}
     259 1. To be able to continue and not get lost introduce shader compiler logs in case of compilation errors by adding the following code into {{{create_shaders()}}} right at after vertex shader compilation:
     260  {{{
     261  #!c
     262  GLint compiled;
     263  glGetShaderiv(v, GL_COMPILE_STATUS, &compiled );
     264  if ( !compiled ) {
     265    GLsizei  maxLength, length;
     266    glGetShaderiv( v, GL_INFO_LOG_LENGTH, &maxLength );
     267    GLchar* log = malloc(sizeof(GLchar)*(maxLength+1));
     268    glGetShaderInfoLog(v,  maxLength, &length, log);
     269    printf("Vertex Shader compilation failed: %s\n", log);
     270    free(log);
     271  }
     272  }}}
    267273 Do not forget to repeat the same thing for fragment shader.
    268274 Add linker debugging
    269    {{{
    270    #!c 
    271     GLint linked;
    272     glGetProgramiv(p, GL_LINK_STATUS, &linked );
    273     if ( !linked ) {
    274       GLsizei len;
    275       glGetProgramiv( p, GL_INFO_LOG_LENGTH, &len );
    276       GLchar* log = malloc(sizeof(GLchar)*(len+1));
    277       glGetProgramInfoLog( p, len, &len, log );
    278       printf("Shader linking failed: %s\n", log);
    279       free(log);
    280     }
    281    }}}
    282    Create some error to verify if it works.
     275  {{{
     276  #!c 
     277  GLint linked;
     278  glGetProgramiv(program, GL_LINK_STATUS, &linked );
     279  if ( !linked ) {
     280    GLsizei len;
     281    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len );
     282    GLchar* log = malloc(sizeof(GLchar)*(len+1));
     283    glGetProgramInfoLog(program, len, &len, log );
     284    printf("Shader linking failed: %s\n", log);
     285    free(log);
     286  }
     287  }}}
     288  Create some error to verify if it works.
    283289  For general (core) OpenGL errors we can use the following `glcheck()` utility at suspicious places.
    284290  {{{
     
    288294                           gluErrorString(s),  __LINE__);}
    289295  }}}
    290  2. Introduce vertex temperature with additional array and buffer at the end off `init()`
    291   [[Image(temperature.png,right)]]
     296 2. [[Image(temperature.png,right)]] Copy `triangle.c` into `temperature.c` and introduce vertex temperature with additional array and buffer at the end of `send_buffers_to_GPU()`
    292297  {{{
    293298  #!c
    294299  GLfloat vertex_temperature[] = {0, 1, 0.2, 0.1, 0.5, 0.9};
    295   glBindBuffer(GL_ARRAY_BUFFER, Buffers[TempBuffer]);
     300  glGenBuffers(1, &vbo_temperature);
     301  glBindBuffer(GL_ARRAY_BUFFER, vbo_temperature);
    296302  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
     303             vertex_temperature, GL_STATIC_DRAW);
     304  }}}
     305  and adding corresponding global attribute and VBOs IDs at the top of `temperature.c` so that global section reads:   
     306  {{{
     307  #!c
     308  GLuint program;
     309  GLuint vbo_vertices;
     310  GLUINT vbo_temperature;
     311  GLint attribute_coord2d;
     312  GLint attribute_temperature;
     313 }}} 
     314 Replace shaders with
    302315  {{{
    303316  #!c
     
    322335  "}"
    323336};
     337  }}}
     338  Bind temperature buffer and specify data layout within {{{display()}}} just before {{{glDrawElements()}}} with
     339  {{{
     340  #!c
     341  glBindBuffer(GL_ARRAY_BUFFER, vbo_temperature);
     342  glVertexAttribPointer(attribute_temperature, 1, GL_FLOAT, GL_FALSE, 0, NULL);
    324343  }}}
    325344  What happens if we don't enable temperature vertex array? Confer [attachment:temperature.c] attached if having troubles.