tavli

view src/object.cc @ 13:f3c5134b4914

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