Advertisement
Guest User

mesh.cpp

a guest
May 3rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include "mesh.h"
  4. #include <stdio.h>
  5.  
  6. void insertModel(Mesh **list, int nv, float * vArr, int nt, int * tArr, float scale, Light m) {
  7.     Mesh * mesh = (Mesh *)malloc(sizeof(Mesh));
  8.  
  9.     mesh->T = { 0, 0, 0, 1 };
  10.     mesh->R = { 0, 0, 0, 1 };
  11.     mesh->S = { 1, 1, 1, 1 };
  12.    
  13.     mesh->material.a = m.a;
  14.     mesh->material.d = m.d;
  15.     mesh->material.s = m.s;
  16.     mesh->material.shininess = m.shininess;
  17.  
  18.     mesh->nv = nv;
  19.     mesh->nt = nt; 
  20.     mesh->vertices = (Vector *) malloc(nv * sizeof(Vector));
  21.     mesh->vnorms = (Vector *)malloc(nv * sizeof(Vector));
  22.     mesh->triangles = (Triangle *) malloc(nt * sizeof(Triangle));
  23.    
  24.     // set mesh vertices
  25.     for (int i = 0; i < nv; i++) {
  26.         mesh->vertices[i].x = vArr[i*3] * scale;
  27.         mesh->vertices[i].y = vArr[i*3+1] * scale;
  28.         mesh->vertices[i].z = vArr[i*3+2] * scale;
  29.     }
  30.  
  31.     // set mesh triangles
  32.     for (int i = 0; i < nt; i++) {
  33.         mesh->triangles[i].vInds[0] = tArr[i*3];
  34.         mesh->triangles[i].vInds[1] = tArr[i*3+1];
  35.         mesh->triangles[i].vInds[2] = tArr[i*3+2];
  36.     }
  37.  
  38.     // Assignment 1:
  39.     // Calculate and store suitable vertex normals for the mesh here.
  40.     // Replace the code below that simply sets some arbitrary normal values
  41.     for (int i = 0; i < nv; ++i) {
  42.         Vector tempNormal = {0, 0, 0};
  43.         for (int j = 0; j < nt; ++j)
  44.         {
  45.             for (int r = 0; r < 3; ++r)
  46.             {
  47.                 if (mesh->triangles[j].vInds[r] == i)
  48.                 {
  49.                     // Get 2 vectors between a triangles vertices
  50.                     Vector v1 = Subtract(mesh->vertices[mesh->triangles[j].vInds[1]], mesh->vertices[mesh->triangles[j].vInds[0]]);
  51.                     Vector v2 = Subtract(mesh->vertices[mesh->triangles[j].vInds[2]], mesh->vertices[mesh->triangles[j].vInds[0]]);
  52.  
  53.                     // Calculate triangle normal
  54.                     Vector normal = Normalize(CrossProduct(v1, v2));
  55.  
  56.                     tempNormal = Add(tempNormal, normal);
  57.                     break;
  58.                 }
  59.             }
  60.         }
  61.         mesh->vnorms[i] = Normalize(tempNormal);
  62.     }
  63.     Vector minPoint = { 0, 0, 0 };
  64.     Vector maxPoint = { 0, 0, 0 };
  65.     Vector centerPoint = { 0, 0, 0 };
  66.     float sphereRadius = 0;
  67.  
  68.     for (int i = 0; i < nv; i++)
  69.     {
  70.         if (minPoint.x > mesh->vertices[i].x)
  71.             minPoint.x = mesh->vertices[i].x;
  72.         if (minPoint.y > mesh->vertices[i].y)
  73.             minPoint.y = mesh->vertices[i].y;
  74.         if (minPoint.z > mesh->vertices[i].z)
  75.             minPoint.z = mesh->vertices[i].z;
  76.         if (maxPoint.x < mesh->vertices[i].x)
  77.             maxPoint.x = mesh->vertices[i].x;
  78.         if (maxPoint.y < mesh->vertices[i].y)
  79.             maxPoint.y = mesh->vertices[i].y;
  80.         if (maxPoint.z < mesh->vertices[i].z)
  81.             maxPoint.z = mesh->vertices[i].z;
  82.     }
  83.     //printf("minPoint.x %f\nminPoint.y %f\nminPoint.z %f\n", minPoint.x, minPoint.y, minPoint.z);
  84.  
  85.     //printf("maxPoint.x %f\nmaxPoint.y %f\nmaxPoint.z %f\n", maxPoint.x, maxPoint.y, maxPoint.z);
  86.     centerPoint = Add(maxPoint, minPoint);
  87.  
  88.     //printf("centerPoint.x: %f\ncenterPoint.y: %f\ncenterPoint.z: %f\n", centerPoint.x, centerPoint.y, centerPoint.z);
  89.  
  90.     centerPoint = ScalarVecMul(0.5, centerPoint);
  91.     //printf("centerPoint.x %f\n", centerPoint.x);
  92.  
  93.     Vector distanceCenterMax = { 0, 0, 0 };
  94.     distanceCenterMax = Subtract(maxPoint, centerPoint);
  95.     sphereRadius = Length(distanceCenterMax);
  96.  
  97.     mesh->radius = sphereRadius;
  98.     mesh->meshCenter = centerPoint;
  99.     Matrix T = Translation(mesh->meshCenter.x, mesh->meshCenter.y, mesh->meshCenter.z);
  100.     mesh->T = { T.e[12], T.e[13], T.e[14] };
  101.     mesh->T.w = 1;
  102.     mesh->meshCenter = { 0, 0, 0 };
  103.  
  104.     mesh->next = *list;
  105.     *list = mesh;  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement