-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_test.cpp
235 lines (187 loc) · 7.22 KB
/
gl_test.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "lib/pipeline.h"
#include "lib/camera.h"
#include "lib/test_utils.h"
extern mv **e, **be, **w, **sw; //The e's, bar e's, w's, and w stars
extern double **coords; //Used for construction of the above arrays and for deallocation
int ORDER = 12;
#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 768
GLuint VBO;
GLuint IBO;
Vector3f Vertices[4];
Vector3f b_Vertices[4];
Camera GameCamera;
static const char* pVS = " \n\
#version 330 \n\
\n\
layout (location = 0) in vec3 Position; \n\
\n\
\n\
out vec4 Color; \n\
\n\
void main() \n\
{ \n\
gl_Position = vec4(Position.x, Position.y, Position.z, Position.z); \n\
Color = vec4(clamp(Position, 0.0, 1.0), 1.0); \n\
}";
static const char* pFS = " \n\
#version 330 \n\
\n\
in vec4 Color; \n\
\n\
out vec4 FragColor; \n\
\n\
void main() \n\
{ \n\
FragColor = Color; \n\
}";
static void bindBuffer(){
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
static void updateVertices(Pipeline p) {
Matrix4f mat = *p.GetTrans();
for(int i=0;i<4;i++){
// cout<<endl<<i<<" "<<b_Vertices[i].x<<" "<<b_Vertices[i].y<<" "<<b_Vertices[i].z<<endl;
mat.updateVector(Vertices[i], b_Vertices[i]);
cout<<i<<" "<<Vertices[i].x<<" "<<Vertices[i].y<<" "<<Vertices[i].z<<endl;
//setPseudoPerspective(Vertices[i]);
// cout<<i<<" "<<Vertices[i].x<<" "<<Vertices[i].y<<" "<<Vertices[i].z<<endl;
}
}
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
static float Scale = 0.0f;
Scale += 0.09f;
Pipeline p;
p.Rotate(0.0f, Scale, 0.0f);
p.WorldPos(0.0f, 0.0f, 0.0f);
p.SetCamera(GameCamera.GetPos(), GameCamera.GetTarget(), GameCamera.GetUp());
p.SetPerspectiveProj(60.0f, WINDOW_WIDTH, WINDOW_HEIGHT, 1.0f, 100.0f);
updateVertices(p);
bindBuffer();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void SpecialKeyboardCB(int Key, int x, int y)
{
GameCamera.OnKeyboard(Key);
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
glutIdleFunc(RenderSceneCB);
glutSpecialFunc(SpecialKeyboardCB);
}
static void CreateVertexBuffer()
{
for(int i=0;i<4;i++){
Vertices[i] = Vector3f();
}
/*
b_Vertices[0] = Vector3f(-0.649519,-0.866025,1.34646);
b_Vertices[1] = Vector3f(0,-0.866025, 0.734343);
b_Vertices[2] = Vector3f(0.649519, -0.866025, 1.44848);
b_Vertices[3] = Vector3f(0,0.866025,1.0404);
*/
b_Vertices[0] = Vector3f(-0.5f, -0.5f, 0.3f);
b_Vertices[1] = Vector3f(0.0f, -0.5f, -0.3f);
b_Vertices[2] = Vector3f(0.5f, -0.5f, 0.4f);
b_Vertices[3] = Vector3f(0.0f, 0.5f, 0.0f);
}
static void CreateIndexBuffer()
{
unsigned int Indices[] = { 0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2 };
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
{
GLuint ShaderObj = glCreateShader(ShaderType);
if (ShaderObj == 0) {
fprintf(stderr, "Error creating shader type %d\n", ShaderType);
exit(0);
}
const GLchar* p[1];
p[0] = pShaderText;
GLint Lengths[1];
Lengths[0]= strlen(pShaderText);
glShaderSource(ShaderObj, 1, p, Lengths);
glCompileShader(ShaderObj);
GLint success;
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
exit(1);
}
glAttachShader(ShaderProgram, ShaderObj);
}
static void CompileShaders()
{
GLuint ShaderProgram = glCreateProgram();
if (ShaderProgram == 0) {
fprintf(stderr, "Error creating shader program\n");
exit(1);
}
AddShader(ShaderProgram, pVS, GL_VERTEX_SHADER);
AddShader(ShaderProgram, pFS, GL_FRAGMENT_SHADER);
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
glLinkProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if (Success == 0) {
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
exit(1);
}
glValidateProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
if (!Success) {
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
exit(1);
}
glUseProgram(ShaderProgram);
}
int main(int argc, char** argv)
{
init_test();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Clifford Algebra Test");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
CreateIndexBuffer();
CompileShaders();
glutMainLoop();
cleanup();
return 0;
}