-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglDisplay.h
202 lines (171 loc) · 5.46 KB
/
glDisplay.h
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
#ifndef GLDISPLAY_H_INCLUDED
#define GLDISPLAY_H_INCLUDED
#define GLM_FORCE_RADIANS
#include "glm/gtc/matrix_transform.hpp"
#include "glm/glm.hpp"
#include <GL/freeglut.h>
#include <limits>
#include "Mesh.h"
#include "globalVariables.h"
int width = 1024;
int height = 768;
extern Mesh mesh;
vec3 translation(0, 0, 0);
real scale = 1;
glm::ivec2 mousePos;
int mouseButtonState = -1;
GLuint listIndex;
GLuint pickingListIndex;
glm::mat4 modelMat;
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(width/2.0f, height/2.0f, 0);
glScalef(scale, scale, scale);
glTranslatef(translation.x, translation.y, 0);
glMultMatrixf((GLfloat*)&modelMat);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1, 1, 1, 0.5);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glCallList(listIndex);
glDisable(GL_BLEND);
glColor3f(0.0f, 1.0f, 0.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCallList(listIndex);
glutSwapBuffers();
glFlush(); // Render now
}
unsigned pick()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(width/2.0f, height/2.0f, 0);
glScalef(scale, scale, scale);
glTranslatef(translation.x, translation.y, 0);
glMultMatrixf((GLfloat*)&modelMat);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glCallList(pickingListIndex);
glutSwapBuffers();
glFlush(); // Render now
unsigned char rgba[4];
glReadPixels(mousePos.x, height - 1 - mousePos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
return (rgba[0] << 16) + (rgba[1] << 8) + rgba[2];
}
void reshape(int w, int h)
{
glViewport(0, 0, width, height);
width = w;
height = h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
mousePos = glm::ivec2(x, y);
if(button == GLUT_RIGHT_BUTTON)
{
mousePos = glm::ivec2(x, y);
if(state == GLUT_DOWN)
printf("%u\n", pick());
}
if(button == 3)
scale *= 1.1;
if(button == 4)
scale *= 0.9;
mouseButtonState = state == GLUT_DOWN ? button : -1;
glutPostRedisplay();
}
void motion(int x, int y)
{
if(mouseButtonState == GLUT_LEFT)
translation += vec3(x - mousePos.x, mousePos.y - y, 0) / scale;
mousePos = glm::ivec2(x, y);
glutPostRedisplay();
}
void prepareList()
{
listIndex = glGenLists(1);
pickingListIndex = glGenLists(1);
std::vector<vec3> vList = mesh.getTriangles();
vec3 minCoord(std::numeric_limits<real>::infinity());
vec3 maxCoord(-std::numeric_limits<real>::infinity());
glNewList(pickingListIndex, GL_COMPILE);
glBegin(GL_TRIANGLES);
for(unsigned i=0; i<vList.size(); i++)
{
unsigned idx = i / 3;
glColor4ub((idx>>16)&0xff, (idx>>8)&0xff, (idx)&0xff, 255);
minCoord = glm::min(minCoord, vList[i]);
maxCoord = glm::max(maxCoord, vList[i]);
glm::vec3 v;
v.x = vList[i].x;
v.y = vList[i].y;
glVertex3fv((GLfloat*)&v);
}
glEnd();
glEndList();
glNewList(listIndex, GL_COMPILE);
glBegin(GL_TRIANGLES);
for(auto it=vList.begin(); it!=vList.end(); it++)
{
minCoord = glm::min(minCoord, *it);
maxCoord = glm::max(maxCoord, *it);
glm::vec3 v;
v.x = it->x;
v.y = it->y;
glVertex3fv((GLfloat*)&v);
}
glEnd();
glPointSize(5);
glBegin(GL_POINTS);
for(unsigned i=0; i<vList.size(); i++)
{
glColor4f(1, 0, 0, 1);
minCoord = glm::min(minCoord, vList[i]);
maxCoord = glm::max(maxCoord, vList[i]);
glm::vec3 v;
v.x = vList[i].x;
v.y = vList[i].y;
glVertex3fv((GLfloat*)&v);
}
glEnd();
glEndList();
vec3 sVec = maxCoord - minCoord;
real minSize = std::min(width, height);
real maxScale = std::max(sVec.x, sVec.y);
modelMat = glm::scale(modelMat, glm::vec3(minSize / maxScale)*0.9f);
vec3 trans = -minCoord-sVec*0.5;
modelMat = glm::translate(modelMat, glm::vec3(trans.x, trans.y, 0));
}
void glDisplay(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Triangulation"); // Create a window with the given title
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutInitWindowSize(width, height); // Set the window's initial width & height
glutReshapeWindow(width, height);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
prepareList();
glutMainLoop(); // Enter the infinitely event-processing loop
}
#endif // GLDISPLAY_H_INCLUDED