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 |
| 659 | GLfloat *point; int points; |
| 660 | GLuint *triangle; int triangles; |
| 661 | GLfloat *pressure; |
| 662 | |
| 663 | void 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 |
| 713 | void 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. |