aboutsummaryrefslogtreecommitdiff
path: root/src/Models.cpp
blob: 63cf26a41e430f7c59645487df16b50854031323 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <algorithm>
#include <cmath>

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

extern bool visions;

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

	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,
			sqrlen(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();
}

void Model::draw()
{
	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]);
	if (visions)
		glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
	else
		glColorPointer(3, GL_FLOAT, 9*sizeof(GLfloat), &vArray[6]);
	glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
}

void Model::draw(float r, float g, float b)
{
	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]);
	glColor4f(r, g, b, 1.0f);
	glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
}

int Model::LineCheck(XYZ p1, XYZ p2, XYZ *p)
{
	int result = -1;
	if (segCrossSphere(p1, p2, boundingspherecenter, boundingsphereradius)) {
		float olddistance = 9999999.0;
		for (int j = 0; j < TriangleNum; ++j) {
			XYZ point;
			if (!segCrossTrigon(p1, p2,
			                    vertex + Triangles[j].vertex[0],
			                    vertex + Triangles[j].vertex[1],
			                    vertex + Triangles[j].vertex[2],
			                    normals + j, &point))
				continue;
			float distance = sqrlen(point - p1);
			if (distance < olddistance || result == -1) {
				olddistance = distance;
				result = j;
				*p = point;
			}
		}
	}
	return result;
}

int Model::LineCheck2(XYZ p1, XYZ p2, XYZ *p, XYZ move, float deg_y)
{
	int result = this->LineCheck(rotate(p1 - move, 0, -deg_y, 0),
		rotate(p2 - move, 0, -deg_y, 0), p);
	*p = rotate(*p, 0, deg_y, 0) + move;
	return result;
}