AdaptiveGrid::AdaptiveGrid( OpenGLFunctions & gl , unsigned short segments , const QVector3D & location , const QVector3D & normal) : m_program(new QOpenGLShaderProgram) , m_buffer(QOpenGLBuffer::VertexBuffer) , m_location(location) , m_normal(normal) , m_size(0) { m_transform = transform(m_location, m_normal); m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, s_vsSource); m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, s_fsSource); m_program->link(); setColor(QColor::fromRgbF(0.8, 0.8, 0.8)); m_vao.create(); m_vao.bind(); setupGridLineBuffer(segments); const int a_vertex = m_program->attributeLocation("a_vertex"); gl.glVertexAttribPointer(a_vertex, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr); gl.glEnableVertexAttribArray(a_vertex); m_vao.release(); }
void AssimpScene::setupVAOs( OpenGLFunctions & gl , const aiScene * scene) { assert(scene); // For each mesh for (unsigned int m = 0; m < scene->mNumMeshes; ++m) { const aiMesh * mesh = scene->mMeshes[m]; // create array with faces // have to convert from Assimp format to array std::vector<unsigned int> indices(mesh->mNumFaces * 3); for (unsigned int f = 0, i = 0; f < mesh->mNumFaces; ++f, i += 3) { const aiFace * face = &mesh->mFaces[f]; indices[i + 0] = face->mIndices[0]; indices[i + 1] = face->mIndices[1]; indices[i + 2] = face->mIndices[2]; } AssimpMesh * amesh = new AssimpMesh(); amesh->faces = mesh->mNumFaces; amesh->vao.create(); amesh->vao.bind(); // create buffers amesh->indices = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer); amesh->indices->create(); amesh->indices->setUsagePattern(QOpenGLBuffer::StaticDraw); amesh->indices->bind(); amesh->indices->allocate(indices.data(), indices.size() * sizeof(unsigned int)); if (mesh->HasPositions()) { amesh->vertices = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); amesh->vertices->create(); amesh->vertices->setUsagePattern(QOpenGLBuffer::StaticDraw); amesh->vertices->bind(); amesh->vertices->allocate(mesh->mVertices, mesh->mNumVertices * sizeof(float) * 3); gl.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, nullptr); gl.glEnableVertexAttribArray(0); } if (mesh->HasNormals()) { amesh->normals = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); amesh->normals->create(); amesh->normals->setUsagePattern(QOpenGLBuffer::StaticDraw); amesh->normals->bind(); amesh->normals->allocate(mesh->mNormals, mesh->mNumVertices * sizeof(float) * 3); gl.glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, sizeof(float) * 3, nullptr); gl.glEnableVertexAttribArray(1); } if (mesh->HasTextureCoords(0)) { float * texcs = new float[2 * mesh->mNumVertices]; for (unsigned int t = 0; t < mesh->mNumVertices; ++t) { texcs[t * 2 + 0] = mesh->mTextureCoords[0][t].x; texcs[t * 2 + 1] = mesh->mTextureCoords[0][t].y; } amesh->texcs= new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); amesh->texcs->create(); amesh->texcs->setUsagePattern(QOpenGLBuffer::StaticDraw); amesh->texcs->bind(); amesh->texcs->allocate(texcs, mesh->mNumVertices * sizeof(float) * 2); gl.glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr); gl.glEnableVertexAttribArray(2); } amesh->vao.release(); AssimpMaterial & material(amesh->material); // create material uniform buffer aiMaterial * mtl = scene->mMaterials[mesh->mMaterialIndex]; // support single texture on diffuse channel only for now... TODO aiString path; if (AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, 0, &path)) { material.texture = FileAssociatedTexture::getOrCreate2D(QString(path.C_Str()), gl); material.texCount = 1; } else material.texCount = 0; retrieveColor(mtl, AI_MATKEY_COLOR_DIFFUSE, material.diffuse, 0.8f, 0.8f, 0.8f, 1.0f); retrieveColor(mtl, AI_MATKEY_COLOR_AMBIENT, material.ambient, 0.2f, 0.2f, 0.2f, 1.0f); retrieveColor(mtl, AI_MATKEY_COLOR_SPECULAR, material.specular, 0.0f, 0.0f, 0.0f, 1.0f); retrieveColor(mtl, AI_MATKEY_COLOR_EMISSIVE, material.emissive, 0.0f, 0.0f, 0.0f, 1.0f); material.shininess = 0.f; unsigned int max; aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &material.shininess, &max); m_meshes.push_back(amesh); } }