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