/// @overload QGLWidget void paintGL(){ vao.bind(); program.bind(); { ///--- Update modelview #ifdef WITH_QGLVIEWER /// use the trackball to specify the matrices setup_modelview(camera(), program); #else ///--- simple unit cube orthographic view static Eigen::Matrix4f M = Eigen::Matrix4f::Identity(); static Eigen::Matrix4f P = Eigen::ortho(-1.0f, +1.0f, -1.0f, +1.0f, -1.0f, +1.0f); static Eigen::Matrix4f V = Eigen::Matrix4f::Identity(); static Eigen::Matrix4f MV = V*M; static Eigen::Matrix4f MVP = P*MV; GLint MVP_id = glGetUniformLocation(program.programId(), "MVP"); glUniformMatrix4fv(MVP_id, 1, GL_FALSE, MVP.data()); GLint MV_id = glGetUniformLocation(program.programId(), "MV"); glUniformMatrix4fv(MV_id, 1, GL_FALSE, MV.data()); #endif ///--- clear & draw glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES, triangles.size(), GL_UNSIGNED_INT, 0); } vao.release(); program.release(); }
/// @overload QGLWidget void paintGL(){ program.bind(); vao.bind(); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); ///< we have 4 verts vao.release(); program.release(); }
/// @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); }
void SingleTri::initializeGL() { glClearColor(0.0f, 0.25f, 0.0f, 1.0f); const char * vs_source = "#version 130 \n" " \n" "void main(void) \n" "{ \n" " const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), \n" " vec4(-0.25, -0.25, 0.5, 1.0), \n" " vec4( 0.25, 0.25, 0.5, 1.0)); \n" " \n" " gl_Position = vertices[gl_VertexID]; \n" "} \n"; const char * fs_source = "#version 130 \n" " \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = vec4(0.0, 0.8, 1.0, 1.0); \n" "} \n"; QOpenGLShader* fs = new QOpenGLShader(QOpenGLShader::Fragment); fs->compileSourceCode(fs_source); QOpenGLShader* vs = new QOpenGLShader(QOpenGLShader::Vertex); vs->compileSourceCode(vs_source); program = new QOpenGLShaderProgram(this); program->addShader(vs); program->addShader(fs); QOpenGLVertexArrayObject* vao = new QOpenGLVertexArrayObject(this); vao->bind(); program->bind(); }
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 FragColorFromPos::initializeGL() { glClearColor(0.0f, 0.25f, 0.0f, 1.0f); const char * vsInterpolate_source = "#version 130 \n" " \n" "void main(void) \n" "{ \n" " const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), \n" " vec4(-0.25, -0.25, 0.5, 1.0), \n" " vec4( 0.25, 0.25, 0.5, 1.0)); \n" " \n" " gl_Position = vertices[gl_VertexID]; \n" "} \n"; const char * fsInterpolate_source = "#version 130 \n" " \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = vec4(sin(gl_FragCoord.x * 0.25) * 0.5 + 0.5, \n" " cos(gl_FragCoord.y * 0.25) * 0.5 + 0.5, \n" " sin(gl_FragCoord.x * 0.15) * cos(gl_FragCoord.y * 0.1), \n" " 1.0); \n" "} \n"; QOpenGLShader* fs = new QOpenGLShader(QOpenGLShader::Fragment); fs->compileSourceCode(fsInterpolate_source); QOpenGLShader* vs = new QOpenGLShader(QOpenGLShader::Vertex); vs->compileSourceCode(vsInterpolate_source); interpolateProgram = new QOpenGLShaderProgram(this); interpolateProgram->addShader(vs); interpolateProgram->addShader(fs); interpolateProgram->link(); const char * vs_source = "#version 130 \n" " \n" "out vec4 vs_color; \n" "void main(void) \n" "{ \n" " const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), \n" " vec4(-0.25, -0.25, 0.5, 1.0), \n" " vec4( 0.25, 0.25, 0.5, 1.0)); \n" " const vec4 colors[] = vec4[](vec4(1.0, 0.0, 0.0, 1.0), \n" " vec4(0.0, 1.0, 0.0, 1.0), \n" " vec4(0.0, 0.0, 1.0, 1.0)); \n" " \n" " gl_Position = vertices[gl_VertexID]; \n" " vs_color = colors[gl_VertexID]; \n" "} \n"; const char * fs_source = "#version 130 \n" " \n" "in vec4 vs_color; \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = vs_color; \n" "} \n"; fs->compileSourceCode(fs_source); vs->compileSourceCode(vs_source); nonInterpolateProgram = new QOpenGLShaderProgram(this); nonInterpolateProgram->addShader(vs); nonInterpolateProgram->addShader(fs); QOpenGLVertexArrayObject* vao = new QOpenGLVertexArrayObject(this); vao->bind(); nonInterpolateProgram->bind(); }
/// @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(); }
void TessellatedTri::initializeGL() { glClearColor(0.0f, 0.25f, 0.0f, 1.0f); const char * vs_source = "#version 420 core \n" " \n" "void main(void) \n" "{ \n" " const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), \n" " vec4(-0.25, -0.25, 0.5, 1.0), \n" " vec4( 0.25, 0.25, 0.5, 1.0)); \n" " \n" " gl_Position = vertices[gl_VertexID]; \n" "} \n"; const char * fs_source = "#version 420 core \n" " \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = vec4(0.0, 0.8, 1.0, 1.0); \n" "} \n"; const char * tcs_source = "#version 420 core \n" " \n" "layout (vertices = 3) out; \n" " \n" "void main(void) \n" "{ \n" " if (gl_InvocationID == 0) \n" " { \n" " gl_TessLevelInner[0] = 5.0; \n" " gl_TessLevelOuter[0] = 5.0; \n" " gl_TessLevelOuter[1] = 5.0; \n" " gl_TessLevelOuter[2] = 5.0; \n" " } \n" " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; \n" "} \n"; const char * tes_source = "#version 420 core \n" " \n" "layout (triangles, equal_spacing, cw) in; \n" " \n" "void main(void) \n" "{ \n" " gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + \n" " (gl_TessCoord.y * gl_in[1].gl_Position) + \n" " (gl_TessCoord.z * gl_in[2].gl_Position); \n" "} \n"; QOpenGLShader* fs = new QOpenGLShader(QOpenGLShader::Fragment, this); fs->compileSourceCode(fs_source); QOpenGLShader* vs = new QOpenGLShader(QOpenGLShader::Vertex, this); vs->compileSourceCode(vs_source); QOpenGLShader* tcs = new QOpenGLShader(QOpenGLShader::TessellationControl, this); tcs->compileSourceCode(tcs_source); QOpenGLShader* tes = new QOpenGLShader(QOpenGLShader::TessellationEvaluation, this); tes->compileSourceCode(tes_source); program = new QOpenGLShaderProgram(this); program->addShader(vs); program->addShader(tcs); program->addShader(tes); program->addShader(fs); program->link(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); QOpenGLVertexArrayObject* vao = new QOpenGLVertexArrayObject(this); vao->bind(); program->bind(); }