Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- WK Shearer, April 2012. OpenGL Normalizer routine mock up.
- This routine is formatted for storing normals to a vector for subsequent use
- with glNormalPointer(). This version assumes use of GL_TRIANGLES.
- */
- #include <vector>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- bool object_loader(vector<float> &f_verts, vector<int> &i_lines)
- {
- int i_size;
- int i;
- float temp_vert;
- int temp_line;
- FILE *p_data_file;
- p_data_file = fopen("data.txt", "r");
- if(!p_data_file)
- {
- printf("Data file was not opened.\n");
- return false;
- }
- fscanf(p_data_file, "%d", &i_size);
- if(ferror(p_data_file) || feof(p_data_file))
- {
- fclose(p_data_file);
- return false;
- }
- else
- f_verts.reserve((size_t)i_size);
- for(i = 0; i < (i_size * 3); i++)
- {
- fscanf(p_data_file, "%f", &temp_vert);
- f_verts.push_back(temp_vert);
- if(ferror(p_data_file) || feof(p_data_file))
- {
- printf("File failed reading vertices\n");
- fclose(p_data_file);
- return false;
- }
- }
- fscanf(p_data_file, "%d", &i_size);
- if(ferror(p_data_file) || feof(p_data_file))
- {
- fclose(p_data_file);
- return false;
- }
- else
- i_lines.reserve((size_t)i_size);
- for(i = 0; i < (i_size * 3); i++)
- {
- fscanf(p_data_file, "%d", &temp_line);
- i_lines.push_back(temp_line);
- if(ferror(p_data_file) || feof(p_data_file))
- {
- printf("File failed reading lines\n");
- fclose(p_data_file);
- return false;
- }
- }
- fclose(p_data_file);
- return true;
- }
- //**********************************************************
- void tri_face_normalizer(vector<float> V_verts, vector<int> V_lines, vector<float> &V_normals)
- {
- struct vec_3f
- {
- float x;
- float y;
- float z;
- };
- vec_3f vert[3], vec1, vec2, x_prod, norms;
- size_t face_points = V_lines.size();
- size_t i;
- float normal_total;
- V_normals.reserve(face_points);
- for(i = 0; i < face_points; i += 3)
- {
- // First, load up the coordinates of the three vertices of the face to be normalized.
- // Each face is composed of three points (called V_lines here) that make up the
- // triangle. Those points are identified in each V_line vector variable, as they
- // were loaded from the data file.
- // Each line point (vert) has three coords (x,y,z), and there are three
- // verts to each face: Hence....
- vert[0].x = V_verts.at(V_lines.at(i) * 3);
- vert[0].y = V_verts.at(V_lines.at(i) * 3 + 1);
- vert[0].z = V_verts.at(V_lines.at(i) * 3 + 2);
- vert[1].x = V_verts.at(V_lines.at(i + 1) * 3);
- vert[1].y = V_verts.at(V_lines.at(i + 1) * 3 + 1);
- vert[1].z = V_verts.at(V_lines.at(i + 1) * 3 + 2);
- vert[2].x = V_verts.at(V_lines.at(i + 2) * 3);
- vert[2].y = V_verts.at(V_lines.at(i + 2) * 3 + 1);
- vert[2].z = V_verts.at(V_lines.at(i + 2) * 3 + 2);
- // Create the two 3D vectors for the faces...
- vec1.x = vert[1].x - vert[0].x;
- vec1.y = vert[1].y - vert[0].y;
- vec1.z = vert[1].z - vert[0].z;
- vec2.x = vert[2].x - vert[0].x;
- vec2.y = vert[2].y - vert[0].y;
- vec2.z = vert[2].z - vert[0].z;
- // From the vectors, obtain the cross product...
- x_prod.x = (vec1.y * vec2.z) - (vec1.z * vec2.y);
- x_prod.y = -((vec1.x * vec2.z) - (vec1.z * vec2.x));
- x_prod.z = (vec1.x * vec2.y) - (vec1.y * vec2.x);
- // Normalize the cross product...
- normal_total = sqrtf(powf(x_prod.x, 2) + powf(x_prod.y, 2) + powf(x_prod.z, 2));
- if(fpclassify(norms.x = x_prod.x / normal_total) != FP_NORMAL)
- V_normals.push_back(0.0f);
- else
- V_normals.push_back(norms.x);
- if(fpclassify(norms.y = x_prod.y / normal_total) != FP_NORMAL)
- V_normals.push_back(0.0f);
- else
- V_normals.push_back(norms.y);
- if(fpclassify(norms.z = x_prod.z / normal_total) != FP_NORMAL)
- V_normals.push_back(0.0f);
- else
- V_normals.push_back(norms.z);
- // The routine can be shortened, yes, but it is only done once, at the beginning of
- // program execution, and it is easier to read this way....
- }
- }
- //**********************************************************
- int main()
- {
- vector<float> Vf_vertices;
- vector<int> Vi_lines;
- vector<float> Vf_normals;
- if(!object_loader(Vf_vertices, Vi_lines))
- {
- printf("Object did not load.\n");
- return -1;
- }
- // Test data
- size_t i;
- printf("Vf_vertices size = %d\n", (int)Vf_vertices.size());
- for(i = 0; i < Vf_vertices.size(); i += 3)
- {
- 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));
- }
- printf("Vi_lines size = %d\n", (int)Vi_lines.size());
- for(i = 0; i < Vi_lines.size(); i += 3)
- {
- printf("Lp1 = %d, Lp2 = %d, Lp3 = %d\n", Vi_lines.at(i), Vi_lines.at(i + 1), Vi_lines.at(i + 2));
- }
- tri_face_normalizer(Vf_vertices, Vi_lines, Vf_normals);
- for(i = 0; i < Vf_normals.size(); i += 3)
- {
- 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));
- }
- return 0;
- }
- /*
- This is the data.txt file...
- 8
- -2.5 -2.5 2.5
- 2.5 -2.5 2.5
- 2.5 2.5 2.5
- -2.5 2.5 2.5
- 2.5 -2.5 -2.5
- -2.5 -2.5 -2.5
- -2.5 2.5 -2.5
- 2.5 2.5 -2.5
- 12
- 0 1 3
- 1 2 3
- 1 4 2
- 4 7 2
- 4 5 7
- 5 6 7
- 5 0 6
- 0 3 6
- 3 2 6
- 2 7 6
- 1 0 4
- 0 5 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement