Changes between Version 48 and Version 49 of tutorial


Ignore:
Timestamp:
Jul 2, 2013, 11:15:31 PM (11 years ago)
Author:
leon
Comment:

Add pressure exercise

Legend:

Unmodified
Added
Removed
Modified
  • tutorial

    v48 v49  
    654654  What happens if we don't enable temperature vertex array? Confer [attachment:temperature.c] attached if having troubles.
    655655 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.
    656 
     656 4. [[Image(pressure.png,right)]] Fetch pressure data as 2D y-slice in file [attachment:p_yNormal.vtk] formatted in [http://www.vtk.org VTK] that can be quickly read by the following code:
     657{{{
     658#!c
     659GLfloat *point;   int points;
     660GLuint *triangle; int triangles;
     661GLfloat *pressure; 
     662
     663void read_VTK_pressure(const char *filename)
     664{
     665  char line[80];
     666  int i; FILE *f;
     667  f = fopen(filename, "r");
     668  while(fgets(line, 80, f))
     669    {
     670      if (strstr(line, "POINTS"))
     671        {
     672          float dummy_y;
     673          points = atof(line+7);
     674          point = malloc(points*2*sizeof(float));
     675          pressure = malloc(points*sizeof(float));
     676          assert(point != NULL && pressure != NULL);
     677          for(i = 0; i < points; ++i)
     678            fscanf(f, "%f %f %f", &point[i*2], &dummy_y, &point[i*2+1]);
     679        }
     680      else if (strstr(line, "POLYGONS"))
     681        {
     682          triangles = atof(line+9);
     683          triangle = malloc(triangles*3*sizeof(GLuint));
     684          for (i = 0; i < triangles; ++i)
     685            {
     686              int n;
     687              fscanf(f, "%d %d %d %d", &n, &triangle[i*3],
     688                     &triangle[i*3+1], &triangle[i*3+2]);
     689            }
     690        }
     691      else if (strstr(line, "FIELD"))
     692        {
     693          fgets(line, 80, f); // skip: p 1 27582 float
     694          for (i = 0; i < points; ++i)
     695            fscanf(f, "%f", &pressure[i]);
     696        }
     697    }
     698  fclose(f);
     699  printf("Read %d points for %d triangles for field %s\n",
     700         points, triangles, filename);
     701}
     702}}}
     703 Insert this code into [attachment:temperature.c] and rename temperature with pressure   everywhere. We will be drawing indexed so the last lines of the {{{display()}} reads
     704{{{
     705#!c
     706  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
     707  glDrawElements(GL_TRIANGLES, triangles*3, GL_UNSIGNED_INT, 0);
     708  glutSwapBuffers();
     709}}}
     710 Sending element data buffers to GPU is slightly changed by using  {{{GL_ELEMENT_ARRAY_BUFFER}}} instead of {{{GL_ARRAY_BUFFER}}} in subroutine
     711{{{
     712#!c
     713void send_buffers_to_GPU(void)
     714{
     715  GLuint vertex_array_object;
     716  glGenVertexArrays(1, &vertex_array_object);
     717  glBindVertexArray(vertex_array_object);
     718 
     719  glGenBuffers(1, &vbo_vertices);
     720  glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
     721  glBufferData(GL_ARRAY_BUFFER, points*2*sizeof(GLfloat), point, GL_STATIC_DRAW);
     722
     723  glGenBuffers(1, &vbo_pressure);
     724  glBindBuffer(GL_ARRAY_BUFFER, vbo_pressure);
     725  glBufferData(GL_ARRAY_BUFFER, points*sizeof(GLfloat), pressure, GL_STATIC_DRAW);
     726
     727  glGenBuffers(1, &ibo_elements);
     728  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
     729  glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles*3*sizeof(GLuint),
     730               triangle, GL_STATIC_DRAW);
     731}
     732}}}
     733 Positioning (translation) of the motorbike and scaling of the pressure [-300..200] to color mix range [0-1] can be done in shaders directly. Confer final [attachment:pressure.c] if having trubles with coding.
    657734
    658735== Interactivity ==
     
    10521129 1. Draw streamlines with velocity colorization
    10531130 1. Convert example into GPU buffered (VBOs).
     1131 1. Combine sliced pressure data(VTK) and model (OBJ) together as custom CFD visualization not available in visualization tools to date.