/
multmodel.cpp
183 lines (165 loc) · 3.53 KB
/
multmodel.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
#include "Angel.h"
#include "model.h"
#include "square.h"
#include "tri.h"
Square* s = new Square();
Tri* t = new Tri();
const int
DRAW_BOTH_SQUARE_FIRST = 1,
DRAW_BOTH_TRI_FIRST = 2,
DRAW_ONLY_SQUARE = 3,
DRAW_ONLY_TRIANGLE = 4;
int drawMethod = DRAW_BOTH_SQUARE_FIRST;
//{{{ printError()
void printError() {
GLenum err = glGetError();
switch(err) {
case GL_NO_ERROR:
break;
case GL_INVALID_ENUM:
printf("gl error - invalid enum\n");
break;
case GL_INVALID_VALUE:
printf("gl error - invalid value\n");
break;
case GL_INVALID_OPERATION:
printf("gl error - invalid operation\n");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
printf("gl error - invalid framebuffer operation\n");
break;
case GL_OUT_OF_MEMORY:
printf("gl error - out of memory\n");
break;
}
}
//}}}
//{{{1 Color Functions
/**
* Converts 1 character hex to int
*
* precondition: cHex element of [0-9A-Ea-e]
*/
int hexCharToInt(char hex) {
int sub = 48;
if(hex >= 'A') {
sub = 55;
}
if(hex >= 'a') {
sub = 87;
}
return hex-sub;
}
/**
* Converts 2 character hex codes to a float from [0,1]
*/
int hexToInt(char* hex) {
return hexCharToInt(hex[0])*16 + hexCharToInt(hex[1]);
}
/**
* Converts 6 character hex codes to vec3
*/
vec3 hexToVec3(char* hex) {
return vec3(
((float)hexToInt(hex))/256.0,
((float)hexToInt(hex+2))/256.0,
((float)hexToInt(hex+4))/256.0);
}
vec3 randomColor() {
return vec3(
(float)rand()/(float)RAND_MAX,
(float)rand()/(float)RAND_MAX,
(float)rand()/(float)RAND_MAX);
}
//}}}1
//{{{1 display()
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw arrays
printf("drawing\n");
switch(drawMethod) {
case DRAW_BOTH_SQUARE_FIRST:
s->render();
t->render();
break;
case DRAW_BOTH_TRI_FIRST:
t->render();
s->render();
break;
case DRAW_ONLY_SQUARE:
s->render();
break;
case DRAW_ONLY_TRIANGLE:
t->render();
break;
}
printError();
glutSwapBuffers();
printf("swapped buffers\n");
}
//}}}1
//{{{1 Init Funciton
void init() {
//init models
s->init();
t->init();
//rest
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0); // background color
}
//}}}1
//{{{1 keyPressed
void keyPressed(unsigned char key, int x, int y) {
switch(key) {
case 'q':
exit(EXIT_SUCCESS);
break;
case 'r':
glutPostRedisplay();
break;
case '1':
drawMethod = DRAW_BOTH_SQUARE_FIRST;
glutPostRedisplay();
break;
case '2':
drawMethod = DRAW_BOTH_TRI_FIRST;
glutPostRedisplay();
break;
case '3':
drawMethod = DRAW_ONLY_SQUARE;
glutPostRedisplay();
break;
case '4':
drawMethod = DRAW_ONLY_TRIANGLE;
glutPostRedisplay();
break;
}
}
//}}}1
//{{{1 reshape()
void reshape(int w, int h) {
glViewport(0,0,w,h);
GLfloat aspect = GLfloat(w)/h;
mat4 proj = Ortho(-1, 1, -1, 1, -1, 1);
s->setProjection(proj);
s->setModelView(mat4(1));
t->setProjection(proj);
t->setModelView(mat4(1));
}
//}}}1
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(512, 512);
//TODO change window name
glutCreateWindow("main");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyPressed);
//glutMouseFunc(mouse);
//glutMotionFuncion(motion);
//glutIdleFunc(idle);
glutMainLoop();
return 0;
}