Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <math.h>
- #include "mesh.h"
- #include <stdio.h>
- void insertModel(Mesh **list, int nv, float * vArr, int nt, int * tArr, float scale, Light m) {
- Mesh * mesh = (Mesh *)malloc(sizeof(Mesh));
- mesh->T = { 0, 0, 0, 1 };
- mesh->R = { 0, 0, 0, 1 };
- mesh->S = { 1, 1, 1, 1 };
- mesh->material.a = m.a;
- mesh->material.d = m.d;
- mesh->material.s = m.s;
- mesh->material.shininess = m.shininess;
- mesh->nv = nv;
- mesh->nt = nt;
- mesh->vertices = (Vector *) malloc(nv * sizeof(Vector));
- mesh->vnorms = (Vector *)malloc(nv * sizeof(Vector));
- mesh->triangles = (Triangle *) malloc(nt * sizeof(Triangle));
- // set mesh vertices
- for (int i = 0; i < nv; i++) {
- mesh->vertices[i].x = vArr[i*3] * scale;
- mesh->vertices[i].y = vArr[i*3+1] * scale;
- mesh->vertices[i].z = vArr[i*3+2] * scale;
- }
- // set mesh triangles
- for (int i = 0; i < nt; i++) {
- mesh->triangles[i].vInds[0] = tArr[i*3];
- mesh->triangles[i].vInds[1] = tArr[i*3+1];
- mesh->triangles[i].vInds[2] = tArr[i*3+2];
- }
- // Assignment 1:
- // Calculate and store suitable vertex normals for the mesh here.
- // Replace the code below that simply sets some arbitrary normal values
- for (int i = 0; i < nv; ++i) {
- Vector tempNormal = {0, 0, 0};
- for (int j = 0; j < nt; ++j)
- {
- for (int r = 0; r < 3; ++r)
- {
- if (mesh->triangles[j].vInds[r] == i)
- {
- // Get 2 vectors between a triangles vertices
- Vector v1 = Subtract(mesh->vertices[mesh->triangles[j].vInds[1]], mesh->vertices[mesh->triangles[j].vInds[0]]);
- Vector v2 = Subtract(mesh->vertices[mesh->triangles[j].vInds[2]], mesh->vertices[mesh->triangles[j].vInds[0]]);
- // Calculate triangle normal
- Vector normal = Normalize(CrossProduct(v1, v2));
- tempNormal = Add(tempNormal, normal);
- break;
- }
- }
- }
- mesh->vnorms[i] = Normalize(tempNormal);
- }
- Vector minPoint = { 0, 0, 0 };
- Vector maxPoint = { 0, 0, 0 };
- Vector centerPoint = { 0, 0, 0 };
- float sphereRadius = 0;
- for (int i = 0; i < nv; i++)
- {
- if (minPoint.x > mesh->vertices[i].x)
- minPoint.x = mesh->vertices[i].x;
- if (minPoint.y > mesh->vertices[i].y)
- minPoint.y = mesh->vertices[i].y;
- if (minPoint.z > mesh->vertices[i].z)
- minPoint.z = mesh->vertices[i].z;
- if (maxPoint.x < mesh->vertices[i].x)
- maxPoint.x = mesh->vertices[i].x;
- if (maxPoint.y < mesh->vertices[i].y)
- maxPoint.y = mesh->vertices[i].y;
- if (maxPoint.z < mesh->vertices[i].z)
- maxPoint.z = mesh->vertices[i].z;
- }
- //printf("minPoint.x %f\nminPoint.y %f\nminPoint.z %f\n", minPoint.x, minPoint.y, minPoint.z);
- //printf("maxPoint.x %f\nmaxPoint.y %f\nmaxPoint.z %f\n", maxPoint.x, maxPoint.y, maxPoint.z);
- centerPoint = Add(maxPoint, minPoint);
- //printf("centerPoint.x: %f\ncenterPoint.y: %f\ncenterPoint.z: %f\n", centerPoint.x, centerPoint.y, centerPoint.z);
- centerPoint = ScalarVecMul(0.5, centerPoint);
- //printf("centerPoint.x %f\n", centerPoint.x);
- Vector distanceCenterMax = { 0, 0, 0 };
- distanceCenterMax = Subtract(maxPoint, centerPoint);
- sphereRadius = Length(distanceCenterMax);
- mesh->radius = sphereRadius;
- mesh->meshCenter = centerPoint;
- Matrix T = Translation(mesh->meshCenter.x, mesh->meshCenter.y, mesh->meshCenter.z);
- mesh->T = { T.e[12], T.e[13], T.e[14] };
- mesh->T.w = 1;
- mesh->meshCenter = { 0, 0, 0 };
- mesh->next = *list;
- *list = mesh;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement