tavli
view src/object.cc @ 15:b1a195c3ee16
added shaders
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Jun 2015 08:34:24 +0300 |
parents | 283eb6e9f0a3 |
children | d6209903454b |
line source
1 #include "object.h"
2 #include "opengl.h"
4 Material::Material()
5 : diffuse(1, 1, 1), specular(0, 0, 0)
6 {
7 shininess = 60.0;
8 alpha = 1.0;
9 }
11 RenderOps::RenderOps()
12 {
13 zwrite = true;
14 }
16 void RenderOps::setup() const
17 {
18 if(!zwrite) {
19 glDepthMask(0);
20 }
21 }
23 Object::Object()
24 {
25 mesh = 0;
26 tex = 0;
27 sdr = 0;
28 }
30 Object::~Object()
31 {
32 delete mesh;
33 }
35 Matrix4x4 &Object::xform()
36 {
37 return matrix;
38 }
40 const Matrix4x4 &Object::xform() const
41 {
42 return matrix;
43 }
45 Matrix4x4 &Object::tex_xform()
46 {
47 return tex_matrix;
48 }
50 const Matrix4x4 &Object::tex_xform() const
51 {
52 return tex_matrix;
53 }
55 void Object::set_mesh(Mesh *m)
56 {
57 this->mesh = m;
58 }
60 Mesh *Object::get_mesh() const
61 {
62 return mesh;
63 }
65 void Object::set_texture(unsigned int tex)
66 {
67 this->tex = tex;
68 }
70 void Object::set_shader(unsigned int sdr)
71 {
72 this->sdr = sdr;
73 }
75 void Object::draw() const
76 {
77 if(!mesh) return;
79 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
80 rop.setup();
82 if(sdr) {
83 glUseProgram(sdr);
84 }
86 if(tex) {
87 glBindTexture(GL_TEXTURE_2D, tex);
88 glEnable(GL_TEXTURE_2D);
90 glMatrixMode(GL_TEXTURE);
91 glPushMatrix();
92 glLoadTransposeMatrixf(tex_matrix[0]);
93 } else {
94 glDisable(GL_TEXTURE_2D);
95 }
97 glMatrixMode(GL_MODELVIEW);
98 glPushMatrix();
99 glMultTransposeMatrixf(matrix[0]);
101 float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
102 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
103 float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
104 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
105 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
107 mesh->draw();
109 if(tex) {
110 glDisable(GL_TEXTURE_2D);
112 glMatrixMode(GL_TEXTURE);
113 glPopMatrix();
114 }
116 if(sdr) {
117 glUseProgram(0);
118 }
120 glMatrixMode(GL_MODELVIEW);
121 glPopMatrix();
123 glPopAttrib();
124 }
126 void Object::draw_wire(const Vector4 &col) const
127 {
128 glPushAttrib(GL_ENABLE_BIT);
129 glDisable(GL_LIGHTING);
131 glMatrixMode(GL_MODELVIEW);
132 glPushMatrix();
133 glMultTransposeMatrixf(matrix[0]);
135 glColor4f(col.x, col.y, col.z, col.w);
136 mesh->draw_wire();
138 glPopMatrix();
139 glPopAttrib();
140 }
142 void Object::draw_vertices(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->draw_vertices();
154 glPopMatrix();
155 glPopAttrib();
156 }
158 void Object::draw_normals(float len, const Vector4 &col) const
159 {
160 glPushAttrib(GL_ENABLE_BIT);
161 glDisable(GL_LIGHTING);
163 glMatrixMode(GL_MODELVIEW);
164 glPushMatrix();
165 glMultTransposeMatrixf(matrix[0]);
167 glColor4f(col.x, col.y, col.z, col.w);
168 mesh->set_vis_vecsize(len);
169 mesh->draw_normals();
171 glPopMatrix();
172 glPopAttrib();
173 }
175 void Object::draw_tangents(float len, const Vector4 &col) const
176 {
177 glPushAttrib(GL_ENABLE_BIT);
178 glDisable(GL_LIGHTING);
180 glMatrixMode(GL_MODELVIEW);
181 glPushMatrix();
182 glMultTransposeMatrixf(matrix[0]);
184 glColor4f(col.x, col.y, col.z, col.w);
185 mesh->set_vis_vecsize(len);
186 mesh->draw_tangents();
188 glPopMatrix();
189 glPopAttrib();
190 }
192 bool Object::intersect(const Ray &ray, HitPoint *hit) const
193 {
194 return false; // TODO
195 }