dbf-halloween2015
view src/object.cc @ 0:50683c78264e
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:09:12 +0200 |
parents | |
children |
line source
1 #include "object.h"
2 #include "opengl.h"
3 #include "sdr.h"
5 Material::Material()
6 : diffuse(1, 1, 1), specular(0, 0, 0)
7 {
8 shininess = 60.0;
9 alpha = 1.0;
10 }
12 RenderOps::RenderOps()
13 {
14 zwrite = true;
15 cast_shadows = true;
16 transparent = false;
17 }
19 void RenderOps::setup() const
20 {
21 if(!zwrite) {
22 glDepthMask(0);
23 }
24 if(transparent) {
25 glEnable(GL_BLEND);
26 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
27 } else {
28 glDisable(GL_BLEND);
29 }
30 }
32 Object::Object()
33 {
34 mesh = 0;
35 tex = 0;
36 sdr = 0;
37 }
39 Object::~Object()
40 {
41 delete mesh;
42 }
44 Matrix4x4 &Object::xform()
45 {
46 return matrix;
47 }
49 const Matrix4x4 &Object::xform() const
50 {
51 return matrix;
52 }
54 Matrix4x4 &Object::tex_xform()
55 {
56 return tex_matrix;
57 }
59 const Matrix4x4 &Object::tex_xform() const
60 {
61 return tex_matrix;
62 }
64 void Object::set_mesh(Mesh *m)
65 {
66 this->mesh = m;
67 }
69 Mesh *Object::get_mesh() const
70 {
71 return mesh;
72 }
74 void Object::set_texture(unsigned int tex)
75 {
76 this->tex = tex;
77 }
79 unsigned int Object::get_texture() const
80 {
81 return tex;
82 }
84 void Object::set_shader(unsigned int sdr)
85 {
86 if(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) {
87 this->sdr = sdr;
88 }
89 }
91 unsigned int Object::get_shader() const
92 {
93 return sdr;
94 }
96 void Object::draw() const
97 {
98 if(!mesh) return;
100 /*
101 if(shadow_pass && !rop.cast_shadows) {
102 return;
103 }
104 */
106 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
107 rop.setup();
109 glUseProgram(sdr);
111 if(tex) {
112 glBindTexture(GL_TEXTURE_2D, tex);
113 glEnable(GL_TEXTURE_2D);
115 glMatrixMode(GL_TEXTURE);
116 glPushMatrix();
117 glLoadTransposeMatrixf(tex_matrix[0]);
118 } else {
119 glDisable(GL_TEXTURE_2D);
120 }
122 glMatrixMode(GL_MODELVIEW);
123 glPushMatrix();
124 glMultTransposeMatrixf(matrix[0]);
126 if(sdr) {
127 set_uniform_matrix4_transposed(sdr, "world_matrix", (float*)matrix[0]);
128 }
130 float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
131 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
132 float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
133 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
134 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
136 mesh->draw();
138 if(tex) {
139 glDisable(GL_TEXTURE_2D);
141 glMatrixMode(GL_TEXTURE);
142 glPopMatrix();
143 }
145 if(sdr) {
146 glUseProgram(0);
147 }
149 glMatrixMode(GL_MODELVIEW);
150 glPopMatrix();
152 glPopAttrib();
153 }
155 void Object::draw_wire(const Vector4 &col) const
156 {
157 glPushAttrib(GL_ENABLE_BIT);
158 glDisable(GL_LIGHTING);
159 glUseProgram(0);
161 glMatrixMode(GL_MODELVIEW);
162 glPushMatrix();
163 glMultTransposeMatrixf(matrix[0]);
165 glColor4f(col.x, col.y, col.z, col.w);
166 mesh->draw_wire();
168 glPopMatrix();
169 glPopAttrib();
170 }
172 void Object::draw_vertices(const Vector4 &col) const
173 {
174 glPushAttrib(GL_ENABLE_BIT);
175 glDisable(GL_LIGHTING);
176 glUseProgram(0);
178 glMatrixMode(GL_MODELVIEW);
179 glPushMatrix();
180 glMultTransposeMatrixf(matrix[0]);
182 glColor4f(col.x, col.y, col.z, col.w);
183 mesh->draw_vertices();
185 glPopMatrix();
186 glPopAttrib();
187 }
189 void Object::draw_normals(float len, const Vector4 &col) const
190 {
191 glPushAttrib(GL_ENABLE_BIT);
192 glDisable(GL_LIGHTING);
194 glMatrixMode(GL_MODELVIEW);
195 glPushMatrix();
196 glMultTransposeMatrixf(matrix[0]);
198 glColor4f(col.x, col.y, col.z, col.w);
199 mesh->set_vis_vecsize(len);
200 mesh->draw_normals();
202 glPopMatrix();
203 glPopAttrib();
204 }
206 void Object::draw_tangents(float len, const Vector4 &col) const
207 {
208 glPushAttrib(GL_ENABLE_BIT);
209 glDisable(GL_LIGHTING);
211 glMatrixMode(GL_MODELVIEW);
212 glPushMatrix();
213 glMultTransposeMatrixf(matrix[0]);
215 glColor4f(col.x, col.y, col.z, col.w);
216 mesh->set_vis_vecsize(len);
217 mesh->draw_tangents();
219 glPopMatrix();
220 glPopAttrib();
221 }
223 bool Object::intersect(const Ray &ray, HitPoint *hit) const
224 {
225 return false; // TODO
226 }