-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
122 lines (96 loc) · 2.53 KB
/
Copy pathmain.cpp
File metadata and controls
122 lines (96 loc) · 2.53 KB
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
#include <math.h>
#include "graphics/shader.h"
#include "graphics/window.h"
#include "math/vec3.h"
#include "math/triangle.h"
#include "math/mat3.h"
const GLchar *vertex_src = R"glsl(
#version 330 core
layout(location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
)glsl";
const GLchar *fragment_src = R"glsl(
#version 330 core
layout(location = 0) out vec4 color;
void main() {
color = vec4(1.0, 1.0, 1.0, 1.0);
}
)glsl";
int main(void)
{
Window window(450, 450, "3D Viewer");
Shader program(vertex_src, fragment_src);
glBindFragDataLocation(program.program, 0, "color");
program.bind();
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
Triangle triangles[4] = {
Triangle(
Vec3(0.5f, 0.5f, 0.5f),
Vec3(-0.5f,-0.5f, 0.5f),
Vec3(-0.5f, 0.5f, -0.5f)),
Triangle(
Vec3(0.5f, 0.5f, 0.5f),
Vec3(-0.5f,-0.5f, 0.5f),
Vec3(0.5f,-0.5f, -0.5f)),
Triangle(
Vec3(-0.5f, 0.5f, -0.5f),
Vec3(0.5f,-0.5f, -0.5f),
Vec3(0.5f, 0.5f, 0.5f)),
Triangle(
Vec3(-0.5f, 0.5f, -0.5f),
Vec3(0.5f,-0.5f, -0.5f),
Vec3(-0.5f,-0.5f, 0.5f))
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
GLuint attrib_pos = glGetAttribLocation(program.program, "position");
glVertexAttribPointer(attrib_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attrib_pos);
double heading = 0.0, pitch = 0.0;
while (!glfwWindowShouldClose(window.window)) {
pitch += .00007;
heading -= .00007;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
double hvals[] =
{
cos(heading), 0.0, sin(heading),
0.0, 1.0, 0.0,
-sin(heading), 0.0, cos(heading)
};
Mat3 ht(hvals);
double pvals[] =
{
1.0, 0.0, 0.0,
0.0, cos(pitch), sin(pitch),
0.0, -sin(pitch), cos(pitch)
};
Mat3 pt(pvals);
Mat3 transform = pt * ht;
float vboBuffer[24];
for (int i = 0; i < 4; i++) {
Vec3 v_1 = transform.transform(triangles[i].x);
Vec3 v_2 = transform.transform(triangles[i].y);
Vec3 v_3 = transform.transform(triangles[i].z);
vboBuffer[i * 6 + 0] = v_1.x;
vboBuffer[i * 6 + 1] = v_1.y;
vboBuffer[i * 6 + 2] = v_2.x;
vboBuffer[i * 6 + 3] = v_2.y;
vboBuffer[i * 6 + 4] = v_3.x;
vboBuffer[i * 6 + 5] = v_3.y;
}
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), vboBuffer, GL_STATIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, 24 / 3 + 1);
glfwSwapBuffers(window.window);
}
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
return 0;
}