-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.cpp
260 lines (216 loc) · 7.14 KB
/
Object.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// Created by hasee on 5/2/2018.
//
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include "Object.h"
#include "Camera.h"
#include "Lighting.h"
#include "Textures.h"
// How to calculate an offset into the vertex buffer
#define BUFFER_OFFSET( i ) ((char *)NULL + (i))
///
// Default constructor
///
Object::Object() {
}
///
// Constructor
//
// @param program - the ID of an OpenGL (GLSL) shader program to which
// parameter values are to be sent
// @param C - the Canvas to use
///
Object::Object( GLuint program, Canvas &C ) {
this->bufferSet.createBuffers( C );
this->program = program;
this->texture = 0;
this->Model = mat4( 1.0f );
}
///
// Draw the object.
///
void Object::drawObject() {
glUseProgram( program );
// set up the buffer
setUpBuffer();
// set up the matrices
setUpMatrix();
// set up the surface material
setUpMaterial();
// set up the lighting properties
setUpLight( program );
// the object has texture
if( texture != 0 ) {
// set up the texture
setUpTexture( texture );
}
// draw it
glDrawElements( GL_TRIANGLES, bufferSet.numElements,
GL_UNSIGNED_INT, ( void * ) 0 );
}
///
// Set up the buffer.
///
void Object::setUpBuffer() {
// bind the buffers
glBindBuffer( GL_ARRAY_BUFFER, bufferSet.vbuffer );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, bufferSet.ebuffer );
// set up the vertex attribute variables
GLint vPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET( 0 ) );
int offset = bufferSet.vSize;
if( bufferSet.cSize ) { // color data
GLint vColor = glGetAttribLocation( program, "vColor" );
glEnableVertexAttribArray( vColor );
glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET( offset ) );
offset += bufferSet.cSize;
}
if( bufferSet.nSize ) { // normal data
GLint vNormal = glGetAttribLocation( program, "vNormal" );
glEnableVertexAttribArray( vNormal );
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET( offset ) );
offset += bufferSet.nSize;
}
if( bufferSet.tSize ) { // texture coordinate data
GLint vTexCoord = glGetAttribLocation( program, "vTexCoord" );
glEnableVertexAttribArray( vTexCoord );
glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET( offset ) );
offset += bufferSet.tSize;
}
}
///
// Set up the model, view, projection matrix.
///
void Object::setUpMatrix() {
extern Camera camera[3];
extern int currentCamera;
// the viewing matrix of current camera
mat4 View = camera[ currentCamera ].getViewMat();
// the projection matrix of current camera
mat4 Projection = camera[ currentCamera ].getProjectionMat();
// the normal matrix
mat3 Normal = mat3( inverseTranspose( View * Model ) );
// set up the model matrix
glUniformMatrix4fv( glGetUniformLocation( program, "modelMat" ),
1, GL_FALSE, value_ptr( Model ) );
// set up the viewing matrix
glUniformMatrix4fv( glGetUniformLocation( program, "viewMat" ),
1, GL_FALSE, value_ptr( View ) );
// set up the projection matrix
glUniformMatrix4fv( glGetUniformLocation( program, "projectionMat" ),
1, GL_FALSE, value_ptr( Projection ) );
// set up the normal matrix
glUniformMatrix3fv( glGetUniformLocation( program, "normalMat" ),
1, GL_FALSE, value_ptr( Normal ) );
}
///
// Set up the material properties.
///
void Object::setUpMaterial() {
glUniform4fv( glGetUniformLocation( program, "material.ambient" ), 1,
value_ptr( material.ambientColor ) );
glUniform4fv( glGetUniformLocation( program, "material.diffuse" ), 1,
value_ptr( material.diffuseColor ) );
glUniform4fv( glGetUniformLocation( program, "material.specular" ), 1,
value_ptr( material.specularColor ) );
glUniform1f( glGetUniformLocation( program, "material.ka" ),
material.ka );
glUniform1f( glGetUniformLocation( program, "material.kd" ),
material.kd );
glUniform1f( glGetUniformLocation( program, "material.ks" ),
material.ks );
glUniform1f( glGetUniformLocation( program, "material.shininess" ),
material.shininess );
}
///
// Reset the model transformation of the object.
///
void Object::reset() {
Model = mat4( 1.0f );
}
///
// This function perfrom a scaling operation.
//
// @param scaleX - amount of scaling along the x-axis
// @param scaleY - amount of scaling along the y-axis
// @param scaleZ - amount of scaling along the z-axis
///
void Object::scale( GLfloat scaleX, GLfloat scaleY, GLfloat scaleZ ) {
mat4 Scale = mat4( 1.0f );
Scale[ 0 ][ 0 ] = scaleX;
Scale[ 1 ][ 1 ] = scaleY;
Scale[ 2 ][ 2 ] = scaleZ;
Model = Scale * Model;
}
///
// This function perfrom a translation operation.
//
// @param translateX - amount of translation along the x axis
// @param translateY - amount of translation along the y axis
// @param translateZ - amount of translation along the z axis
///
void Object::translate( GLfloat translateX, GLfloat translateY,
GLfloat translateZ ) {
mat4 Translate = mat4( 1.0f );
Translate[ 3 ][ 0 ] = translateX;
Translate[ 3 ][ 1 ] = translateY;
Translate[ 3 ][ 2 ] = translateZ;
Model = Translate * Model;
}
///
// This function perform of rotation around the x axis.
//
// @param rotateX - angle of rotation around the x-axis, in degrees
///
void Object::rotateX( GLfloat rX ) {
// radians of the degrees
GLfloat rad = rX * 3.14159265f / 180.0f;
GLfloat sin = glm::sin( rad );
GLfloat cos = glm::cos( rad );
mat4 RotateX = mat4( 1.0f );
RotateX[ 1 ][ 1 ] = cos;
RotateX[ 1 ][ 2 ] = sin;
RotateX[ 2 ][ 1 ] = -sin;
RotateX[ 2 ][ 2 ] = cos;
Model = RotateX * Model;
}
///
// This function perform a rotation around the y axis.
//
// @param rotateY - angle of rotation around the y-axis, in degrees
///
void Object::rotateY( GLfloat rY ) {
// radians of the degrees
GLfloat rad = rY * 3.14159265f / 180.0f;
GLfloat sin = glm::sin( rad );
GLfloat cos = glm::cos( rad );
mat4 RotateY = mat4( 1.0f );
RotateY[ 0 ][ 0 ] = cos;
RotateY[ 0 ][ 2 ] = -sin;
RotateY[ 2 ][ 0 ] = sin;
RotateY[ 2 ][ 2 ] = cos;
Model = RotateY * Model;
}
///
// This function perform a rotation around the z axis.
//
// @param rotateZ - angle of rotation around the z-axis, in degrees
///
void Object::rotateZ( GLfloat rZ ) {
// radians of the degrees
GLfloat rad = rZ * 3.14159265f / 180.0f;
GLfloat sin = glm::sin( rad );
GLfloat cos = glm::cos( rad );
mat4 RotateZ = mat4( 1.0f );
RotateZ[ 0 ][ 0 ] = cos;
RotateZ[ 0 ][ 1 ] = sin;
RotateZ[ 1 ][ 0 ] = -sin;
RotateZ[ 1 ][ 1 ] = cos;
Model = RotateZ * Model;
}