tavli

annotate src/object.cc @ 6:a0d30f6f20d4

- texture coordinate generation in class Mesh - wood texture
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Jun 2015 04:22:06 +0300
parents b41ceead1708
children a8e26f163f99
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@3 93 bool Object::intersect(const Ray &ray, HitPoint *hit) const
nuclear@3 94 {
nuclear@3 95 return false; // TODO
nuclear@3 96 }