-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.cpp
126 lines (104 loc) · 2.28 KB
/
renderer.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
#include "renderer.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <GLUT/glut.h>
using namespace std;
Renderer* Renderer::currentInstance;
Renderer::Renderer()
{
mMesh = NULL;
currentInstance = this;
}
void Renderer::init(int *argc, char *argv[])
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(800, 800);
glutCreateWindow("OBJRenderer");
}
void Renderer::render(OBJMesh *mesh)
{
mMesh = mesh;
//update display list from the mesh
updateDisplayList();
//wireframe mode
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
//register glut functions
glutKeyboardFunc(&Renderer::static_keyb);
glutDisplayFunc(&Renderer::static_loop);
//launch display loop
glutMainLoop();
}
void Renderer::updateDisplayList()
{
//fetch updated list from the mesh
displayList = mMesh->getTriangles();
//sort
std::sort(displayList.begin(), displayList.end());
}
void Renderer::static_keyb(unsigned char key, int x, int y)
{
currentInstance->keyb(key, x, y);
}
void Renderer::keyb(unsigned char key, int x, int y)
{
if (key == 's') //xminus
{
rotateDisplayList(XMINUS);
}
else if (key == 'w') //xplus
{
rotateDisplayList(XPLUS);
}
else if (key == 'a') //yminus
{
rotateDisplayList(YMINUS);
}
else if (key == 'd') //yplus
{
rotateDisplayList(YPLUS);
}
else if (key == 27) //quit
{
exit(0);
}
}
void Renderer::rotateDisplayList(enum DIRECTION d)
{
//rotate mesh
mMesh->rotate(d);
//update display list form the mesh
updateDisplayList();
//post redisplay
glutPostRedisplay();
}
void Renderer::static_loop(void)
{
currentInstance->loop();
}
void Renderer::loop(void)
{
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
debugDrawTriangles();
glutSwapBuffers();
}
void Renderer::debugDrawTriangles()
{
std::vector<OBJMeshTriangle>::iterator i;
for (i = displayList.begin(); i != displayList.end(); ++i)
{
glBegin(GL_TRIANGLES);
glColor3f(abs((*i).n1.k), 0, 0);
glVertex3f((*i).v1.x, (*i).v1.y, (*i).v1.z);
glColor3f(abs((*i).n2.k), 0, 0);
glVertex3f((*i).v2.x, (*i).v2.y, (*i).v2.z);
glColor3f(abs((*i).n3.k), 0, 0);
glVertex3f((*i).v3.x, (*i).v3.y, (*i).v3.z);
glEnd();
}
}
void Renderer::drawTriangles()
{
}