tavli

annotate src/object.cc @ 19:37dead56f01e

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