/
example2.cpp
199 lines (171 loc) · 5.05 KB
/
example2.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
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGl/glu.h>
#include <GLUT/glut.h>
#elif defined __linux__ || defined _WIN32 || defined _WIN64
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include <algorithm>
#include <iostream>
#include "Shader.hpp"
#include "Light.hpp"
#include <cmath>
// shader object
Caffeina::Shader shader;
// light object
Caffeina::Light light;
// camera parameters
float cRadius = 5.0f; // our radius distance from our character
float xCam = 5;
float yCam = 5;
float zCam = 5;
float xCamRot = 0;
float yCamRot = 0;
float camAngle = 0.0;
float lastx, lasty;
void DrawGround(){
GLfloat fExtent = 10.0f;
GLfloat fStep = 1.0f;
GLfloat y = -0.4f;
GLint iLine;
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
for(iLine = -fExtent; iLine <= fExtent; iLine += fStep) {
glVertex3f(iLine, y, fExtent); // Draw Z lines
glVertex3f(iLine, y, -fExtent);
glVertex3f(fExtent, y, iLine);
glVertex3f(-fExtent, y, iLine);
}
glEnd();
}
/*void camera() {
// rotate our camera on the x-axis (left and right)
glRotatef(xCamRot, 1.0, 0.0, 0.0);
// rotate our camera on the y-axis (up and down)
glRotatef(yCamRot, 0.0, 1.0, 0.0);
// translate the screento the position of our camera
glTranslated(-xCam, -yCam, -zCam);
}
*/
void initScene() {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_SMOOTH);
glShadeModel(GL_SMOOTH);
shader.init("shader.vert", "shader.frag");
}
void draw() {
}
void renderScene() {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// turn on lights
light.turnOn();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -cRadius);
glRotatef(xCamRot,1.0,0.0,0.0);
glRotatef(yCamRot, 0.0, 1.0, 0.0);
glTranslated(-xCam, 0.0f, -zCam); //translate the screen
shader.bind();
// draw ground
DrawGround();
// draw primitives
draw();
shader.unBind();
glutSwapBuffers();
}
void reshape(int width, int height) {
// Set our viewport to the size of our window
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
// Switch to the projection matrix so that we can manipulate how our scene
glMatrixMode(GL_PROJECTION);
// Reset the projection matrix to the identity matrix
glLoadIdentity();
// Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
// Switch back to the model view matrix, so that we can start drawing shapes correctly
glMatrixMode(GL_MODELVIEW);
}
void mouseMovement(int x, int y) {
//check the difference between the current x and the last x position
int diffx = x-lastx;
//check the difference between the current y and the last y position
int diffy = y-lasty;
//set lastx to the current x position
lastx = x;
//set lasty to the current y position
lasty = y;
//set the xrot to xrot with the addition of the difference in the y position
xCamRot -= (float) diffy;
//set the xrot to yrot with the addition of the difference in the x position
yCamRot -= (float) diffx;
// Limit loking up to vertically up
if(xCamRot < 0) {
xCamRot = 0;
}
// Limit looking down to vertically down
if (xCamRot > 90) {
xCamRot = 90;
}
}
void keyHandler(unsigned char key, int x, int y) {
switch(key) {
case 'A':
case 'a':
float yrotrad3;
yrotrad3 = (yCamRot / 180 * 3.141592654f);
xCam -= float(cos(yrotrad3)) * 0.5;
zCam -= float(sin(yrotrad3)) * 0.5;
break;
case 'D':
case 'd':
float yrotrad2;
yrotrad2 = (yCamRot / 180 * 3.141592654f);
xCam += float(cos(yrotrad2)) * 0.5;
zCam += float(sin(yrotrad2)) * 0.5;
break;
case 's':
case 'S':
float xrotrad1, yrotrad1;
yrotrad1 = (yCamRot / 180 * 3.141592654f);
xrotrad1 = (xCamRot / 180 * 3.141592654f);
xCam -= float(sin(yrotrad1)) * 0.5;
zCam += float(cos(yrotrad1)) * 0.5;
yCam += float(sin(xrotrad1)) * 0.5;
break;
case 'W':
case 'w':
float xrotrad, yrotrad;
yrotrad = (yCamRot / 180 * 3.141592654f);
xrotrad = (xCamRot / 180 * 3.141592654f);
xCam += float(sin(yrotrad)) * 0.5;
zCam -= float(cos(yrotrad)) * 0.5;
yCam -= float(sin(xrotrad)) * 0.5;
break;
case '+':
cRadius -= 0.5f;
break;
case '-':
cRadius += 0.5f;
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(650, 500);
glutInitWindowPosition(400, 400);
glutCreateWindow("OpenGL");
initScene();
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyHandler);
glutMotionFunc(mouseMovement);
//glutSpecialFunc(specialKeyHandler);
glutMainLoop();
return 0;
}