"#version 130 \n"
C++ (Qt)#include <QGLFormat>#include <QGLWidget>#include <QString>#include <iostream>#include <QApplication> int main( int argc, char *argv[] ){ QApplication app( argc, argv ); QGLFormat format; format.setVersion( 4, 3 ); QGLFormat::setDefaultFormat( format ); QGLWidget w; w.makeCurrent(); QString vendor( QLatin1String( reinterpret_cast<const char*>( glGetString( GL_VENDOR ) ) ) ); std::cout << "Vendor: " << vendor.toStdString() << std::endl; QString renderer( QLatin1String( reinterpret_cast<const char*>( glGetString( GL_RENDERER ) ) ) ); std::cout << "Renderer: " << renderer.toStdString() << std::endl; QString versionOfGL( QLatin1String( reinterpret_cast<const char*>( glGetString( GL_VERSION ) ) ) ); std::cout << "Version: " << versionOfGL.toStdString() << std::endl; QString versionGLSL( QLatin1String( reinterpret_cast<const char*>( glGetString( GL_SHADING_LANGUAGE_VERSION ) ) ) ); std::cout << "GLSL: "<< versionGLSL.toStdString() << std::endl; return app.exec();}
C++ (Qt)#include "singletri.h"#include <QGLShader>#include <QGLShaderProgram>#include <QOpenGLVertexArrayObject>#include <QPainter> SingleTri::SingleTri(QWidget *parent) : QGLWidget(parent){ setWindowTitle("Single Triangle");} void SingleTri::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"; QGLShader* fs = new QGLShader(QGLShader::Fragment); fs->compileSourceCode(fs_source); QGLShader* vs = new QGLShader(QGLShader::Vertex); vs->compileSourceCode(vs_source); program = new QGLShaderProgram(this); program->addShader(vs); program->addShader(fs); QOpenGLVertexArrayObject* vao = new QOpenGLVertexArrayObject(this); vao->bind(); program->bind();} void SingleTri::paintGL(){ glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 3);} void SingleTri::resizeGL(int w, int h){ glViewport(0, 0, w, h);}
C++ (Qt) 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";
C++ (Qt)#include "movingtri.h"#include <QGLShader>#include <QGLShaderProgram>#include <QOpenGLVertexArrayObject>#include <QPainter>#include <QTime>#include <math.h>#include <QVector4D> MovingTri::MovingTri(QWidget *parent) : QGLWidget(parent){ setWindowTitle("Moving Triangle"); time = new QTime; time->start(); startTimer(33);} MovingTri::~MovingTri(){ delete time;} void MovingTri::initializeGL(){ const char * vs_source = "#version 410 core \n" " \n" "layout (location = 0) in vec4 offset; \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" " // Add 'offset' to our hard-coded vertex position \n" " gl_Position = vertices[gl_VertexID] + offset; \n" "} \n"; const char * fs_source = "#version 410 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"; QGLShader* fs = new QGLShader(QGLShader::Fragment); fs->compileSourceCode(fs_source); QGLShader* vs = new QGLShader(QGLShader::Vertex); vs->compileSourceCode(vs_source); program = new QGLShaderProgram(this); program->addShader(vs); program->addShader(fs); program->link(); QOpenGLVertexArrayObject* vao = new QOpenGLVertexArrayObject(this); vao->bind(); program->bind();} void MovingTri::paintGL(){ GLfloat currentTime = time->elapsed() / 1000.0f; glClearColor(sinf(currentTime) * 0.5f + 0.5f, cosf(currentTime) * 0.5f + 0.5f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); QVector4D attrib ( sinf(currentTime) * 0.5f, cosf(currentTime) * 0.6f, 0.0f, 0.0f ); program->setAttributeValue(0, attrib); glDrawArrays(GL_TRIANGLES, 0, 3);} void MovingTri::resizeGL(int w, int h){ glViewport(0, 0, w, h);} void MovingTri::timerEvent(QTimerEvent *){ updateGL();}