-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathobject.cpp
More file actions
342 lines (288 loc) · 8.02 KB
/
Copy pathobject.cpp
File metadata and controls
342 lines (288 loc) · 8.02 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "core.h"
#include "object.h"
Object::Object()
{
#ifdef DEBUG_LEVEL
std::cout << __FILE__ << " " << __FUNCTION__ << std::endl;
#endif
//Reset();
_shaderProg = 0;
memset(_hShaders, 0, sizeof(_hShaders) );
_shaderNum = 0;
//for rendering
_stepSize = 0.001f;
}
Object::~Object()
{
}
int Object::LoadShaderFile(const char* fileName, GLuint shader)
{
std::fstream file;
GLint compiled;
int size = 0;
char* buffer = NULL;
file.open(fileName, std::ios::in | std::ios::binary);
if(!file) {
std::cerr << "Open file " << fileName << " failed!" << std::endl;
return -1;
}
//get file size and allocate memory
file.seekg(0, std::ios::end);
size = file.tellg();
file.seekg(0, std::ios::beg);
buffer = new char[size];
file.read(buffer, size);
//load to shader
glShaderSource(shader, 1, (const GLchar **)&buffer, &size);
delete []buffer;
file.close();
return 0;
}
void Object::EnableShader()
{
CreateShaderProgram();
}
void Object::RebindShader(GLuint hVertexShader, GLuint hfragShader)
{
GLint testVal;
GLsizei maxCount = 2;
GLsizei count;
GLuint shaders[maxCount];
//get exists shaders
glGetAttachedShaders(_shaderProg, maxCount, &count, shaders);
//and detach then
for (int i = 0; i < count; i++) {
glDetachShader(_shaderProg, shaders[i]);
}
// Bind index 0 to the shader input variable "VerPos"
glBindAttribLocation(_shaderProg, 0, "VerPos");
// Bind index 1 to the shader input variable "VerClr"
glBindAttribLocation(_shaderProg, 1, "VerClr");
glAttachShader(_shaderProg, hVertexShader);
glAttachShader(_shaderProg, hfragShader);
// Attempt to link
glLinkProgram(_shaderProg);
// Make sure link worked too
glGetProgramiv(_shaderProg, GL_LINK_STATUS, &testVal);
if(testVal == GL_FALSE) {
char infoLog[1024];
glGetProgramInfoLog(_shaderProg, 1024, NULL, infoLog);
std::cerr << "The program " << _shaderProg
<< " failed to link with the following error:" << std::endl
<< infoLog << std::endl;
glDeleteProgram(_shaderProg);
exit(-1);
}
glUseProgram(_shaderProg);
}
void Object::CreateShaderProgram()
{
GLint testVal;
GLuint hReturn;
// Create the final program object, and attach the shaders
hReturn = glCreateProgram();
// All done, return our ready to use shader program
_shaderProg = hReturn;
}
void Object::RegisterShader(const char* progName, GLenum shaderType)
{
// Temporary Shader objects
GLuint hShader;
GLuint hReturn = 0;
GLint testVal;
// Create shader objects
hShader = glCreateShader(shaderType);
// Load, If fail clean up and return null
if(LoadShaderFile(progName, hShader) < 0) {
glDeleteShader(hShader);
std::cerr << "The shader at " << progName
<< " could not be found." << std::endl;
exit(-1);
}
// Compile
glCompileShader(hShader);
// Check for errors in shader
glGetShaderiv(hShader, GL_COMPILE_STATUS, &testVal);
if(testVal == GL_FALSE) {
char infoLog[1024];
glGetShaderInfoLog(hShader, 1024, NULL, infoLog);
std::cerr << "The shader at " << progName
<< " failed to compile with the following error:" << std::endl
<< infoLog << std::endl;
glDeleteShader(hShader);
exit(-1);
}
// Check for errors in shader
glGetShaderiv(hShader, GL_COMPILE_STATUS, &testVal);
if(testVal == GL_FALSE) {
char infoLog[1024];
glGetShaderInfoLog(hShader, 1024, NULL, infoLog);
std::cerr << "The shader at " << progName
<< " failed to compile with the following error:" << std::endl
<< infoLog << std::endl;
glDeleteShader(hShader);
exit(-1);
}
_hShaders[_shaderNum++] = hShader;
#if 0
// Now, we need to bind the attribute names to their specific locations
// List of attributes
va_list attributeList;
va_start(attributeList, fragmentProgName);
// Iterate over this argument list
char *szNextArg;
int iArgCount = va_arg(attributeList, int);
// Number of attributes
for(int i = 0; i < iArgCount; i++)
{
int index = va_arg(attributeList, int);
szNextArg = va_arg(attributeList, char*);
glBindAttribLocation(hReturn, index, szNextArg);
}
va_end(attributeList);
#endif
}
void Object::RegisterParentWindow(GLFWwindow* windowHandle)
{
_windowHandle = windowHandle;
}
void Object::Resize(GLFWwindow* windowHandle, int x, int y)
{
_arcball.SetWidthHeight(x, y);
}
void Object::MouseButton(GLFWwindow *window, int button,int action,int mods)
{
double mouseX, mouseY;
if(button == GLFW_MOUSE_BUTTON_LEFT) {
::glfwGetCursorPos(window, &mouseX, &mouseY);
if(action == GLFW_PRESS) {
_isLeftKeyPressed = true;
_arcball.StartRotation(mouseX, mouseY);
}
else if(action == GLFW_RELEASE) {
_isLeftKeyPressed = false;
_arcball.StopRotation();
}
}
else if(button == GLFW_MOUSE_BUTTON_MIDDLE) {
_isMiddleKeyPressed = (action == GLFW_PRESS);
}
else if(button == GLFW_MOUSE_BUTTON_RIGHT) {
#if 0
if (action == GLFW_PRESS) {
_isRightKeyPressed = true;
::glfwGetCursorPos(window, &mouseX, &mouseY);
_arcball.StartZooming(mouseX, mouseY);
}
else if(action ==GLFW_RELEASE) {
_isRightKeyPressed = false;
_arcball.StopZooming();
}
#endif
}
}
void Object::MouseMotion(GLFWwindow *window, double nx, double ny)
{
if(_isLeftKeyPressed && _isCtrlPressed) {
}
else if(_isLeftKeyPressed) {
_arcball.UpdateRotation(nx, ny);
}
#if 0
else if(_isRightKeyPressed) {
_arcball.UpdateZooming(nx, ny);
}
#endif
}
void Object::MouseScroll(GLFWwindow *window, double nx, double ny)
{
_arcball.StartZooming(0, 0);
_arcball.UpdateZooming(-ny, nx);
_arcball.StopZooming();
}
void Object::ComputeNormal(GLfloat v[3][3], GLfloat normal[])
{
float v1[3],v2[3];
static const int x = 0;
static const int y = 1;
static const int z = 2;
// Calculate two vectors from the three points
v1[x] = v[0][x] - v[1][x];
v1[y] = v[0][y] - v[1][y];
v1[z] = v[0][z] - v[1][z];
v2[x] = v[1][x] - v[2][x];
v2[y] = v[1][y] - v[2][y];
v2[z] = v[1][z] - v[2][z];
// Take the cross product of the two vectors to get
// the normal vector which will be stored in out
normal[x] = v1[y]*v2[z] - v1[z]*v2[y];
normal[y] = v1[z]*v2[x] - v1[x]*v2[z];
normal[z] = v1[x]*v2[y] - v1[y]*v2[x];
// Normalize the vector (shorten length to one)
}
void Object::Reset() {
#ifdef DEBUG_LEVEL
std::cout << __FILE__ << " " << __FUNCTION__ << std::endl;
#endif
int width, height;
glfwGetWindowSize(_windowHandle, &width, &height);
_winX = width;
_winY = height;
_arcball.SetWidthHeight(width, height);
#if 1
_depth = 3.8;
_rotX = 10;
_rotY = -20;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -_depth);
glRotatef(_rotX, 1, 0, 0);
glRotatef(_rotY, 0, 1, 0);
#endif
}
bool Object::LoadFile(char* fileName)
{
_file.open(fileName);
if (!_file) {
std::cout << "Open file: " << fileName << " failed!" << std::endl;
return false;
}
std::cout << "Open file: " << fileName << " succeed!" << std::endl;
return true;
}
//this is a test function, just draw a cube
void Object::Cube() {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
GLfloat vertices[][3] = { { -1.0f, -1.0f, -1.0f },
{ 1.0f, -1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f },
{ -1.0f, 1.0f, -1.0f }, { -1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f } }; // Vertices
int faces[][4] = { { 1, 2, 6, 5 }, { 2, 3, 7, 6 }, { 4, 5, 6, 7 },
{ 0, 4, 7, 3 }, { 0, 1, 5, 4 }, { 0, 3, 2, 1 } };
GLfloat colors[][3] = { { 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f }, { 0.0f, 0.5f, 0.5f },
{ 0.5f, 0.0f, 0.5f }, { 0.5f, 0.5f, 0.0f } };
//glBegin(GL_QUADS);
for (int i = 0; i < 6; i++) {
//glColor3fv(colors[i]);
Eigen::Vector3f nm;
Eigen::Vector3f v0(vertices[faces[i][0]][0], vertices[faces[i][0]][1], vertices[faces[i][0]][2]);
Eigen::Vector3f v1(vertices[faces[i][1]][0], vertices[faces[i][1]][1], vertices[faces[i][1]][2]);
Eigen::Vector3f v2(vertices[faces[i][2]][0], vertices[faces[i][2]][1], vertices[faces[i][2]][2]);
Eigen::Vector3f l1 = v1-v0;
Eigen::Vector3f l2 = v2-v0;
nm = l1.cross(l2);
nm.normalize();
glNormal3f(nm(0),nm(1),nm(2));
glBegin(GL_LINE_STRIP);
for (int j = 0; j < 4; j++)
glVertex3fv(vertices[faces[i][j]]);
glEnd();
}
//glEnd();
}
void Object::Show()
{
Cube();
}