aboutsummaryrefslogtreecommitdiffstats
path: root/src/game/objects.cpp
blob: a92a83b25fb16a7869d9b3ce60dffadfc491ca0b (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>

#include "../graphics/Mesh.hpp"
#include "../graphics/Vertex.hpp"
#include "objects.hpp"

using namespace glm;

const int cube_num_v = 8;
Vertex cube_vertices[cube_num_v] = {
	// first 4 - up edge
	{ vec3(-1.0f, -1.0f, -1.0f), vec2(0.0f, 0.0f) },
	{ vec3(1.0f, -1.0f, -1.0f), vec2(0.0f, 0.0f) },
	{ vec3(1.0f, 1.0f, -1.0f), vec2(0.0f, 0.0f) },
	{ vec3(-1.0f, 1.0f, -1.0f), vec2(0.0f, 0.0f) },
	{ vec3(1.0f, 1.0f, 1.0f), vec2(0.0f, 0.0f) },
	{ vec3(-1.0f, 1.0f, 1.0f), vec2(0.0f, 0.0f) },
	{ vec3(-1.0f, -1.0f, 1.0f), vec2(0.0f, 0.0f) },
	{ vec3(1.0f, -1.0f, 1.0f), vec2(0.0f, 0.0f) }
};

const int cube_num_i = 12 * 3;
GLuint cube_indices[cube_num_i] = {
	0, 1, 2, // up edge
 	0, 2, 3,
	2, 3, 4, // far edge
	3, 4, 5,
	0, 3, 5, // left edge
	0, 5, 6,
	0, 6, 7, // near edge
	0, 1, 7,
	1, 4, 7, // right edge
	1, 2, 4,
	4, 5, 6, // down edge
	4, 6, 7
};

Mesh *form_cube()
{
	return new Mesh(GL_TRIANGLES, cube_vertices, cube_num_v, cube_indices, cube_num_i);
}

Mesh *form_sphere(int longitude_count, int latitude_count)
{
	/* The points on the sphere with radius r > 0 and center (x0, y0, z0)...
		...can be parameterized via:
	{
		x = x0 + R * sin(tetta) * cos(phi)
		y = y0 + R * sin(tetta) * sin(phi)
		z = z0 + R * cos(tetta)
		tetta : [0, PI];  phi : [0, 2PI]
	}
	or:
	{
		x = x0 + R * cos(phi) * cos(tetta)
		y = y0 + R * cos(phi) * sin(tetta)
		z = z0 + R * sin(phi)
	}
	our x0, y0, z0 = 0 */
	int i, j, idx, k1, k2;
	float x, y, z;
	float s, t;
	float radius = 1.0;

	// sectors
	float longitude_step = pi<float>() / longitude_count;
	float longitude_angle;

	// stacks
	float latitude_step = 2 * pi<float>() / latitude_count;
	float latitude_angle;

	int sphere_num_v = (longitude_count + 1) * (latitude_count + 1);
	Vertex *sphere_vertices = new Vertex[sphere_num_v];
	for(i = 0, idx = 0; i <= longitude_count; i++) {
		longitude_angle = i * longitude_step;
		z = radius * cos(longitude_angle);

		for(j = 0; j <= latitude_count; j++) {
			latitude_angle = j * latitude_step;

			x = radius * sinf(longitude_angle) * cosf(latitude_angle);
			y = radius * sinf(longitude_angle) * sinf(latitude_angle);
			sphere_vertices[idx].pos_coords = glm::vec3(x, y, z);

			s = (float)j / longitude_count;
			t = (float)i / latitude_count;
			sphere_vertices[idx].tex_coords = glm::vec2(s, t);
			idx++;
		}
	}

	int sphere_num_i = (longitude_count - 1) * latitude_count * 6;
	GLuint *sphere_indices = new GLuint[sphere_num_i];
	for(i = 0, idx = 0; i < longitude_count; i++) {
		k1 = i * (latitude_count+1);
		k2 = k1 + (latitude_count+1);

		for(j = 0; j < latitude_count; j++, k1++, k2++) {
			if(i != 0)
			{
				sphere_indices[idx++] = k1;
				sphere_indices[idx++] = k2;
				sphere_indices[idx++] = k1 + 1;
			}
			if(i != (longitude_count-1))
			{
				sphere_indices[idx++] = k1 + 1;
				sphere_indices[idx++] = k2;
				sphere_indices[idx++] = k2 + 1;
			}
		}
	}

	Mesh *sphere = new Mesh(GL_TRIANGLES, sphere_vertices, sphere_num_v,
		sphere_indices, sphere_num_i);

	delete[] sphere_vertices;
	delete[] sphere_indices;
	return sphere;
}

Mesh *form_circle(int angle_count)
{
	int i, idx, idy;
	float x, y, z;
	float radius = 1.0f;
	float angle_step = 2 * pi<float>() / angle_count;
	float angle;

	Vertex *circle_vertices = new Vertex[angle_count];
	GLuint *circle_indices = new GLuint[angle_count * 2];
	for(i = 0, idx = 0, idy = 0; i < angle_count; i++) {
		angle = i * angle_step;
		x = radius * cosf(angle);
		y = 0;
		z = radius * sinf(angle);
		circle_vertices[idx].pos_coords = glm::vec3(x, y, z);
		circle_vertices[idx].tex_coords = glm::vec2(0, 0);
		idx++;

		if(i != 0) {
			circle_indices[idy++] = i-1;
			circle_indices[idy++] = i;
		}
	}
	circle_indices[idy++] = i-1;
	circle_indices[idy] = 0;

	Mesh *circle = new Mesh(GL_LINES, circle_vertices, angle_count,
		circle_indices, angle_count * 2);

	delete[] circle_vertices;
	delete[] circle_indices;
	return circle;
}