ld33_umonster

view src/object.cc @ 10:1b30bd381667

sweep curve mesh gen and dragon horns
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 27 Aug 2015 05:25:04 +0300
parents 4a6683050e29
children
line source
1 #include "object.h"
2 #include "opengl.h"
3 #include "shadow.h"
4 #include "shader.h"
6 Material::Material()
7 : diffuse(1, 1, 1), specular(0, 0, 0)
8 {
9 shininess = 60.0;
10 alpha = 1.0;
11 }
13 RenderOps::RenderOps()
14 {
15 zwrite = true;
16 cast_shadows = true;
17 transparent = false;
18 }
20 void RenderOps::setup() const
21 {
22 if(!zwrite) {
23 glDepthMask(0);
24 }
25 if(transparent) {
26 glEnable(GL_BLEND);
27 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
28 } else {
29 glDisable(GL_BLEND);
30 }
31 }
33 Object::Object()
34 {
35 mesh = 0;
36 tex = 0;
37 sdr = 0;
38 }
40 Object::~Object()
41 {
42 delete mesh;
43 }
45 Matrix4x4 &Object::xform()
46 {
47 return matrix;
48 }
50 const Matrix4x4 &Object::xform() const
51 {
52 return matrix;
53 }
55 Matrix4x4 &Object::tex_xform()
56 {
57 return tex_matrix;
58 }
60 const Matrix4x4 &Object::tex_xform() const
61 {
62 return tex_matrix;
63 }
65 void Object::set_mesh(Mesh *m)
66 {
67 this->mesh = m;
68 }
70 Mesh *Object::get_mesh() const
71 {
72 return mesh;
73 }
75 void Object::set_texture(unsigned int tex)
76 {
77 this->tex = tex;
78 }
80 void Object::set_shader(unsigned int sdr)
81 {
82 if(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) {
83 this->sdr = sdr;
84 }
85 }
87 unsigned int Object::get_shader() const
88 {
89 return sdr;
90 }
92 void Object::draw() const
93 {
94 if(!mesh) return;
96 if(shadow_pass && !rop.cast_shadows) {
97 return;
98 }
100 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
101 rop.setup();
103 if(glcaps.shaders) {
104 if(sdr) {
105 if(!shadow_pass) {
106 ::set_shader(sdr);
107 }
108 } else {
109 ::set_shader(0);
110 }
111 }
113 if(tex) {
114 glBindTexture(GL_TEXTURE_2D, tex);
115 glEnable(GL_TEXTURE_2D);
117 glMatrixMode(GL_TEXTURE);
118 glPushMatrix();
119 glLoadTransposeMatrixf(tex_matrix[0]);
120 } else {
121 glDisable(GL_TEXTURE_2D);
122 }
124 glMatrixMode(GL_MODELVIEW);
125 glPushMatrix();
126 glMultTransposeMatrixf(matrix[0]);
128 float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
129 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
130 float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
131 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
132 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
134 mesh->draw();
136 if(tex) {
137 glDisable(GL_TEXTURE_2D);
139 glMatrixMode(GL_TEXTURE);
140 glPopMatrix();
141 }
143 if(sdr) {
144 ::set_shader(0);
145 }
147 glMatrixMode(GL_MODELVIEW);
148 glPopMatrix();
150 glPopAttrib();
151 }
153 void Object::draw_wire(const Vector4 &col) const
154 {
155 if(shadow_pass) return;
157 glPushAttrib(GL_ENABLE_BIT);
158 glDisable(GL_LIGHTING);
159 glUseProgram(0);
161 glMatrixMode(GL_MODELVIEW);
162 glPushMatrix();
163 glMultTransposeMatrixf(matrix[0]);
165 glColor4f(col.x, col.y, col.z, col.w);
166 mesh->draw_wire();
168 glPopMatrix();
169 glPopAttrib();
170 }
172 void Object::draw_vertices(const Vector4 &col) const
173 {
174 glPushAttrib(GL_ENABLE_BIT);
175 glDisable(GL_LIGHTING);
176 glUseProgram(0);
178 glMatrixMode(GL_MODELVIEW);
179 glPushMatrix();
180 glMultTransposeMatrixf(matrix[0]);
182 glColor4f(col.x, col.y, col.z, col.w);
183 mesh->draw_vertices();
185 glPopMatrix();
186 glPopAttrib();
187 }
189 void Object::draw_normals(float len, const Vector4 &col) const
190 {
191 int cur_sdr;
192 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
193 glUseProgram(0);
195 glPushAttrib(GL_ENABLE_BIT);
196 glDisable(GL_LIGHTING);
198 glMatrixMode(GL_MODELVIEW);
199 glPushMatrix();
200 glMultTransposeMatrixf(matrix[0]);
202 glColor4f(col.x, col.y, col.z, col.w);
203 mesh->set_vis_vecsize(len);
204 mesh->draw_normals();
206 glPopMatrix();
207 glPopAttrib();
209 glUseProgram(cur_sdr);
210 }
212 void Object::draw_tangents(float len, const Vector4 &col) const
213 {
214 glPushAttrib(GL_ENABLE_BIT);
215 glDisable(GL_LIGHTING);
217 glMatrixMode(GL_MODELVIEW);
218 glPushMatrix();
219 glMultTransposeMatrixf(matrix[0]);
221 glColor4f(col.x, col.y, col.z, col.w);
222 mesh->set_vis_vecsize(len);
223 mesh->draw_tangents();
225 glPopMatrix();
226 glPopAttrib();
227 }
229 bool Object::intersect(const Ray &ray, HitPoint *hit) const
230 {
231 return false; // TODO
232 }