Advertisement
KeithS

Normalizer in OpenGL format

Apr 22nd, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 KB | None | 0 0
  1. /*
  2. WK Shearer, April 2012. OpenGL Normalizer routine mock up.
  3. This routine is formatted for storing normals to a vector for subsequent use
  4. with glNormalPointer(). This version assumes use of GL_TRIANGLES.
  5. */
  6.  
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cmath>
  10.  
  11. using namespace std;
  12.  
  13. bool object_loader(vector<float> &f_verts, vector<int> &i_lines)
  14. {
  15.     int i_size;
  16.     int i;
  17.     float temp_vert;
  18.     int temp_line;
  19.     FILE *p_data_file;
  20.     p_data_file = fopen("data.txt", "r");
  21.     if(!p_data_file)
  22.     {
  23.         printf("Data file was not opened.\n");
  24.         return false;
  25.     }
  26.  
  27.     fscanf(p_data_file, "%d", &i_size);
  28.     if(ferror(p_data_file) || feof(p_data_file))
  29.     {
  30.     fclose(p_data_file);
  31.         return false;
  32.     }
  33.     else
  34.         f_verts.reserve((size_t)i_size);
  35.  
  36.     for(i = 0; i < (i_size * 3); i++)
  37.     {
  38.         fscanf(p_data_file, "%f", &temp_vert);
  39.         f_verts.push_back(temp_vert);
  40.         if(ferror(p_data_file) || feof(p_data_file))
  41.         {
  42.             printf("File failed reading vertices\n");
  43.         fclose(p_data_file);
  44.             return false;
  45.         }
  46.     }
  47.  
  48.     fscanf(p_data_file, "%d", &i_size);
  49.     if(ferror(p_data_file) || feof(p_data_file))
  50.     {
  51.         fclose(p_data_file);
  52.         return false;
  53.     }
  54.     else
  55.         i_lines.reserve((size_t)i_size);
  56.  
  57.     for(i = 0; i < (i_size * 3); i++)
  58.     {
  59.         fscanf(p_data_file, "%d", &temp_line);
  60.         i_lines.push_back(temp_line);
  61.         if(ferror(p_data_file) || feof(p_data_file))
  62.         {
  63.             printf("File failed reading lines\n");
  64.         fclose(p_data_file);
  65.             return false;
  66.         }
  67.  
  68.     }
  69.     fclose(p_data_file);
  70.     return true;
  71. }
  72.  
  73. //**********************************************************
  74.  
  75. void tri_face_normalizer(vector<float> V_verts, vector<int> V_lines, vector<float> &V_normals)
  76. {
  77.     struct vec_3f
  78.     {
  79.         float x;
  80.         float y;
  81.         float z;
  82.     };
  83.     vec_3f vert[3], vec1, vec2, x_prod, norms;
  84.  
  85.     size_t face_points = V_lines.size();
  86.     size_t i;
  87.     float normal_total;
  88.  
  89.     V_normals.reserve(face_points);
  90.  
  91.     for(i = 0; i < face_points; i += 3)
  92.     {
  93.         // First, load up the coordinates of the three vertices of the face to be normalized.
  94.         // Each face is composed of three points (called V_lines here) that make up the
  95.     // triangle. Those points are identified in each V_line vector variable, as they
  96.     // were loaded from the data file.
  97.     // Each line point (vert) has three coords (x,y,z), and there are three
  98.         // verts to each face: Hence....
  99.  
  100.         vert[0].x = V_verts.at(V_lines.at(i) * 3);
  101.         vert[0].y = V_verts.at(V_lines.at(i) * 3 + 1);
  102.         vert[0].z = V_verts.at(V_lines.at(i) * 3 + 2);
  103.  
  104.         vert[1].x = V_verts.at(V_lines.at(i + 1) * 3);
  105.         vert[1].y = V_verts.at(V_lines.at(i + 1) * 3 + 1);
  106.         vert[1].z = V_verts.at(V_lines.at(i + 1) * 3 + 2);
  107.  
  108.         vert[2].x = V_verts.at(V_lines.at(i + 2) * 3);
  109.         vert[2].y = V_verts.at(V_lines.at(i + 2) * 3 + 1);
  110.         vert[2].z = V_verts.at(V_lines.at(i + 2) * 3 + 2);
  111.  
  112.         // Create the two 3D vectors for the faces...
  113.         vec1.x = vert[1].x - vert[0].x;
  114.         vec1.y = vert[1].y - vert[0].y;
  115.         vec1.z = vert[1].z - vert[0].z;
  116.  
  117.         vec2.x = vert[2].x - vert[0].x;
  118.         vec2.y = vert[2].y - vert[0].y;
  119.         vec2.z = vert[2].z - vert[0].z;
  120.  
  121.         // From the vectors, obtain the cross product...
  122.         x_prod.x =   (vec1.y * vec2.z) - (vec1.z * vec2.y);
  123.         x_prod.y = -((vec1.x * vec2.z) - (vec1.z * vec2.x));
  124.         x_prod.z =   (vec1.x * vec2.y) - (vec1.y * vec2.x);
  125.  
  126.         // Normalize the cross product...
  127.         normal_total = sqrtf(powf(x_prod.x, 2) + powf(x_prod.y, 2) + powf(x_prod.z, 2));
  128.  
  129.     if(fpclassify(norms.x = x_prod.x / normal_total) != FP_NORMAL)
  130.         V_normals.push_back(0.0f);
  131.     else
  132.         V_normals.push_back(norms.x);
  133.  
  134.     if(fpclassify(norms.y = x_prod.y / normal_total) != FP_NORMAL)
  135.         V_normals.push_back(0.0f);
  136.     else
  137.         V_normals.push_back(norms.y);
  138.  
  139.     if(fpclassify(norms.z = x_prod.z / normal_total) != FP_NORMAL)
  140.             V_normals.push_back(0.0f);
  141.     else
  142.         V_normals.push_back(norms.z);
  143.  
  144.         // The routine can be shortened, yes, but it is only done once, at the beginning of
  145.         // program execution, and it is easier to read this way....
  146.  
  147.     }
  148.  
  149. }
  150.  
  151. //**********************************************************
  152.  
  153. int main()
  154. {
  155.     vector<float> Vf_vertices;
  156.     vector<int> Vi_lines;
  157.     vector<float> Vf_normals;
  158.     if(!object_loader(Vf_vertices, Vi_lines))
  159.     {
  160.         printf("Object did not load.\n");
  161.     return -1;
  162.     }
  163.  
  164.     // Test data
  165.     size_t i;
  166.  
  167.     printf("Vf_vertices size = %d\n", (int)Vf_vertices.size());
  168.     for(i = 0; i < Vf_vertices.size(); i += 3)
  169.     {
  170.         printf("Vert_x = %.2f, Vert_y = %.2f, Vert_z = %.2f\n", Vf_vertices.at(i), Vf_vertices.at(i + 1), Vf_vertices.at(i + 2));
  171.     }
  172.     printf("Vi_lines size = %d\n", (int)Vi_lines.size());
  173.     for(i = 0; i < Vi_lines.size(); i += 3)
  174.     {
  175.         printf("Lp1 = %d, Lp2 = %d, Lp3 = %d\n", Vi_lines.at(i), Vi_lines.at(i + 1), Vi_lines.at(i + 2));
  176.     }
  177.  
  178.     tri_face_normalizer(Vf_vertices, Vi_lines, Vf_normals);
  179.  
  180.     for(i = 0; i < Vf_normals.size(); i += 3)
  181.     {
  182.         printf("X_norm = %.2f, Y_norm = %.2f, Z_norm = %.2f\n", Vf_normals.at(i), Vf_normals.at(i + 1), Vf_normals.at(i + 2));
  183.     }
  184.  
  185.  
  186.     return 0;
  187. }
  188.  
  189. /*
  190. This is the data.txt file...
  191.  
  192. 8
  193. -2.5 -2.5 2.5
  194. 2.5 -2.5 2.5
  195. 2.5 2.5 2.5
  196. -2.5 2.5 2.5
  197. 2.5 -2.5 -2.5
  198. -2.5 -2.5 -2.5
  199. -2.5 2.5 -2.5
  200. 2.5 2.5 -2.5
  201. 12
  202. 0 1 3
  203. 1 2 3
  204. 1 4 2
  205. 4 7 2
  206. 4 5 7
  207. 5 6 7
  208. 5 0 6
  209. 0 3 6
  210. 3 2 6
  211. 2 7 6
  212. 1 0 4
  213. 0 5 4
  214.  
  215. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement