-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasstcommon.h
More file actions
179 lines (143 loc) · 5.31 KB
/
asstcommon.h
File metadata and controls
179 lines (143 loc) · 5.31 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
#ifndef ASSTCOMMON_H
#define ASSTCOMMON_H
#include <vector>
#include <memory>
#include <stdexcept>
#if __GNUG__
# include <tr1/memory>
#endif
#include "geometry.h"
#include "glsupport.h"
#include "scenegraph.h"
extern const bool g_Gl2Compatible;
//-------------------
// ShaderState
//-------------------
//
struct ShaderState {
GlProgram program;
// Handles to uniform variables
GLint h_uLight, h_uLight2;
GLint h_uProjMatrix;
GLint h_uModelViewMatrix;
GLint h_uNormalMatrix;
GLint h_uColor;
GLint h_uIdColor;
// Handles to vertex attributes
GLint h_aPosition;
GLint h_aNormal;
ShaderState(const char* vsfn, const char* fsfn) {
readAndCompileShader(program, vsfn, fsfn);
const GLuint h = program; // short hand
// Retrieve handles to uniform variables
h_uLight = safe_glGetUniformLocation(h, "uLight");
h_uLight2 = safe_glGetUniformLocation(h, "uLight2");
h_uProjMatrix = safe_glGetUniformLocation(h, "uProjMatrix");
h_uModelViewMatrix = safe_glGetUniformLocation(h, "uModelViewMatrix");
h_uNormalMatrix = safe_glGetUniformLocation(h, "uNormalMatrix");
h_uColor = safe_glGetUniformLocation(h, "uColor");
h_uIdColor = safe_glGetUniformLocation(h, "uIdColor");
// Retrieve handles to vertex attributes
h_aPosition = safe_glGetAttribLocation(h, "aPosition");
h_aNormal = safe_glGetAttribLocation(h, "aNormal");
if (!g_Gl2Compatible)
glBindFragDataLocation(h, 0, "fragColor");
checkGlErrors();
}
};
// takes a projection matrix and send to the the shaders
inline void sendProjectionMatrix(const ShaderState& curSS, const Matrix4& projMatrix) {
GLfloat glmatrix[16];
projMatrix.writeToColumnMajorMatrix(glmatrix); // send projection matrix
safe_glUniformMatrix4fv(curSS.h_uProjMatrix, glmatrix);
}
// takes MVM and its normal matrix to the shaders
inline void sendModelViewNormalMatrix(const ShaderState& curSS, const Matrix4& MVM, const Matrix4& NMVM) {
GLfloat glmatrix[16];
MVM.writeToColumnMajorMatrix(glmatrix); // send MVM
safe_glUniformMatrix4fv(curSS.h_uModelViewMatrix, glmatrix);
NMVM.writeToColumnMajorMatrix(glmatrix); // send NMVM
safe_glUniformMatrix4fv(curSS.h_uNormalMatrix, glmatrix);
}
//------------------------------------
// Geometry
//------------------------------------
// Macro used to obtain relative offset of a field within a struct
#define FIELD_OFFSET(StructType, field) &(((StructType *)0)->field)
// A vertex with floating point position and normal
struct VertexPN {
Cvec3f p, n;
VertexPN() {}
VertexPN(float x, float y, float z,
float nx, float ny, float nz)
: p(x,y,z), n(nx, ny, nz)
{}
// Define copy constructor and assignment operator from GenericVertex so we can
// use make* functions from geometry.h
VertexPN(const GenericVertex& v) {
*this = v;
}
VertexPN& operator = (const GenericVertex& v) {
p = v.pos;
n = v.normal;
return *this;
}
};
struct Geometry {
GlBufferObject vbo, ibo;
int vboLen, iboLen;
Geometry(VertexPN *vtx, unsigned short *idx, int vboLen, int iboLen) {
this->vboLen = vboLen;
this->iboLen = iboLen;
// Now create the VBO and IBO
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexPN) * vboLen, vtx, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * iboLen, idx, GL_STATIC_DRAW);
}
void draw(const ShaderState& curSS) {
// Enable the attributes used by our shader
safe_glEnableVertexAttribArray(curSS.h_aPosition);
safe_glEnableVertexAttribArray(curSS.h_aNormal);
// bind vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
safe_glVertexAttribPointer(curSS.h_aPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPN), FIELD_OFFSET(VertexPN, p));
safe_glVertexAttribPointer(curSS.h_aNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexPN), FIELD_OFFSET(VertexPN, n));
// bind ibo
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
// draw!
glDrawElements(GL_TRIANGLES, iboLen, GL_UNSIGNED_SHORT, 0);
// Disable the attributes used by our shader
safe_glDisableVertexAttribArray(curSS.h_aPosition);
safe_glDisableVertexAttribArray(curSS.h_aNormal);
}
};
//---------------------------------------------------
// Scene graph shape node that references a geometry
//---------------------------------------------------
class SgGeometryShapeNode : public SgShapeNode {
std::tr1::shared_ptr<Geometry> geometry_;
Matrix4 affineMatrix_;
Cvec3 color_;
public:
SgGeometryShapeNode(std::tr1::shared_ptr<Geometry> geometry,
const Cvec3& color,
const Cvec3& translation = Cvec3(0, 0, 0),
const Cvec3& eulerAngles = Cvec3(0, 0, 0),
const Cvec3& scales = Cvec3(1, 1, 1))
: geometry_(geometry)
, color_(color)
, affineMatrix_(Matrix4::makeTranslation(translation) *
Matrix4::makeXRotation(eulerAngles[0]) *
Matrix4::makeYRotation(eulerAngles[1]) *
Matrix4::makeZRotation(eulerAngles[2]) *
Matrix4::makeScale(scales)) {}
virtual Matrix4 getAffineMatrix() {
return affineMatrix_;
}
virtual void draw(const ShaderState& curSS) {
safe_glUniform3f(curSS.h_uColor, color_[0], color_[1], color_[2]);
geometry_->draw(curSS);
}
};
#endif