void Model_Impl::insert_vbo(int vertex_count, const struct aiScene* sc, const struct aiNode* nd) { int i; unsigned int n = 0, t; bool use_texcoords = !vbo_texcoords.is_null(); // All meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; int num_vertex = mesh->mNumFaces * 3; if (!num_vertex) continue; std::vector<CL_Vec3f> normals; std::vector<CL_Vec3f> vertices; std::vector<CL_Vec2f> tex_coords; normals.reserve(num_vertex); vertices.reserve(num_vertex); if (use_texcoords) { if (mesh->mTextureCoords == NULL || mesh->mTextureCoords[0] == NULL) throw CL_Exception("This example expects texcoords to be set for this object"); tex_coords.reserve(num_vertex); } for (t = 0; t < mesh->mNumFaces; ++t) { const struct aiFace* face = &mesh->mFaces[t]; if (face->mNumIndices != 3) throw CL_Exception("This example only supports triangles"); for(i = 0; i < face->mNumIndices; i++) { int index = face->mIndices[i]; normals.push_back(&mesh->mNormals[index].x); vertices.push_back( &mesh->mVertices[index].x); if (use_texcoords) tex_coords.push_back( &mesh->mTextureCoords[0][index].x); } } vbo_positions.upload_data(vertex_count * sizeof(CL_Vec3f), &vertices[0], num_vertex * sizeof(CL_Vec3f)); vbo_normals.upload_data(vertex_count * sizeof(CL_Vec3f), &normals[0], num_vertex * sizeof(CL_Vec3f)); if (use_texcoords) vbo_texcoords.upload_data(vertex_count * sizeof(CL_Vec2f), &tex_coords[0], num_vertex * sizeof(CL_Vec2f)); vertex_count += num_vertex; } // All children for (n = 0; n < nd->mNumChildren; ++n) { insert_vbo(vertex_count, sc, nd->mChildren[n]); } }
void Model_Impl::Draw(CL_GraphicContext &gc, GraphicStore *gs, const CL_Mat4f &modelview_matrix) { gc.set_modelview(modelview_matrix); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vbo_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vbo_normals, 3, cl_type_float, (void *) 0); if (!vbo_texcoords.is_null()) { prim_array.set_attributes(2, vbo_texcoords, 2, cl_type_float, (void *) 0); gc.set_texture(0, gs->texture_underwater); gc.set_texture(1, gs->texture_background); gs->shader_texture.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_texture.Use(gc); } else { throw CL_Exception("What! no texure coordinates?"); } gc.draw_primitives(cl_triangles, vbo_size, prim_array); gc.reset_texture(0); gc.reset_texture(0); }
void Model_Impl::Draw(CL_GraphicContext &gc, GraphicStore *gs, const CL_Mat4f &modelview_matrix, bool is_draw_shadow) { gc.set_modelview(modelview_matrix); CL_PrimitivesArray prim_array(gc); prim_array.set_attributes(0, vbo_positions, 3, cl_type_float, (void *) 0); prim_array.set_attributes(1, vbo_normals, 3, cl_type_float, (void *) 0); if (is_draw_shadow) { gs->shader_depth.Use(gc); gc.draw_primitives(cl_triangles, vbo_size, prim_array); } else { if (!vbo_texcoords.is_null()) { prim_array.set_attributes(2, vbo_texcoords, 2, cl_type_float, (void *) 0); gs->shader_texture.SetShadowMatrix(gs->shadow_matrix); gc.set_texture(0, gs->texture_brick); gc.set_texture(1, gs->texture_shadow); gs->shader_texture.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_texture.Use(gc); } else { gs->shader_color.SetMaterial(material_shininess, material_emission, material_ambient, material_specular); gs->shader_color.Use(gc); } gc.draw_primitives(cl_triangles, vbo_size, prim_array); gc.reset_texture(0); gc.reset_texture(1); } }