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 AdaptiveGrid::draw( OpenGLFunctions & gl) { gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl.glEnable(GL_BLEND); gl.glEnable(GL_DEPTH_TEST); m_program->bind(); m_vao.bind(); gl.glDrawArrays(GL_LINES, 0, m_size); m_vao.release(); m_program->release(); gl.glDisable(GL_BLEND); }
void AssimpScene::draw( OpenGLFunctions & gl , QOpenGLShaderProgram & program , const GLenum mode) { if (!m_valid) return; if (!program.isLinked()) return; std::vector<AssimpMesh *>::const_iterator i = m_meshes.begin(); const std::vector<AssimpMesh *>::const_iterator iEnd = m_meshes.end(); program.bind(); program.setUniformValue("model", m_transform * m_normalize); for (; i != iEnd; ++i) { AssimpMesh * mesh(*i); program.setUniformValue("diffuse", mesh->material.diffuse); program.setUniformValue("ambient", mesh->material.ambient); program.setUniformValue("specular", mesh->material.specular); program.setUniformValue("emissive", mesh->material.emissive); program.setUniformValue("shininess", mesh->material.shininess); program.setUniformValue("texCount", mesh->material.texCount); if (mesh->material.texCount > 0) { program.setUniformValue("difftex", 0); gl.glActiveTexture(GL_TEXTURE0); gl.glBindTexture(GL_TEXTURE_2D, mesh->material.texture); } mesh->vao.bind(); gl.glDrawElements(mode, mesh->faces * 3, GL_UNSIGNED_INT, nullptr); mesh->vao.release(); if (mesh->material.texCount > 0) gl.glBindTexture(GL_TEXTURE_2D, 0); } program.release(); }
void PolygonalDrawable::draw(OpenGLFunctions & gl) { m_vao.bind(); gl.glEnable(GL_DEPTH_TEST); gl.glEnable(GL_CULL_FACE); /*if (m_indices.size()) { gl.glDrawElements(m_mode, m_indices.size(), GL_UNSIGNED_INT, (void*)0); } else*/ { gl.glDrawArrays(m_mode, 0, m_vertices.size()); } gl.glDisable(GL_DEPTH_TEST); gl.glDisable(GL_CULL_FACE); }
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); } }