28 if(data.positions.size() == 0)
34 m_camera.lookat(pmin, pmax);
37 if(data.normals.size() == 0)
41 m_mesh= buffers(data);
44 m_vertex_count= m_mesh.positions.size();
45 m_texcoord_count= m_mesh.texcoords.size();
46 m_normal_count= m_mesh.normals.size();
47 m_index_count= m_mesh.indices.size();
50 size_t size= m_mesh.vertex_buffer_size() + m_mesh.texcoord_buffer_size() + m_mesh.normal_buffer_size();
51 glGenBuffers(1, &m_vertex_buffer);
52 glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
53 glBufferData(GL_ARRAY_BUFFER, size,
nullptr, GL_STATIC_DRAW);
55 glGenVertexArrays(1, &m_vao);
56 glBindVertexArray(m_vao);
60 size= m_mesh.vertex_buffer_size();
61 glBufferSubData(GL_ARRAY_BUFFER, offset, size, m_mesh.vertex_buffer());
63 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (
const GLvoid *) offset);
64 glEnableVertexAttribArray(0);
67 if(m_mesh.texcoord_buffer_size())
69 offset= offset + size;
70 size= m_mesh.texcoord_buffer_size();
71 glBufferSubData(GL_ARRAY_BUFFER, offset, size, m_mesh.texcoord_buffer());
73 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (
const GLvoid *) offset);
74 glEnableVertexAttribArray(1);
78 offset= offset + size;
79 size= m_mesh.normal_buffer_size();
80 glBufferSubData(GL_ARRAY_BUFFER, offset, size, m_mesh.normal_buffer());
82 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (
const GLvoid *) offset);
83 glEnableVertexAttribArray(2);
86 glGenBuffers(1, &m_index_buffer);
87 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer);
88 glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_mesh.index_buffer_size(), m_mesh.index_buffer(), GL_STATIC_DRAW);
92 glBindBuffer(GL_ARRAY_BUFFER, 0);
93 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
96 if(m_mesh.texcoord_buffer_size())
104 glGenSamplers(1, &m_sampler);
105 glSamplerParameteri(m_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
106 glSamplerParameteri(m_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
107 glSamplerParameteri(m_sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);
108 glSamplerParameteri(m_sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
119 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
122 glDepthFunc(GL_LESS);
123 glEnable(GL_DEPTH_TEST);
145 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
147 static bool wireframe=
false;
151 wireframe= !wireframe;
155 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
157 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
161 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
162 if(mb & SDL_BUTTON(1))
163 m_camera.rotation(mx, my);
164 else if(mb & SDL_BUTTON(2))
167 else if(mb & SDL_BUTTON(3))
174 m_camera.move(16.f * wheel.y);
180 glUseProgram(m_program);
194 program_uniform(m_program,
"mvpMatrix", mvp);
195 program_uniform(m_program,
"mvMatrix", mv);
199 program_uniform(m_program,
"diffuse_color",
vec4(1, 1, 1, 1));
201 static bool flat=
false;
209 static int show_lights= 0;
210 static int show_material= 0;
216 show_material= (show_material +1) % m_mesh.material_groups.size();
222 show_material= (show_material +m_mesh.material_groups.size() -1) % m_mesh.material_groups.size();
226 glBindVertexArray(m_vao);
227 for(
int i= 0; i < (int) m_mesh.material_groups.size(); i++)
229 int material_id= m_mesh.material_groups[i].material;
230 const MaterialData& material= m_mesh.materials[material_id];
232 program_uniform(m_program,
"flat_shading",
float(flat));
235 program_uniform(m_program,
"diffuse_color",
Color((material_id % 100) / 99.f, 1 - (material_id % 10) / 9.f, (material_id % 4) / 3.f));
237 program_uniform(m_program,
"diffuse_color", material.
diffuse);
239 if(show && material_id == show_material)
240 program_uniform(m_program,
"diffuse_color",
Color(1, 0, 0));
242 if(show_lights && material.
emission.power() > 0)
243 program_uniform(m_program,
"diffuse_color",
Color(.8f, .4f, 0));
247 glDrawElements(GL_TRIANGLES, m_mesh.material_groups[i].count,
248 GL_UNSIGNED_INT, m_mesh.index_buffer_offset(m_mesh.material_groups[i].first));
252 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
255 label(m_widgets,
"mesh '%s', %d vertices %s %s", m_filename, m_vertex_count,
256 m_texcoord_count ?
"texcoords" :
"", m_normal_count ?
"normals" :
"");
259 label(m_widgets,
"%d materials",
int(m_mesh.materials.size()));
262 button(m_widgets,
"show materials", show);
263 button(m_widgets,
"show lights", show_lights);
267 const MaterialData& material= m_mesh.materials[show_material];
270 label(m_widgets,
"material%02d", show_material);
272 label(m_widgets,
"diffuse material");
273 else if(material.
ns > 1)
274 label(m_widgets,
"glossy material");
276 label(m_widgets,
"undef material ??");
283 label(m_widgets,
" Ns %.2f", material.
ns);
288 label(m_widgets,
" Ns texture %s", material.
ns_filename.c_str());