-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderable.cpp
More file actions
146 lines (120 loc) · 3.28 KB
/
Renderable.cpp
File metadata and controls
146 lines (120 loc) · 3.28 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
#include <vector>
#include "Renderable.h"
#include "Matrix.h"
#include "ShaderProgram.h"
Renderable::Renderable()
{
vao.init();
vbo.init();
}
Renderable::~Renderable()
{
}
bool Renderable::uploaded()
{
return is_uploaded;
}
void Renderable::upload()
{
}
void Renderable::bind()
{
vao.bind();
}
void Renderable::draw()
{
if( vao) {
if( ibo) {
glDrawElements( GL_TRIANGLES, numVertices, GL_UNSIGNED_INT, NULL);
}
else {
// If we don't have an index buffer object, just assume we can use the vertices in order
glDrawArrays( GL_TRIANGLES, 0, numVertices);
}
GL_WARN_IF_ERROR();
}
}
void Renderable::setShaderProgram( const std::shared_ptr<Program> & program)
{
shaderProgram = program;
glUseProgram(shaderProgram->id);
}
void Renderable::useProgram()
{
glUseProgram( shaderProgram->id);
}
void Renderable::setWorldMatrix( const Matrix4x4 & mat)
{
GLint loc = shaderProgram->uniformLocation("world");
shaderProgram->setUniformMatrix4fv(loc, 1, mat.data);
}
void Renderable::setViewMatrix( const Matrix4x4 & mat)
{
GLint loc = shaderProgram->uniformLocation("view");
shaderProgram->setUniformMatrix4fv(loc, 1, mat.data);
}
void Renderable::setProjection( const Matrix4x4 & mat)
{
GLint loc = shaderProgram->uniformLocation("projection");
shaderProgram->setUniformMatrix4fv(loc, 1, mat.data);
}
void Renderable::setCameraPosition( const Vector4 & pos)
{
GLint loc = shaderProgram->uniformLocation("cameraPosition");
shaderProgram->setUniform4fv(loc, 1, pos.data);
}
void Renderable::setAnimTime( float t)
{
GLint loc = shaderProgram->uniformLocation("anim_time");
shaderProgram->setUniform(loc, t);
}
void Renderable::setHighlighted( bool highlighted)
{
GLint loc = shaderProgram->uniformLocation("highlighted");
shaderProgram->setUniform(loc, highlighted);
}
void Renderable::setLights( float * pos, float * intensity, int numLights)
{
GLint loc;
loc = shaderProgram->uniformLocation("numLights");
shaderProgram->setUniform(loc, numLights);
loc = shaderProgram->uniformLocation("lightPositions");
shaderProgram->setUniform3fv(loc, numLights, pos);
loc = shaderProgram->uniformLocation("lightIntensities");
shaderProgram->setUniform3fv(loc, numLights, intensity);
}
void Renderable::setTexture( GLuint texId)
{
textureId = texId;
hasTexture = true;
}
void Renderable::setRoughness( float roughness)
{
this->roughness = std::max(roughness, 0.05f);
}
void Renderable::setF0( float F0)
{
this->F0 = F0;
}
void Renderable::setDiffuseColor( const RGBColor & c)
{
diffuseColor = c;
}
void Renderable::uploadMaterialUniforms()
{
GLint loc = shaderProgram->uniformLocation("roughness");
shaderProgram->setUniform(loc, roughness);
loc = shaderProgram->uniformLocation("F0");
shaderProgram->setUniform(loc, F0);
loc = shaderProgram->uniformLocation("diffuseColor");
shaderProgram->setUniform3fv(loc, 1, &diffuseColor.r);
}
void Renderable::uploadTextureUniforms()
{
GLint loc;
loc = shaderProgram->uniformLocation("useTexture");
shaderProgram->setUniform(loc, hasTexture);
loc = shaderProgram->uniformLocation("tex");
GLint textureUnit = 0;
shaderProgram->setUniform(loc, textureUnit);
}