tavli

annotate src/object.cc @ 14:283eb6e9f0a3

scenery
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 07:44:23 +0300
parents a8e26f163f99
children b1a195c3ee16
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@1 27 }
nuclear@1 28
nuclear@1 29 Object::~Object()
nuclear@1 30 {
nuclear@1 31 delete mesh;
nuclear@1 32 }
nuclear@1 33
nuclear@1 34 Matrix4x4 &Object::xform()
nuclear@1 35 {
nuclear@1 36 return matrix;
nuclear@1 37 }
nuclear@1 38
nuclear@1 39 const Matrix4x4 &Object::xform() const
nuclear@1 40 {
nuclear@1 41 return matrix;
nuclear@1 42 }
nuclear@1 43
nuclear@6 44 Matrix4x4 &Object::tex_xform()
nuclear@6 45 {
nuclear@6 46 return tex_matrix;
nuclear@6 47 }
nuclear@6 48
nuclear@6 49 const Matrix4x4 &Object::tex_xform() const
nuclear@6 50 {
nuclear@6 51 return tex_matrix;
nuclear@6 52 }
nuclear@6 53
nuclear@1 54 void Object::set_mesh(Mesh *m)
nuclear@1 55 {
nuclear@1 56 this->mesh = m;
nuclear@1 57 }
nuclear@1 58
nuclear@1 59 Mesh *Object::get_mesh() const
nuclear@1 60 {
nuclear@1 61 return mesh;
nuclear@1 62 }
nuclear@1 63
nuclear@4 64 void Object::set_texture(unsigned int tex)
nuclear@4 65 {
nuclear@4 66 this->tex = tex;
nuclear@4 67 }
nuclear@4 68
nuclear@1 69 void Object::draw() const
nuclear@1 70 {
nuclear@1 71 if(!mesh) return;
nuclear@1 72
nuclear@14 73 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@14 74 rop.setup();
nuclear@14 75
nuclear@4 76 if(tex) {
nuclear@4 77 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@4 78 glEnable(GL_TEXTURE_2D);
nuclear@6 79
nuclear@6 80 glMatrixMode(GL_TEXTURE);
nuclear@6 81 glPushMatrix();
nuclear@6 82 glLoadTransposeMatrixf(tex_matrix[0]);
nuclear@4 83 } else {
nuclear@4 84 glDisable(GL_TEXTURE_2D);
nuclear@4 85 }
nuclear@4 86
nuclear@1 87 glMatrixMode(GL_MODELVIEW);
nuclear@1 88 glPushMatrix();
nuclear@2 89 glMultTransposeMatrixf(matrix[0]);
nuclear@1 90
nuclear@6 91 float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
nuclear@6 92 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
nuclear@6 93 float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
nuclear@6 94 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
nuclear@6 95 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
nuclear@6 96
nuclear@1 97 mesh->draw();
nuclear@1 98
nuclear@4 99 if(tex) {
nuclear@4 100 glDisable(GL_TEXTURE_2D);
nuclear@6 101
nuclear@6 102 glMatrixMode(GL_TEXTURE);
nuclear@6 103 glPopMatrix();
nuclear@4 104 }
nuclear@6 105
nuclear@6 106 glMatrixMode(GL_MODELVIEW);
nuclear@6 107 glPopMatrix();
nuclear@14 108
nuclear@14 109 glPopAttrib();
nuclear@1 110 }
nuclear@3 111
nuclear@11 112 void Object::draw_wire(const Vector4 &col) const
nuclear@11 113 {
nuclear@11 114 glPushAttrib(GL_ENABLE_BIT);
nuclear@11 115 glDisable(GL_LIGHTING);
nuclear@11 116
nuclear@11 117 glMatrixMode(GL_MODELVIEW);
nuclear@11 118 glPushMatrix();
nuclear@11 119 glMultTransposeMatrixf(matrix[0]);
nuclear@11 120
nuclear@11 121 glColor4f(col.x, col.y, col.z, col.w);
nuclear@11 122 mesh->draw_wire();
nuclear@11 123
nuclear@11 124 glPopMatrix();
nuclear@11 125 glPopAttrib();
nuclear@11 126 }
nuclear@11 127
nuclear@11 128 void Object::draw_vertices(const Vector4 &col) const
nuclear@11 129 {
nuclear@11 130 glPushAttrib(GL_ENABLE_BIT);
nuclear@11 131 glDisable(GL_LIGHTING);
nuclear@11 132
nuclear@11 133 glMatrixMode(GL_MODELVIEW);
nuclear@11 134 glPushMatrix();
nuclear@11 135 glMultTransposeMatrixf(matrix[0]);
nuclear@11 136
nuclear@11 137 glColor4f(col.x, col.y, col.z, col.w);
nuclear@11 138 mesh->draw_vertices();
nuclear@11 139
nuclear@11 140 glPopMatrix();
nuclear@11 141 glPopAttrib();
nuclear@11 142 }
nuclear@11 143
nuclear@11 144 void Object::draw_normals(float len, const Vector4 &col) const
nuclear@11 145 {
nuclear@11 146 glPushAttrib(GL_ENABLE_BIT);
nuclear@11 147 glDisable(GL_LIGHTING);
nuclear@11 148
nuclear@11 149 glMatrixMode(GL_MODELVIEW);
nuclear@11 150 glPushMatrix();
nuclear@11 151 glMultTransposeMatrixf(matrix[0]);
nuclear@11 152
nuclear@11 153 glColor4f(col.x, col.y, col.z, col.w);
nuclear@11 154 mesh->set_vis_vecsize(len);
nuclear@11 155 mesh->draw_normals();
nuclear@11 156
nuclear@11 157 glPopMatrix();
nuclear@11 158 glPopAttrib();
nuclear@11 159 }
nuclear@11 160
nuclear@11 161 void Object::draw_tangents(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_tangents();
nuclear@11 173
nuclear@11 174 glPopMatrix();
nuclear@11 175 glPopAttrib();
nuclear@11 176 }
nuclear@11 177
nuclear@3 178 bool Object::intersect(const Ray &ray, HitPoint *hit) const
nuclear@3 179 {
nuclear@3 180 return false; // TODO
nuclear@3 181 }