tavli

annotate src/object.cc @ 15:b1a195c3ee16

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