dbf-halloween2015

annotate src/object.cc @ 4:4316c0c879e9

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