tavli

view src/object.cc @ 23:3e6430028d54

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