ld33_umonster

annotate src/object.cc @ 2:35349df5392d

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