Changes between Version 37 and Version 38 of tutorial


Ignore:
Timestamp:
Jun 30, 2013, 10:22:50 PM (11 years ago)
Author:
leon
Comment:

Teapot revisited

Legend:

Unmodified
Added
Removed
Modified
  • tutorial

    v37 v38  
    308308  GLuint program;
    309309  GLuint vbo_vertices;
    310   GLUINT vbo_temperature;
     310  GLuint vbo_temperature;
    311311  GLint attribute_coord2d;
    312312  GLint attribute_temperature;
     
    349349[[Image(teapot.png,right)]]
    350350
    351 Assemble the following Utah teapot model and attached virtual [attachment:trackball.h] and [attachment:trackball.c] sources from SGI. We use
     351Assemble the following Utah teapot model and attached virtual [attachment:trackball.h] and [attachment:trackball.c] sources from SGI.
    352352{{{
    353353#!c
     
    360360#include "trackball.h"
    361361
    362 GLuint p; // program needs to be global!
    363 float lpos[4] = {1, 0.5, 1, 0};
    364 GLfloat m[4][4]; // modelview rotation matrix
    365 float last[4], cur[4]; // rotation tracking quaternions
    366 int width, height, beginx, beginy;
    367 float p1x, p1y, p2x, p2y;
    368 
    369 void display(void) {
    370   GLuint location = glGetUniformLocation(p, "RotationMatrix");
    371   build_rotmatrix(m, cur); 
    372   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    373   glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    374   if( location >= 0 )
    375     glUniformMatrix4fv(location, 1, GL_FALSE, &m[0][0]);
    376   glutSolidTeapot(0.6);
    377   glutSwapBuffers();
    378 }
    379 
    380 
    381 void processNormalKeys(unsigned char key, int x, int y) {
    382         if (key == 27)
    383                 exit(0);
    384 }
    385 
    386 void mouse(int button,int state, int x, int y)   
    387 {
    388   beginx = x;
    389   beginy = y;
    390 }
    391 
    392 void motion(int x,int y)   
    393 {
    394   p1x = (2.0*beginx - width)/width;
    395   p1y = (height - 2.0*beginy)/height;
    396   p2x = (2.0 * x - width) / width;
    397   p2y = (height - 2.0 * y) / height;
    398   trackball(last, p1x, p1y, p2x, p2y);   
    399   add_quats(last, cur, cur);   
    400   beginx = x;
    401   beginy = y;
    402   glutPostRedisplay();   
    403 }
    404 
    405 void reshape (int w, int h)
    406 {
    407   double l = 1;
    408   width=w;  height=h;
    409   glViewport (0, 0, w, h);
    410   glMatrixMode (GL_PROJECTION);
    411   glLoadIdentity();
    412   glOrtho(-l, l, -l, l, -l, l);
    413   glMatrixMode(GL_MODELVIEW);
    414   glLoadIdentity();
    415 }
     362GLuint program;
    416363
    417364static const GLchar * vertex_shader[] ={"\
     
    451398}"};
    452399
    453 void setShaders()
     400void create_shaders()
    454401{
    455402  GLuint v, f;
     
    460407  glShaderSource(f, 1, fragment_shader, NULL);
    461408  glCompileShader(v);
     409  GLint compiled;
     410  glGetShaderiv(v, GL_COMPILE_STATUS, &compiled );
     411  if ( !compiled ) {
     412    GLsizei  maxLength, length;
     413    glGetShaderiv( v, GL_INFO_LOG_LENGTH, &maxLength );
     414    GLchar* log = malloc(sizeof(GLchar)*(maxLength+1));
     415    glGetShaderInfoLog(v,  maxLength, &length, log);
     416    printf("Vertex Shader compilation failed: %s\n", log);
     417    free(log);
     418  }
    462419  glCompileShader(f);
    463   p = glCreateProgram();
    464   glAttachShader(p,f);
    465   glAttachShader(p,v);
    466   glLinkProgram(p);
    467   glUseProgram(p);
     420  glGetShaderiv(f, GL_COMPILE_STATUS, &compiled );
     421  if ( !compiled ) {
     422    GLsizei  maxLength, length;
     423    glGetShaderiv( f, GL_INFO_LOG_LENGTH, &maxLength );
     424    GLchar* log = malloc(sizeof(GLchar)*(maxLength+1));
     425    glGetShaderInfoLog(f,  maxLength, &length, log);
     426    printf("Fragment Shader compilation failed: %s\n", log);
     427    free(log);
     428  }
     429  program = glCreateProgram();
     430  glAttachShader(program, f);
     431  glAttachShader(program, v);
     432  glLinkProgram(program);
     433  GLint linked;
     434  glGetProgramiv(program, GL_LINK_STATUS, &linked );
     435  if ( !linked ) {
     436    GLsizei len;
     437    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len );
     438    GLchar* log = malloc(sizeof(GLchar)*(len+1));
     439    glGetProgramInfoLog(program, len, &len, log );
     440    printf("Shader linking failed: %s\n", log);
     441    free(log);
     442  }
     443  glUseProgram(program);
     444}
     445
     446
     447float lpos[4] = {1, 0.5, 1, 0};
     448GLfloat m[4][4]; // modelview rotation matrix
     449float last[4], cur[4]; // rotation tracking quaternions
     450int width, height, beginx, beginy;
     451float p1x, p1y, p2x, p2y;
     452
     453void display(void) {
     454  GLuint location = glGetUniformLocation(program, "RotationMatrix");
     455  build_rotmatrix(m, cur); 
     456  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     457  glLightfv(GL_LIGHT0, GL_POSITION, lpos);
     458  if( location >= 0 )
     459    glUniformMatrix4fv(location, 1, GL_FALSE, &m[0][0]);
     460  glutSolidTeapot(0.6);
     461  glutSwapBuffers();
     462}
     463
     464void reshape (int w, int h)
     465{
     466  double l = 1;
     467  width=w;  height=h;
     468  glViewport (0, 0, w, h);
     469  glMatrixMode (GL_PROJECTION);
     470  glLoadIdentity();
     471  glOrtho(-l, l, -l, l, -l, l);
     472  glMatrixMode(GL_MODELVIEW);
     473  glLoadIdentity();
     474}
     475
     476void keys(unsigned char key, int x, int y)
     477{
     478   if (key == 27 || key == 'q')
     479         exit(0);
     480}
     481
     482void mouse(int button,int state, int x, int y)   
     483{
     484  beginx = x;
     485  beginy = y;
     486}
     487
     488void motion(int x,int y)   
     489{
     490  p1x = (2.0*beginx - width)/width;
     491  p1y = (height - 2.0*beginy)/height;
     492  p2x = (2.0 * x - width) / width;
     493  p2y = (height - 2.0 * y) / height;
     494  trackball(last, p1x, p1y, p2x, p2y);   
     495  add_quats(last, cur, cur);   
     496  beginx = x;
     497  beginy = y;
     498  glutPostRedisplay();   
    468499}
    469500
     
    483514  glutMouseFunc(mouse);
    484515  glutMotionFunc(motion);
    485   glutKeyboardFunc(processNormalKeys);
     516  glutKeyboardFunc(keys);
    486517
    487518  glEnable(GL_DEPTH_TEST);
     
    493524     exit(EXIT_FAILURE);
    494525   }
    495   setShaders();
     526  create_shaders();
    496527  glutMainLoop();
    497   return 0;
    498 }
     528  return EXIT_SUCCESS;
     529}
     530}}}
     531To build two sources we add the following line to `Makefile`:
     532{{{
     533#!sh
     534
     535teapot: teapot.o trackball.o
     536
    499537}}}
    500538=== Exercises #3 ===
     
    538576== Reading Objects ==
    539577[[Image(motorBike-subset.png,right)]]
    540 Sometimes we hit limitations of the visualisation tools for the data that we want to visualize. For example [attachment:motorBike.obj] from [http://openfoam.org OpenFOAM] contains object groups that we want to show colored separately and not as whole. Neither [http://wci.llnl.gov/codes/visit/ VisIt] and [http://paraview.org ParaView] can read [http://en.wikipedia.org/wiki/Wavefront_.obj_file Wavefront OBJ] file with group separation. We are forced to convert [attachment:motorBike.obj] into bunch of files and read them one by one. The following {{{wavefront.c}}} converts [attachment:motorBike.obj] into 67 files. Try to open them in VisIt and ParaView.
     578Sometimes we hit limitations of the visualisation tools for the data that we want to visualize. For example [attachment:motorBike.obj] from [http://openfoam.org OpenFOAM] contains object groups that we want to show colored separately and not as whole. Neither [http://wci.llnl.gov/codes/visit/ VisIt] and [http://paraview.org ParaView] can read [http://en.wikipedia.org/wiki/Wavefront_.obj_file Wavefront OBJ] file with group separation. We are forced to convert [attachment:motorBike.obj] into bunch of files and read them one by one. The following {{{wavefront.c}}} converts [attachment:motorBike.obj] into 67 files. Try to open them in [http://wci.llnl.gov/codes/visit/ VisIt] and [http://paraview.org ParaView]. Note that we need to compensate vertex counting that starts with 1 and not with 0.
    541579{{{
    542580#!c
     
    608646}}}
    609647[[Image(point-cloud.png,right)]]
    610 === Exercises #3 ===
     648=== Exercises #4 ===
    611649 1. Insert {{{teapot.c}}} interactivity example into {{{wavefront.c}}} above and save it as {{{motorbike.c}}}.
    612650   Verify that there are no compile problems and that the {{{main()}}} contains {{{read_wavefront("motorBike.obj");}}}.