tavli

annotate src/object.cc @ 11:a8e26f163f99

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