void ScreenAlignedQuad::strip(QOpenGLBuffer &vertices) { float rawv[] = { +1.f, -1.f , +1.f, +1.f , -1.f, -1.f , -1.f, +1.f }; vertices.bind(); vertices.allocate(rawv, sizeof(float)* 8); }
void GLImageShader2D::setCoords(QOpenGLBuffer& coords, const GLfloat *offset, int tupleSize, int stride) { coords.bind(); setCoords(offset, tupleSize, stride); coords.release(); }
void DrawContext::DrawGeometry() { // Load geometry and bind a vertex buffer QOpenGLBuffer* vbo = geometry_->VBO(); vbo->bind(); // Load per-vertex attribute arrays const ShaderStandardVariables& locs = shader_->StandardVariables(); SetupAttributeArray(program_, locs.sv_vert_pos, geometry_->NumVertices(), GL_FLOAT, geometry_->VertexOffset(), 3); SetupAttributeArray(program_, locs.sv_normal, geometry_->NumNormals(), GL_FLOAT, geometry_->NormalOffset(), 3); SetupAttributeArray(program_, locs.sv_diffuse, geometry_->NumDiffuse(), GL_FLOAT, geometry_->DiffuseOffset(), 4); SetupAttributeArray(program_, locs.sv_specular, geometry_->NumSpecular(), GL_FLOAT, geometry_->SpecularOffset(), 4); SetupAttributeArray(program_, locs.sv_shininess, geometry_->NumShininess(), GL_FLOAT, geometry_->ShininessOffset(), 1); SetupAttributeArray(program_, locs.sv_tex_coords_0, geometry_->NumTexCoords0(), GL_FLOAT, geometry_->TexCoords0Offset(), 2); // TODO load custom attribute arrays // Draw the geometry QOpenGLBuffer* index_buffer = geometry_->IndexBuffer(); if (index_buffer) { index_buffer->bind(); glDrawElements(geometry_->GLMode(), geometry_->NumIndices(), geometry_->IndexType(), 0); index_buffer->release(); } else { glDrawArrays(geometry_->GLMode(), 0, geometry_->NumVertices()); } vbo->release(); }
void GLLineShader2D::setColour(QOpenGLBuffer& colour, const GLfloat *offset, int tupleSize, int stride) { colour.bind(); setColour(offset, tupleSize, stride); colour.release(); }
void QGLView::initializeVertexBuffer(ModelType type, const void *bufferData, int bufferLength) { QOpenGLBuffer *glBuffer = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); glBuffer->create(); glBuffer->setUsagePattern(QOpenGLBuffer::StaticDraw); glBuffer->bind(); glBuffer->allocate(bufferData, bufferLength); glBuffer->release(); m_vertexBufferMap.insert(type, glBuffer); }
/****************************************************************************** * Renders a 2d polyline in the viewport. ******************************************************************************/ void ViewportSceneRenderer::render2DPolyline(const Point2* points, int count, const ColorA& color, bool closed) { OVITO_STATIC_ASSERT(sizeof(points[0]) == 2*sizeof(GLfloat)); // Load OpenGL shader. QOpenGLShaderProgram* shader = loadShaderProgram("line", ":/core/glsl/lines/line.vs", ":/core/glsl/lines/line.fs"); if(!shader->bind()) throw Exception(tr("Failed to bind OpenGL shader.")); bool wasDepthTestEnabled = glIsEnabled(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST); GLint vc[4]; glGetIntegerv(GL_VIEWPORT, vc); QMatrix4x4 tm; tm.ortho(vc[0], vc[0] + vc[2], vc[1] + vc[3], vc[1], -1, 1); OVITO_CHECK_OPENGL(shader->setUniformValue("modelview_projection_matrix", tm)); QOpenGLBuffer vertexBuffer; if(glformat().majorVersion() >= 3) { if(!vertexBuffer.create()) throw Exception(tr("Failed to create OpenGL vertex buffer.")); if(!vertexBuffer.bind()) throw Exception(tr("Failed to bind OpenGL vertex buffer.")); vertexBuffer.allocate(points, 2 * sizeof(GLfloat) * count); OVITO_CHECK_OPENGL(shader->enableAttributeArray("position")); OVITO_CHECK_OPENGL(shader->setAttributeBuffer("position", GL_FLOAT, 0, 2)); vertexBuffer.release(); } else { OVITO_CHECK_OPENGL(glEnableClientState(GL_VERTEX_ARRAY)); OVITO_CHECK_OPENGL(glVertexPointer(2, GL_FLOAT, 0, points)); } if(glformat().majorVersion() >= 3) { OVITO_CHECK_OPENGL(shader->disableAttributeArray("color")); OVITO_CHECK_OPENGL(shader->setAttributeValue("color", color.r(), color.g(), color.b(), color.a())); } else { OVITO_CHECK_OPENGL(glColor4(color)); } OVITO_CHECK_OPENGL(glDrawArrays(closed ? GL_LINE_LOOP : GL_LINE_STRIP, 0, count)); if(glformat().majorVersion() >= 3) { shader->disableAttributeArray("position"); } else { OVITO_CHECK_OPENGL(glDisableClientState(GL_VERTEX_ARRAY)); } shader->release(); if(wasDepthTestEnabled) glEnable(GL_DEPTH_TEST); }
QOpenGLBuffer* GLImageProcessor::buildQuadTextured() const { QOpenGLBuffer* lpvbo = new QOpenGLBuffer(); typedef struct { float xyz[3]; float uv[2]; } VertexUV; VertexUV Vertices[] = { { { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } }, { { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } }, { { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } }, { { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } } }; const size_t BufferSize = sizeof(Vertices); const size_t VertexSize = sizeof(Vertices[0]); const size_t UVOffset = sizeof(Vertices[0].xyz); // Setup our vertex buffer object. lpvbo->create(); lpvbo->bind(); lpvbo->allocate(Vertices, BufferSize); lpvbo->bind(); QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glEnableVertexAttribArray(0); f->glEnableVertexAttribArray(1); f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VertexSize, 0); f->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, VertexSize, reinterpret_cast<void *>(UVOffset)); lpvbo->release(); return lpvbo; }
void QGLView::drawModelVertices(ModelType type) { QOpenGLBuffer *vertexBuffer = m_vertexBufferMap[type]; QList<Parameters*> *modelParametersList = getDrawableList(type); if (modelParametersList->isEmpty()) { return; } vertexBuffer->bind(); m_modelProgram->enableAttributeArray(m_positionLocation); m_modelProgram->enableAttributeArray(m_normalLocation); m_modelProgram->setAttributeBuffer(m_positionLocation, GL_FLOAT, 0, 3, sizeof(ModelVertex)); m_modelProgram->setAttributeBuffer(m_normalLocation, GL_FLOAT, 3*sizeof(GLfloat), 3, sizeof(ModelVertex)); for (int i = 0; i < modelParametersList->size(); ++i) { Parameters *modelParameters = static_cast<Parameters*>(modelParametersList->at(i)); m_modelProgram->setUniformValue(m_colorLocation, modelParameters->color); m_modelProgram->setUniformValue(m_modelMatrixLocation, modelParameters->modelMatrix); if (m_selectionModeActive) // selection mode active { m_modelProgram->setUniformValue(m_idColorLocation, QColor(0xFF000000u + m_currentDrawableId)); // color for selection mode m_drawableIdMap.insert(m_currentDrawableId, modelParameters); m_currentDrawableId++; } glDrawArrays(GL_TRIANGLES, 0, vertexBuffer->size()/sizeof(ModelVertex)); } m_modelProgram->disableAttributeArray(m_positionLocation); m_modelProgram->disableAttributeArray(m_normalLocation); vertexBuffer->release(); }
void Viewer_impl::showDistance(QPoint pixel) { static bool isAset = false; bool found; CGAL::qglviewer::Vec point; point = viewer->camera()->pointUnderPixel(pixel, found); if(!isAset && found) { //set APoint APoint = point; isAset = true; clearDistancedisplay(); } else if (found) { //set BPoint BPoint = point; isAset = false; // fills the buffers std::vector<float> v; v.resize(6); v[0] = float(APoint.x); v[1] = float(APoint.y); v[2] = float(APoint.z); v[3] = float(BPoint.x); v[4] = float(BPoint.y); v[5] = float(BPoint.z); vao.bind(); buffer.bind(); buffer.allocate(v.data(),6*sizeof(float)); rendering_program_dist.enableAttributeArray("vertex"); rendering_program_dist.setAttributeBuffer("vertex",GL_FLOAT,0,3); buffer.release(); vao.release(); distance_is_displayed = true; double dist = std::sqrt((BPoint.x-APoint.x)*(BPoint.x-APoint.x) + (BPoint.y-APoint.y)*(BPoint.y-APoint.y) + (BPoint.z-APoint.z)*(BPoint.z-APoint.z)); QFont font; font.setBold(true); TextItem *ACoord = new TextItem(float(APoint.x), float(APoint.y), float(APoint.z), QString("A(%1,%2,%3)").arg(APoint.x-viewer->offset().x).arg(APoint.y-viewer->offset().y).arg(APoint.z-viewer->offset().z), true, font, Qt::red, true); distance_text.append(ACoord); TextItem *BCoord = new TextItem(float(BPoint.x), float(BPoint.y), float(BPoint.z), QString("B(%1,%2,%3)").arg(BPoint.x-viewer->offset().x).arg(BPoint.y-viewer->offset().y).arg(BPoint.z-viewer->offset().z), true, font, Qt::red, true); distance_text.append(BCoord); CGAL::qglviewer::Vec centerPoint = 0.5*(BPoint+APoint); TextItem *centerCoord = new TextItem(float(centerPoint.x), float(centerPoint.y), float(centerPoint.z), QString(" distance: %1").arg(dist), true, font, Qt::red, true); distance_text.append(centerCoord); Q_FOREACH(TextItem* ti, distance_text) textRenderer->addText(ti); Q_EMIT(viewer->sendMessage(QString("First point : A(%1,%2,%3), second point : B(%4,%5,%6), distance between them : %7") .arg(APoint.x-viewer->offset().x) .arg(APoint.y-viewer->offset().y) .arg(APoint.z-viewer->offset().z) .arg(BPoint.x-viewer->offset().x) .arg(BPoint.y-viewer->offset().y) .arg(BPoint.z-viewer->offset().z) .arg(dist))); } }
void OpenGLVideoPrivate::bindAttributes(VideoShader* shader, const QRectF &t, const QRectF &r) { const bool tex_rect = shader->textureTarget() == GL_TEXTURE_RECTANGLE; // also check size change for normalizedROI computation if roi is not normalized const bool roi_changed = valiad_tex_width != material->validTextureWidth() || roi != r || video_size != material->frameSize(); const int tc = shader->textureLocationCount(); if (roi_changed) { roi = r; valiad_tex_width = material->validTextureWidth(); video_size = material->frameSize(); } if (tex_target != shader->textureTarget()) { tex_target = shader->textureTarget(); update_geo = true; } QRectF& target_rect = rect; if (target.isValid()) { if (roi_changed || target != t) { target = t; update_geo = true; } } else { if (roi_changed) { update_geo = true; } } if (!update_geo) goto end; //qDebug("updating geometry..."); geometry.setRect(target_rect, material->mapToTexture(0, roi)); if (tex_rect) { geometry.setTextureCount(tc); for (int i = 1; i < tc; ++i) { // tc can > planes, but that will compute chroma plane geometry.setTextureRect(material->mapToTexture(i, roi), i); } } update_geo = false; if (!try_vbo) goto end; { //VAO scope BEGIN #if QT_VAO if (try_vao) { //qDebug("updating vao..."); if (!vao.isCreated()) { if (!vao.create()) { try_vao = false; qDebug("VAO is not supported"); } } } QOpenGLVertexArrayObject::Binder vao_bind(&vao); Q_UNUSED(vao_bind); #endif if (!vbo.isCreated()) { if (!vbo.create()) { try_vbo = false; // not supported by OpenGL try_vao = false; // also disable VAO. destroy? qWarning("VBO is not supported"); goto end; } } //qDebug("updating vbo..."); vbo.bind(); //check here vbo.allocate(geometry.data(), geometry.size()); #if QT_VAO if (try_vao) { shader->program()->setAttributeBuffer(0, GL_FLOAT, 0, geometry.tupleSize(), geometry.stride()); shader->program()->setAttributeBuffer(1, GL_FLOAT, geometry.tupleSize()*sizeof(float), geometry.tupleSize(), geometry.stride()); if (tex_rect) { for (int i = 1; i < tc; ++i) { shader->program()->setAttributeBuffer(i + 1, GL_FLOAT, i*geometry.textureSize() + geometry.tupleSize()*sizeof(float), geometry.tupleSize(), geometry.stride()); } } char const *const *attr = shader->attributeNames(); for (int i = 0; attr[i]; ++i) { shader->program()->enableAttributeArray(i); //TODO: in setActiveShader } } #endif vbo.release(); } //VAO scope END end: #if QT_VAO if (try_vao && vao.isCreated()) { vao.bind(); return; } #endif if (try_vbo && vbo.isCreated()) { vbo.bind(); shader->program()->setAttributeBuffer(0, GL_FLOAT, 0, geometry.tupleSize(), geometry.stride()); shader->program()->setAttributeBuffer(1, GL_FLOAT, geometry.tupleSize()*sizeof(float), geometry.tupleSize(), geometry.stride()); if (tex_rect) { for (int i = 1; i < tc; ++i) { shader->program()->setAttributeBuffer(i + 1, GL_FLOAT, i*geometry.textureSize() + geometry.tupleSize()*sizeof(float), geometry.tupleSize(), geometry.stride()); } } } else { shader->program()->setAttributeArray(0, GL_FLOAT, geometry.data(0), geometry.tupleSize(), geometry.stride()); shader->program()->setAttributeArray(1, GL_FLOAT, geometry.data(1), geometry.tupleSize(), geometry.stride()); if (tex_rect) { for (int i = 1; i < tc; ++i) { shader->program()->setAttributeArray(i + 1, GL_FLOAT, geometry.data(1), i*geometry.textureSize() + geometry.tupleSize(), geometry.stride()); } } } char const *const *attr = shader->attributeNames(); for (int i = 0; attr[i]; ++i) { shader->program()->enableAttributeArray(i); //TODO: in setActiveShader } }
void OpenGLWidget::paintImg(QImage *img){ if(img==NULL) return; //create Vertices======================================== QVector4D *vertices =new QVector4D[4]; vertices[0]= QVector4D(0,0,0,1); vertices[1]= QVector4D((float)img->width(),0,0,1); vertices[2]= QVector4D((float)img->width(),(float)img->height(),0,1); vertices[3]= QVector4D(0,(float)img->height(),0,1); //end==================================================== glClear(GL_COLOR_BUFFER_BIT); shaderProgram->bind(); QOpenGLTexture * texture= new QOpenGLTexture(*img); //texture->setMagnificationFilter(QOpenGLTexture::Nearest); texture->setMagnificationFilter(QOpenGLTexture::LinearMipMapLinear); texture->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::MirroredRepeat); texture->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::MirroredRepeat); glActiveTexture(GL_TEXTURE0); texture->bind(); glUniform1i(glGetUniformLocation(shaderProgram->programId(), "texture1"), 0); shaderProgram->setUniformValue("mouse", mouse); float lensSize = 0.2; shaderProgram->setUniformValue("lensSize",static_cast<GLfloat>(lensSize)); QVector2D *resolution= new QVector2D(img->width(), img->height()); shaderProgram->setUniformValue("u_resolution",*resolution); shaderProgram->setUniformValue("EdgeThreshold",static_cast<GLfloat>(edgeThreshold)); float BillboardGrid = 0.15; shaderProgram->setUniformValue("grid",static_cast<GLfloat>(billboardGrid)); shaderProgram->setUniformValue("dividerValue",static_cast<GLfloat>(dividerValue)); float BillboardX = 0.1; shaderProgram->setUniformValue("step_x",static_cast<GLfloat>(BillboardX)); float BillboardY = 0.1; shaderProgram->setUniformValue("step_y",static_cast<GLfloat>(BillboardY)); float BillboardOpacity = 0.3; //shaderProgram->setUniformValue("qt_Opacity",static_cast<GLfloat>(BillboardOpacity)); char uniName[20]; float weights[5], sum, sigma2 = 4.0f; weights[0] = gauss(0,sigma2); sum = weights[0]; for( int i = 1; i < 5; i++ ) { weights[i] = gauss(i, sigma2); sum += 2 * weights[i]; } for( int i = 0; i < 5; i++ ) { snprintf(uniName, 20, "Weight[%d]", i); shaderProgram->setUniformValue(uniName, weights[i] / sum); } shaderProgram->setUniformValue("blurSize",static_cast<GLfloat>(blurSize)); shaderProgram->setUniformValue("LumThresh",static_cast<GLfloat>(luminosityThreshold)); shaderProgram->setUniformValue("Gamma",static_cast<GLfloat>(gamma)); float magTol = 0.3; shaderProgram->setUniformValue("magTol",static_cast<GLfloat>(magTol)); float quantize = 8.0; shaderProgram->setUniformValue("quantize",static_cast<GLfloat>(quantize)); //tansfer vertices to gpu=========================================== QOpenGLBuffer *vboVertices = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); vboVertices ->create(); vboVertices ->bind(); vboVertices ->setUsagePattern(QOpenGLBuffer::StaticDraw); vboVertices ->allocate(vertices , 4 *sizeof(QVector4D)); vboVertices->bind();//vertices is linked with a_position shaderProgram->enableAttributeArray("a_position");//vertices is linked with a_position shaderProgram->setAttributeBuffer("a_position",GL_FLOAT, 0,4,0);//vertices is linked with a_position //end=============================================================== //coordenate texture is linked with a_texCoord"==================== vboTextureCoordinate->bind(); shaderProgram->enableAttributeArray("a_texCoord"); shaderProgram->setAttributeBuffer("a_texCoord",GL_FLOAT, 0,2,0); //end=============================================================== vboIndices->bind(); glDrawElements(GL_TRIANGLES,2*3,GL_UNSIGNED_INT,0); vboIndices->release(); texture->release(); vboVertices->release(); shaderProgram->release(); delete vboVertices; delete [] vertices; delete resolution; delete texture; }