-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
134 lines (102 loc) · 4.67 KB
/
object.cpp
File metadata and controls
134 lines (102 loc) · 4.67 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
////////////////////////////////////////////////////////////////////////
// A lightweight class representing an instance of an object that can
// be drawn onscreen. An Object consists of a shape (batch of
// triangles), and various transformation, color and texture
// parameters. It also contains a list of child objects to be drawn
// in a hierarchical fashion under the control of parent's
// transformations.
//
// Methods consist of a constructor, and a Draw procedure, and an
// append for building hierarchies of objects.
#include "math.h"
#include <fstream>
#include <stdlib.h>
#include <glbinding/gl/gl.h>
#include <glbinding/Binding.h>
using namespace gl;
#define GLM_FORCE_CTOR_INIT
#define GLM_FORCE_RADIANS
#define GLM_SWIZZLE
#include <glm/glm.hpp>
#include "framework.h"
#include "shapes.h"
#include "transform.h"
#include <glu.h> // For gluErrorString
#define CHECKERROR {GLenum err = glGetError(); if (err != GL_NO_ERROR) { fprintf(stderr, "OpenGL error (at line object.cpp:%d): %s\n", __LINE__, gluErrorString(err)); exit(-1);} }
Object::Object(Shape* _shape, const int _objectId,
const glm::vec3 _diffuseColor, const glm::vec3 _specularColor, const float _shininess, bool _reflective, Texture* _texture, Texture* _normalTexture)
: diffuseColor(_diffuseColor), specularColor(_specularColor), shininess(_shininess), reflective(_reflective),
shape(_shape), texture(_texture), normalTexture(_normalTexture), objectId(_objectId), drawMe(true)
{}
void Object::Draw(ShaderProgram* program, glm::mat4& objectTr)
{
CHECKERROR;
// @@ The object specific parameters (uniform variables) used by
// the shader are set here. Scene specific parameters are set in
// the DrawScene procedure in scene.cpp
// @@ Textures, being uniform sampler2d variables in the shader,
// are also set here. Call texture->Bind in texture.cpp to do so.
// Inform the shader of the surface values Kd, Ks, and alpha.
int loc = glGetUniformLocation(program->programId, "diffuse");
glUniform3fv(loc, 1, &diffuseColor[0]);
loc = glGetUniformLocation(program->programId, "specular");
glUniform3fv(loc, 1, &specularColor[0]);
loc = glGetUniformLocation(program->programId, "shininess");
glUniform1f(loc, shininess);
// Inform the shader of which object is being drawn so it can make
// object specific decisions.
loc = glGetUniformLocation(program->programId, "objectId");
glUniform1i(loc, objectId);
// Inform the shader of this object's model transformation. The
// inverse of the model transformation, needed for transforming
// normals, is calculated and passed to the shader here.
loc = glGetUniformLocation(program->programId, "ModelTr");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(objectTr));
glm::mat4 inv = glm::inverse(objectTr);
loc = glGetUniformLocation(program->programId, "NormalTr");
glUniformMatrix4fv(loc, 1, GL_FALSE, Pntr(inv));
loc = glGetUniformLocation(program->programId, "hasTexture");
glUniform1f(loc, false);
loc = glGetUniformLocation(program->programId, "hasNormal");
glUniform1f(loc, false);
loc = glGetUniformLocation(program->programId, "reflective");
glUniform1i(loc, reflective);
// If this object has an associated texture, this is the place to
// load the texture into a texture-unit of your choice and inform
// the shader program of the texture-unit number. See
// Texture::Bind for the 4 lines of code to do exactly that.
// Draw this object
CHECKERROR;
if (shape && drawMe)
{
if (texture)
{
texture->BindTexture(0, program->programId, "tex");
loc = glGetUniformLocation(program->programId, "hasTexture");
glUniform1f(loc, true);
}
if (normalTexture)
{
normalTexture->BindTexture(1, program->programId, "normalTex");
loc = glGetUniformLocation(program->programId, "hasNormal");
glUniform1f(loc, true);
float currentTime = glfwGetTime();
loc = glGetUniformLocation(program->programId, "time");
glUniform1f(loc, currentTime);
}
shape->DrawVAO();
if (normalTexture) normalTexture->UnbindTexture(1);
if (texture) texture->UnbindTexture(0);
}
CHECKERROR;
CHECKERROR;
// Recursively draw each sub-objects, each with its own transformation.
if (drawMe)
for (int i=0; i<instances.size(); i++) {
CHECKERROR;
glm::mat4 itr = objectTr*instances[i].second*animTr;
CHECKERROR;
instances[i].first->Draw(program, itr);
CHECKERROR; }
CHECKERROR;
}