tavli
view src/object.cc @ 16:d6209903454b
opengl capabilities
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Jun 2015 08:48:25 +0300 |
parents | b1a195c3ee16 |
children | 37dead56f01e |
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 if(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) {
73 this->sdr = sdr;
74 }
75 }
77 void Object::draw() const
78 {
79 if(!mesh) return;
81 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
82 rop.setup();
84 if(sdr) {
85 glUseProgram(sdr);
86 }
88 if(tex) {
89 glBindTexture(GL_TEXTURE_2D, tex);
90 glEnable(GL_TEXTURE_2D);
92 glMatrixMode(GL_TEXTURE);
93 glPushMatrix();
94 glLoadTransposeMatrixf(tex_matrix[0]);
95 } else {
96 glDisable(GL_TEXTURE_2D);
97 }
99 glMatrixMode(GL_MODELVIEW);
100 glPushMatrix();
101 glMultTransposeMatrixf(matrix[0]);
103 float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
104 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
105 float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
106 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
107 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
109 mesh->draw();
111 if(tex) {
112 glDisable(GL_TEXTURE_2D);
114 glMatrixMode(GL_TEXTURE);
115 glPopMatrix();
116 }
118 if(sdr) {
119 glUseProgram(0);
120 }
122 glMatrixMode(GL_MODELVIEW);
123 glPopMatrix();
125 glPopAttrib();
126 }
128 void Object::draw_wire(const Vector4 &col) const
129 {
130 glPushAttrib(GL_ENABLE_BIT);
131 glDisable(GL_LIGHTING);
133 glMatrixMode(GL_MODELVIEW);
134 glPushMatrix();
135 glMultTransposeMatrixf(matrix[0]);
137 glColor4f(col.x, col.y, col.z, col.w);
138 mesh->draw_wire();
140 glPopMatrix();
141 glPopAttrib();
142 }
144 void Object::draw_vertices(const Vector4 &col) const
145 {
146 glPushAttrib(GL_ENABLE_BIT);
147 glDisable(GL_LIGHTING);
149 glMatrixMode(GL_MODELVIEW);
150 glPushMatrix();
151 glMultTransposeMatrixf(matrix[0]);
153 glColor4f(col.x, col.y, col.z, col.w);
154 mesh->draw_vertices();
156 glPopMatrix();
157 glPopAttrib();
158 }
160 void Object::draw_normals(float len, const Vector4 &col) const
161 {
162 glPushAttrib(GL_ENABLE_BIT);
163 glDisable(GL_LIGHTING);
165 glMatrixMode(GL_MODELVIEW);
166 glPushMatrix();
167 glMultTransposeMatrixf(matrix[0]);
169 glColor4f(col.x, col.y, col.z, col.w);
170 mesh->set_vis_vecsize(len);
171 mesh->draw_normals();
173 glPopMatrix();
174 glPopAttrib();
175 }
177 void Object::draw_tangents(float len, const Vector4 &col) const
178 {
179 glPushAttrib(GL_ENABLE_BIT);
180 glDisable(GL_LIGHTING);
182 glMatrixMode(GL_MODELVIEW);
183 glPushMatrix();
184 glMultTransposeMatrixf(matrix[0]);
186 glColor4f(col.x, col.y, col.z, col.w);
187 mesh->set_vis_vecsize(len);
188 mesh->draw_tangents();
190 glPopMatrix();
191 glPopAttrib();
192 }
194 bool Object::intersect(const Ray &ray, HitPoint *hit) const
195 {
196 return false; // TODO
197 }