void AxisRenderer::render(const AxisCamera &camera, QGLShaderProgram &program, QGLBuffer &vertBuffer, QGLBuffer &indexBuffer, bool selected) { qDebug()<<"[AxisRender] start of render"; initializeGLFunctions(); //bind the program for use if(!program.bind()){ QMessageBox::critical(0, "Error", "Could not bind program for use"); qFatal("Could not bind program for use"); } qDebug()<<"[AxisRender] Drawing"; //set up shader locations program.bind(); GLint vertLoc = program.attributeLocation("vertex"); Q_ASSERT(vertLoc != -1); GLint colorLoc = program.uniformLocation("color"); Q_ASSERT(colorLoc != -1); GLint mvpLoc = program.uniformLocation("modelToCamera"); Q_ASSERT(mvpLoc != -1); //set default color to a cyan QVector4D color(0.0f, .5f, 1.0f, 1.0f); //modify the color for selected geometry if(selected){ qDebug()<<"[AxisRender] adding selection color"; color = color + QVector4D(1.0f, 0.0f, 0.0f, 0.0f); } //set the uniform values program.setUniformValueArray(colorLoc, &color, 1); program.setUniformValueArray(mvpLoc, &camera.getProjMatrix(), 1); //bind and set the vertex buffer for attribute vertBuffer.bind(); program.enableAttributeArray(vertLoc); program.setAttributeBuffer(vertLoc, GL_FLOAT, 0, 4); //bind the index buffer for the draw call indexBuffer.bind(); qDebug()<<"[AxisRender] Drawing"; //draw the index buffer NOTE: Hard set to 24 values for a box! glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, NULL); }
/// @overload QGLWidget void initializeGL(){ printf("OpenGL %d.%d\n",this->format().majorVersion(),this->format().minorVersion()); ///--- Create an array object to store properties { bool success = vao.create(); Q_ASSERT(success); vao.bind(); } ///--- Load/compile shaders { bool vok = program.addShaderFromSourceCode(QGLShader::Vertex, vshader); bool fok = program.addShaderFromSourceCode(QGLShader::Fragment, fshader); bool lok = program.link (); Q_ASSERT(lok && vok && fok); bool success = program.bind(); Q_ASSERT(success); } ///--- Create vertex buffer/attributes "position" { static float vertices[] = { -1.0000,-1.0000,+0.0000, +1.0000,-1.0000,+0.0000, -1.0000,+1.0000,+0.0000, +1.0000,+1.0000,+0.0000,}; vertexbuffer = QGLBuffer(QGLBuffer::VertexBuffer); bool success = vertexbuffer.create(); Q_ASSERT(success); vertexbuffer.setUsagePattern( QGLBuffer::StaticDraw ); success = vertexbuffer.bind(); Q_ASSERT(success); vertexbuffer.allocate( vertices, sizeof(vertices) ); program.setAttributeBuffer("position", GL_FLOAT, 0, 3 ); program.enableAttributeArray("position"); } ///--- Unbind to avoid pollution vao.release(); program.release(); ///--- Background glClearColor(1.0, 1.0, 1.0, 1.0); ///--- Setup opengl flags glDisable(GL_DEPTH_TEST); }
GLBufferHolder(QGLBuffer &vb) : vb(vb) { static const QString pfx = "GLBufferHolder::GLBufferHolder(): "; msuccess = vb.create(); if (!msuccess) { GerbilApplication::internalError( QString(pfx) + "QGLBuffer::create() failed."); return; } msuccess = vb.bind(); if (!msuccess) { GerbilApplication::internalError( QString(pfx) + "QGLBuffer::bind() failed"); return; } }
static PyObject *meth_QGLBuffer_type(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QGLBuffer *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QGLBuffer, &sipCpp)) { QGLBuffer::Type sipRes; Py_BEGIN_ALLOW_THREADS sipRes = sipCpp->type(); Py_END_ALLOW_THREADS return sipConvertFromEnum(sipRes,sipType_QGLBuffer_Type); } }
void Compute::storeVertices(const ViewportCtx &ctx, const std::vector<BinSet> &sets, const binindex& index, QGLBuffer &vb, bool drawMeans, const std::vector<multi_img::Value> &illuminant) { vb.setUsagePattern(QGLBuffer::StaticDraw); GLBufferHolder vbh(vb); if (!vbh.success()) { return; } //GGDBGM(boost::format("shuffleIdx.size()=%1%, (*ctx)->dimensionality=%2%\n") // %shuffleIdx.size() %(*ctx)->dimensionality) if (index.size() == 0) { std::cerr << "Compute::storeVertices(): error: empty binindex" << std::endl; return; } const size_t nbytes = index.size() * ctx.dimensionality * sizeof(GLfloat) * 2; //GGDBGP("Compute::storeVertices(): allocating "<< nbytes << " bytes" << endl); vb.allocate(nbytes); //GGDBGM("before vb.map()\n"); GLfloat *varr = (GLfloat*)vb.map(QGLBuffer::WriteOnly); //GGDBGM("after vb.map()\n"); if (!varr) { GerbilApplication::criticalError("Compute::storeVertices(): " "QGLBuffer::map() failed"); return; } GenerateVertices generate(drawMeans, ctx.dimensionality, ctx.minval, ctx.binsize, illuminant, sets, index, varr); tbb::parallel_for(tbb::blocked_range<size_t>(0, index.size()), generate, tbb::auto_partitioner()); return; }
QGLBuffer *Visualizer::WriteBufferData(QVector<QVector3D> vertices, QVector<QVector3D> normals, QVector<QVector2D> textures) { QGLBuffer* tmp = new QGLBuffer; tmp->create(); tmp->bind(); int offset = 0; tmp->allocate(vertices.size() * (3+3+2) * sizeof (GLfloat)); tmp->write(offset, vertices.constData() , vertices.size() * 3 * sizeof (GLfloat)); offset += vertices.size() * 3 * sizeof (GLfloat); tmp->write(offset, normals.constData(), normals.size() * 3 * sizeof (GLfloat)); offset += normals.size() * 3 * sizeof (GLfloat); tmp->write(offset, textures.constData(), textures.size() * 2 * sizeof (GLfloat)); tmp->release(); return tmp; }
void Renderer::draw(Mesh *mesh) { QGLBuffer* vertexBuffer = mesh->GLVertices(); vertexBuffer->bind(); _shaderProgram->setAttributeBuffer(_shaderProgram->vertexAttributeLocation(), GL_FLOAT, 0, 3, 8 * sizeof(float)); _shaderProgram->setAttributeBuffer(_shaderProgram->normalAttributeLocation(), GL_FLOAT, 3 * sizeof(float), 3, 8 * sizeof(float)); _shaderProgram->setAttributeBuffer(_shaderProgram->texCoordsAttributeLocation(), GL_FLOAT, 6 * sizeof(float), 2, 8 * sizeof(float)); _shaderProgram->setUniformValue(_shaderProgram->mvpUniformLocation(), _currentCamera->projection() * _currentCamera->world() * mesh->world()); _shaderProgram->setUniformValue(_shaderProgram->mUniformLocation(), mesh->world()); _shaderProgram->setUniformValue(_shaderProgram->vUniformLocation(), _currentCamera->world()); _shaderProgram->setUniformValue(_shaderProgram->colorUniformLocation(), mesh == _currentMesh ? QColor(Qt::red) : mesh->material()->color()); _shaderProgram->setUniformValue(_shaderProgram->hasTextureUniformLocation(), mesh->material()->hasTexture()); _shaderProgram->setUniformValue(_shaderProgram->diffuseCoefUniformLocation(), mesh->material()->diffuseCoef()); _shaderProgram->setUniformValue(_shaderProgram->specularCoefUniformLocation(), mesh->material()->specularCoef()); if (mesh->material()->hasTexture()) { glBindTexture(GL_TEXTURE_2D, mesh->material()->texture()); } QGLBuffer* faceBuffer = mesh->GLFaces(); faceBuffer->bind(); glPolygonMode(GL_FRONT_AND_BACK, isWireframe() ? GL_LINE : GL_FILL); glDrawElements(GL_TRIANGLES, faceBuffer->size(), GL_UNSIGNED_INT, (GLvoid*)0); }
QGLBuffer* Visualizer::WriteBufferData(QVector<QVector3D> vertices, QVector<QVector3D> colors) { QGLBuffer* tmp = new QGLBuffer; tmp->create(); tmp->bind(); int offset = 0; tmp->allocate(vertices.size() * (3+3) * sizeof (GLfloat)); tmp->write(offset, vertices.constData() , vertices.size() * 3 * sizeof (GLfloat)); offset += vertices.size() * 3 * sizeof (GLfloat); tmp->write(offset, colors.constData(), colors.size() * 3 * sizeof (GLfloat)); tmp->release(); return tmp; }
/// @overload QGLWidget void initializeGL(){ printf("OpenGL %d.%d\n",this->format().majorVersion(),this->format().minorVersion()); ///--- Background glClearColor(1.0, 1.0, 1.0, 1.0); ///--- Viewport (simple, for unresizeable window) glViewport(0, 0, this->width(), this->height()); ///--- Setup opengl flags glEnable(GL_DEPTH_TEST); ///--- Create the triangle index buffer { assert(mesh.is_triangle_mesh()); triangles.clear(); for(auto f: mesh.faces()) for(auto v: mesh.vertices(f)) triangles.push_back(v.idx()); } ///--- Create an array object to store properties { bool success = vao.create(); assert(success); vao.bind(); } ///--- Load/compile shaders { bool vok = program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl"); bool fok = program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl"); bool lok = program.link (); assert(lok && vok && fok); bool success = program.bind(); assert(success); } ///--- Create vertex buffer/attributes "position" { auto vpoints = mesh.get_vertex_property<Vec3>("v:point"); bool success = vertexbuffer.create(); assert(success); vertexbuffer.setUsagePattern( QGLBuffer::StaticDraw ); success = vertexbuffer.bind(); assert(success); vertexbuffer.allocate( vpoints.data(), sizeof(Vec3) * mesh.n_vertices() ); program.setAttributeBuffer("vpoint", GL_FLOAT, 0, 3 ); program.enableAttributeArray("vpoint"); } ///--- Create vertex buffer/attributes "normal" { auto vnormal = mesh.get_vertex_property<Vec3>("v:normal"); bool success = normalbuffer.create(); assert(success); normalbuffer.setUsagePattern( QGLBuffer::StaticDraw ); success = normalbuffer.bind(); assert(success); normalbuffer.allocate( vnormal.data(), sizeof(Vec3) * mesh.n_vertices() ); program.setAttributeBuffer("vnormal", GL_FLOAT, 0, 3 ); program.enableAttributeArray("vnormal"); } ///--- Create the index "triangle" buffer { bool success = indexbuffer.create(); assert(success); indexbuffer.setUsagePattern( QGLBuffer::StaticDraw ); success = indexbuffer.bind(); assert(success); indexbuffer.allocate(&triangles[0], triangles.size()*sizeof(unsigned int)); } #ifdef WITH_QGLVIEWER ///--- Setup camera { Box3 bbox = OpenGP::bounding_box(mesh); camera()->setType(qglviewer::Camera::ORTHOGRAPHIC); camera()->setSceneCenter(qglviewer::tr(bbox.center())); camera()->setSceneRadius(bbox.diagonal().norm()/2.0); camera()->showEntireScene(); } #endif ///--- Unbind to avoid pollution vao.release(); program.release(); }