aboutsummaryrefslogblamecommitdiff
path: root/src/Models.cpp
blob: ad0f35746e43d030665517ffa298a6d6361d7b5a (plain) (tree)
1
2
3
4
5
6
                    
                
 
                   
                 
 











                                                           








                                                                
 








                                                                 
 









                                                                 
 






                                                  
                                                                     

                                                                           

 
                                  
 







                                                      
 









                                                                 

                           
 






















                                                                           













                                                                   



                                           

                                    







                                                                                                                                                              
                                                









                                                                                                                                     

                                    











                                                                                                                                                              
                                                


                                                                                                                                     
 








                                                                        

                                    










                                                                                                                                     




                                                       
                                                


                                                                                                                                     
 








                                                                              

                                    











                                                                                                                                                              
                                                






                                                                                                                                     
 
#include <algorithm>
#include <cmath>

#include "Models.h"
#include "misc.h"

void Model::CalculateNormals()
{
	for (int i = 0; i < TriangleNum; ++i) {
		CrossProduct(vertex[Triangles[i].vertex[1]]
			- vertex[Triangles[i].vertex[0]],
			vertex[Triangles[i].vertex[2]]
			- vertex[Triangles[i].vertex[0]],
			normals + i);
		Normalise(normals + i);
	}

	for (int i = 0; i < TriangleNum; ++i) {
		vArray[i*27+0]=vertex[Triangles[i].vertex[0]].x;
		vArray[i*27+1]=vertex[Triangles[i].vertex[0]].y;
		vArray[i*27+2]=vertex[Triangles[i].vertex[0]].z;
		vArray[i*27+3]=normals[i].x;
		vArray[i*27+4]=normals[i].y;
		vArray[i*27+5]=normals[i].z;
		vArray[i*27+6]=Triangles[i].r;
		vArray[i*27+7]=Triangles[i].g;
		vArray[i*27+8]=Triangles[i].b;

		vArray[i*27+9]=vertex[Triangles[i].vertex[1]].x;
		vArray[i*27+10]=vertex[Triangles[i].vertex[1]].y;
		vArray[i*27+11]=vertex[Triangles[i].vertex[1]].z;
		vArray[i*27+12]=normals[i].x;
		vArray[i*27+13]=normals[i].y;
		vArray[i*27+14]=normals[i].z;
		vArray[i*27+15]=Triangles[i].r;
		vArray[i*27+16]=Triangles[i].g;
		vArray[i*27+17]=Triangles[i].b;

		vArray[i*27+18]=vertex[Triangles[i].vertex[2]].x;
		vArray[i*27+19]=vertex[Triangles[i].vertex[2]].y;
		vArray[i*27+20]=vertex[Triangles[i].vertex[2]].z;
		vArray[i*27+21]=normals[i].x;
		vArray[i*27+22]=normals[i].y;
		vArray[i*27+23]=normals[i].z;
		vArray[i*27+24]=Triangles[i].r;
		vArray[i*27+25]=Triangles[i].g;
		vArray[i*27+26]=Triangles[i].b;
	}

	boundingspherecenter = {};
	for (int i = 0; i < vertexNum; ++i)
		boundingspherecenter += vertex[i];
	boundingspherecenter /= vertexNum;

	boundingsphereradius = 0;
	for (int i = 0; i < vertexNum; ++i)
		boundingsphereradius = std::max(boundingsphereradius,
			findDistancefast(boundingspherecenter, vertex[i]));
	boundingsphereradius = sqrt(boundingsphereradius);
}

void Model::load(const char* path)
{
	auto model = loadModel(path);
	vertexNum = model.vertices.len;
	for (short i = 0; i < vertexNum; ++i) {
		vertex[i].x = model.vertices.ptr[i].x;
		vertex[i].y = model.vertices.ptr[i].y;
		vertex[i].z = model.vertices.ptr[i].z;
	}
	free(model.vertices.ptr);

	TriangleNum = model.faces.len;
	for (short i = 0; i < TriangleNum; ++i) {
		Triangles[i].vertex[0] = model.faces.ptr[i].v[0];
		Triangles[i].vertex[1] = model.faces.ptr[i].v[1];
		Triangles[i].vertex[2] = model.faces.ptr[i].v[2];
		Triangles[i].r = model.faces.ptr[i].r;
		Triangles[i].g = model.faces.ptr[i].g;
		Triangles[i].b = model.faces.ptr[i].b;
	}
	free(model.faces.ptr);
	CalculateNormals();
}

extern int nocolors;
void Model::draw()
{
	if(!nocolors){
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glVertexPointer(3, GL_FLOAT, 9*sizeof(GLfloat),&vArray[0]);
	glNormalPointer(GL_FLOAT, 9*sizeof(GLfloat),&vArray[3]);
	glColorPointer(3,GL_FLOAT, 9*sizeof(GLfloat),&vArray[6]);
	glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
	}
	if(nocolors){
		glColor4f(0,0,0,1);
		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_NORMAL_ARRAY);
		glDisableClientState(GL_COLOR_ARRAY);
		glVertexPointer(3, GL_FLOAT, 9*sizeof(GLfloat),&vArray[0]);
		glNormalPointer(GL_FLOAT, 9*sizeof(GLfloat),&vArray[3]);
		glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
	}
}

void Model::draw(float r, float g, float b)
{
	if(!nocolors)glColor4f(r,g,b,1);
	if(nocolors==1)glColor4f(0,0,0,1);
	if(nocolors==2)glColor4f(1,0,0,1);
	if(nocolors==3)glColor4f(0,0,1,1);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	glVertexPointer(3, GL_FLOAT, 9*sizeof(GLfloat),&vArray[0]);
	glNormalPointer(GL_FLOAT, 9*sizeof(GLfloat),&vArray[3]);
	glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
}

int Model::LineCheck(XYZ p1,XYZ p2, XYZ *p)
{
  	int j;
	float distance;
	float olddistance=9999999.0;
	int intersecting=0;
	int firstintersecting=-1;
	XYZ point;
	if(sphere_line_intersection(p1.x,p1.y,p1.z,
								p2.x,p2.y,p2.z,
								boundingspherecenter.x,boundingspherecenter.y,boundingspherecenter.z,
								boundingsphereradius))
	for (j=0;j<TriangleNum;j++){
		intersecting=LineFacetd(p1,p2,vertex[Triangles[j].vertex[0]],vertex[Triangles[j].vertex[1]],vertex[Triangles[j].vertex[2]],normals[j],&point);
		if (intersecting == 0) continue;
		distance=(point.x-p1.x)*(point.x-p1.x)+(point.y-p1.y)*(point.y-p1.y)+(point.z-p1.z)*(point.z-p1.z);
		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
	}
	return firstintersecting;
}

int Model::LineCheck2(XYZ p1,XYZ p2, XYZ *p, XYZ move, float rotate)
{
  	int j;
	float distance;
	float olddistance=9999999.0;
	int intersecting=0;
	int firstintersecting=-1;
	XYZ point;
	p1=p1-move;
	p2=p2-move;
	if(rotate)p1=DoRotation(p1,0,-rotate,0);
	if(rotate)p2=DoRotation(p2,0,-rotate,0);
	if(sphere_line_intersection(p1.x,p1.y,p1.z,
								p2.x,p2.y,p2.z,
								boundingspherecenter.x,boundingspherecenter.y,boundingspherecenter.z,
								boundingsphereradius))
	for (j=0;j<TriangleNum;j++){
		intersecting=LineFacetd(p1,p2,vertex[Triangles[j].vertex[0]],vertex[Triangles[j].vertex[1]],vertex[Triangles[j].vertex[2]],normals[j],&point);
		if (intersecting == 0) continue;
		distance=(point.x-p1.x)*(point.x-p1.x)+(point.y-p1.y)*(point.y-p1.y)+(point.z-p1.z)*(point.z-p1.z);
		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
	}

	if(rotate)*p=DoRotation(*p,0,rotate,0);
	*p=*p+move;
	return firstintersecting;
}

int Model::LineCheck2(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
{
  	int j;
	float distance;
	float olddistance=9999999.0;
	int intersecting=0;
	int firstintersecting=-1;
	XYZ point;
	*p1=*p1-*move;
	*p2=*p2-*move;
	if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
	if(*rotate)*p2=DoRotation(*p2,0,-*rotate,0);
	if(sphere_line_intersection(p1->x,p1->y,p1->z,
								p2->x,p2->y,p2->z,
								boundingspherecenter.x,boundingspherecenter.y,boundingspherecenter.z,
								boundingsphereradius))
	for (j=0;j<TriangleNum;j++){
		intersecting = LineFacetd(*p1, *p2,
			vertex[Triangles[j].vertex[0]],
			vertex[Triangles[j].vertex[1]],
			vertex[Triangles[j].vertex[2]],
			normals[j], &point);
		if (intersecting == 0) continue;
		distance=(point.x-p1->x)*(point.x-p1->x)+(point.y-p1->y)*(point.y-p1->y)+(point.z-p1->z)*(point.z-p1->z);
		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
	}

	if(*rotate)*p=DoRotation(*p,0,*rotate,0);
	*p=*p+*move;
	return firstintersecting;
}

int Model::LineCheck3(XYZ p1,XYZ p2, XYZ *p, XYZ move, float rotate, float *d)
{
  	int j;
	float distance;
	float olddistance=9999999.0;
	int intersecting=0;
	int firstintersecting=-1;
	XYZ point;
	p1=p1-move;
	p2=p2-move;
	p1=DoRotation(p1,0,-rotate,0);
	p2=DoRotation(p2,0,-rotate,0);
	if(sphere_line_intersection(p1.x,p1.y,p1.z,
								p2.x,p2.y,p2.z,
								boundingspherecenter.x,boundingspherecenter.y,boundingspherecenter.z,
								boundingsphereradius))
	for (j=0;j<TriangleNum;j++){
		intersecting=LineFacetd(p1,p2,vertex[Triangles[j].vertex[0]],vertex[Triangles[j].vertex[1]],vertex[Triangles[j].vertex[2]],normals[j],&point);
		if (intersecting == 0) continue;
		distance=(point.x-p1.x)*(point.x-p1.x)+(point.y-p1.y)*(point.y-p1.y)+(point.z-p1.z)*(point.z-p1.z);
		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
	}
	*d=intersecting;
	*p=DoRotation(*p,0,rotate,0);
	*p=*p+move;
	return firstintersecting;
}