-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
239 lines (199 loc) · 5.82 KB
/
Source.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
236
237
238
239
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <cmath>
#include <fstream>
#include "Camera.h"
#include "OBJReader.h"
#include "Mesh.h"
#include "Material.h";
using namespace std;
int width = 800;
int height = 600;
float v = 0.6f;
int hv = 1.0f;
int objA = 0;
int selectedGroup = 0;
Camera *cam;
Mesh* m = new Mesh();
vector<Material*> mat;
void applyTexture() {
// Create one OpenGL texture
for (int l = 0; l < mat.size(); l++)
{
if (mat[l]->hasTexture()) {
GLuint textureID;
glGenTextures(1, &textureID);
mat[l]->textureID = textureID;
glBindTexture(GL_TEXTURE_2D, mat[l]->textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mat[l]->img->getWidth(), mat[l]->img->getHeight(), 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, mat[l]->img->getPixels());
}
}
}
void DrawObject() {
for (int i = 0; i < m->groups.size(); i++)
{
if (!m->groups[i]->visible) continue;
int selectedMaterial = -1;
for (int l = 0; l < mat.size(); l++)
{
if (mat[l]->name == m->groups[i]->material) {
selectedMaterial = l;
break;
}
}
if (selectedMaterial > -1 && mat[selectedMaterial]->hasTexture())
{
glBindTexture(GL_TEXTURE_2D, mat[selectedMaterial]->textureID);
}
if (selectedMaterial > -1) {
glMaterialfv(GL_FRONT, GL_AMBIENT, mat[selectedMaterial]->Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat[selectedMaterial]->Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat[selectedMaterial]->Ks);
glMaterialf(GL_FRONT, GL_SHININESS, mat[selectedMaterial]->Ns);
}
for (int j = 0; j < m->groups[i]->faces.size(); j++)
{
glBegin(GL_POLYGON);
for (int k = 0; k < m->groups[i]->faces[j]->refPoints.size(); k++)
{
Vertex* vx = m->allVertex[m->groups[i]->faces[j]->refPoints[k]];
if (m->groups[i]->faces[j]->normalPoints.size() > 0) {
Vertex* vn = m->allNormals[m->groups[i]->faces[j]->normalPoints[k]];
glNormal3f(vn->coords[0], vn->coords[1], vn->coords[2]);
}
if (m->groups[i]->faces[j]->textPoints.size() > 0) {
TextureMapping* vt = m->allTextures[m->groups[i]->faces[j]->textPoints[k]];
glTexCoord2f(vt->u, vt->v);
}
glVertex3f(vx->coords[0], vx->coords[1], vx->coords[2]);
}
glEnd();
}
}
}
void reshape(int w, int h) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float) w / (float) h, 1.0, 10000);
glViewport(0, 0, width = w, height = h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
}
void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawObject();
/* don't wait!
* start processing buffered OpenGL routines
*/
glutSwapBuffers ();
}
void keyboard(unsigned char key, int x, int y) {
if (key == 'w') {
int c = (cos(cam->rad) * v + cam->ex);
int l = (sin(cam->rad) * v + cam->ez);
cam->move(v);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
glutPostRedisplay();
}
if (key == 's') {
int c = (cos(cam->rad) * -v + cam->ex);
int l = (sin(cam->rad) * -v + cam->ez);
cam->move(-v);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
glutPostRedisplay();
}
if (key == 'd') {
cam->rotate(hv);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
glutPostRedisplay();
}
if (key == 'a') {
cam->rotate(-1 * hv);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
glutPostRedisplay();
}
if (key == 'u') {
cam->upDownCam(v);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
glutPostRedisplay();
}
if (key == 'j') {
cam->upDownCam(-v);
glLoadIdentity();
gluLookAt(cam->ex, cam->ey, cam->ez, cam->dx, cam->dy, cam->dz, cam->ux, cam->uy, cam->uz);
glutPostRedisplay();
}
if (key == 'm') {
selectedGroup = (selectedGroup++) % (m->groups.size() - 1);
glutPostRedisplay();
}
if (key == 'b') {
if (selectedGroup > -1) {
m->groups[selectedGroup]->changeVisibility();
glutPostRedisplay();
}
}
if (key == 'n') {
if (selectedGroup > 0) {
selectedGroup--;
glutPostRedisplay();
}
}
if (key == 'q' || key == 'Q') {
exit(0);
}
}
void init (void)
{
/* select clearing (background) color */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
cam = new Camera(0, 1, 0);
OBJReader* reader = new OBJReader();
reader->readObj("Sonic/Sonic.obj", m, mat);
//reader->readObj("Deadpool/dead 123456.obj", m, mat);
//reader->readObj("moon.obj", m, mat);
//reader->readObj("tie-intercept.obj", m, mat);
//reader->readObj("Millennium_Falcon/millenium-falcon.obj", m, mat);
applyTexture();
//reader->readObj("bb8.obj", m, mat);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize (width, height);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}