Last active
July 17, 2019 01:38
-
-
Save royshil/8df3047671b99b6a98e2c46c78b925fc to your computer and use it in GitHub Desktop.
Simple example of OpenGL 4.1 in a QGLWidget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* GLWidgetOpenGL4Example.h | |
* | |
* Created on: Apr 23, 2016 | |
* Author: roy_shilkrot | |
* | |
*/ | |
class GLWidgetOpenGL4Example : public QGLWidget { | |
public: | |
GLWidgetOpenGL4Example(const QGLFormat& fmt):QGLWidget(fmt),vaoId(0),elementBuffer(0) {} | |
protected: | |
virtual void paintGL(); | |
virtual void initializeGL(); | |
virtual void resizeGL(int width, int height) { glViewport(0, 0, width, height); } | |
private: | |
const int vertexLocationInShader = 0; | |
GLuint vaoId; | |
GLuint elementBuffer; | |
QGLShaderProgram shaderProgram; | |
}; | |
void GLWidgetOpenGL4Example::paintGL() { | |
glBindVertexArray(vaoId); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); | |
shaderProgram.bind(); | |
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | |
shaderProgram.release(); | |
glBindVertexArray(0); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
} | |
void GLWidgetOpenGL4Example::initializeGL() { | |
qInfo() << "Widget OpenGl: " << format().majorVersion() << "." << format().minorVersion(); | |
qInfo() << "Context valid: " << context()->isValid(); | |
qInfo() << "Really used OpenGl: " << context()->format().majorVersion() << "." << context()->format().minorVersion(); | |
qInfo() << "OpenGl information: VENDOR: " << (const char*)glGetString(GL_VENDOR); | |
qInfo() << " RENDERDER: " << (const char*)glGetString(GL_RENDERER); | |
qInfo() << " VERSION: " << (const char*)glGetString(GL_VERSION); | |
qInfo() << " GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION); | |
glClearColor(0.0, 1.0, 0.0, 1.0); | |
//Faces: | |
vector<GLuint> f = { | |
0, 1, 2, | |
0, 2, 3 | |
}; | |
//Vertices: | |
vector<GLfloat> v = { | |
0.1, 0.1, 0.0, // 0 | |
0.1, 0.9, 0.0, // 1 | |
0.9, 0.9, 0.0, // 2 | |
0.9, 0.1, 0.0 // 3 | |
}; | |
qInfo() << "VAO: generate Vertex Array"; | |
glGenVertexArrays(1,&vaoId); | |
glBindVertexArray(vaoId); | |
{ | |
// buffer for faces | |
glGenBuffers(1, &elementBuffer); | |
qInfo("VAO %d: new elements (faces) buffer %d: %d faces", vaoId, elementBuffer, f.size()/3); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * f.size(), &(f[0]), GL_STATIC_DRAW); | |
} | |
// buffer for vertex positions | |
{ | |
GLuint buffer; | |
glGenBuffers(1, &buffer); | |
qInfo("VAO %d: new position / vertex buffer %d: %d vertices", vaoId, buffer, v.size()/3); | |
glBindBuffer(GL_ARRAY_BUFFER, buffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * v.size(), &(v[0]), GL_STATIC_DRAW); | |
glVertexAttribPointer(vertexLocationInShader, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
glEnableVertexAttribArray(vertexLocationInShader); | |
} | |
glBindVertexArray(0); | |
const char *vsrc = | |
"uniform mat4 projectionMatrix; \n" | |
"uniform mat4 modelviewMatrix; \n" | |
" \n" | |
"layout(location = 0) in vec3 inPosition; \n" | |
"void main(void) \n" | |
"{ \n" | |
" gl_Position = projectionMatrix * \n" | |
" modelviewMatrix * \n" | |
" inPosition; \n" | |
"} \n"; | |
const char *fsrc = | |
"layout(location = 0, index = 0) out vec4 fragColor; \n" | |
"void main(void) \n" | |
"{ \n" | |
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" | |
"} \n"; | |
shaderProgram.addShaderFromSourceCode(QGLShader::Vertex, vsrc); | |
shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, fsrc); | |
shaderProgram.bindAttributeLocation("inPosition", vertexLocationInShader); | |
shaderProgram.link(); | |
//setup ortho projection | |
shaderProgram.bind(); | |
QMatrix4x4 m; | |
shaderProgram.setUniformValue("modelviewMatrix", m); | |
m.ortho(0, 1, 0, 1, 0, -1); | |
shaderProgram.setUniformValue("projectionMatrix", m); | |
shaderProgram.release(); | |
} | |
int main(int argc, char** argv) { | |
// Read command lines arguments. | |
QApplication application(argc, argv); | |
//Important: Setup the Core profile (and not the compatability profile, OpenGL 2.1) | |
QGLFormat fmt = QGLFormat::defaultFormat(); | |
fmt.setVersion(4, 1); | |
fmt.setProfile(QGLFormat::CoreProfile); | |
// Instantiate the viewer. | |
GLWidgetOpenGL4Example glwidgetExample(fmt); | |
glwidgetExample.setWindowTitle("QGLWidget OpenGL 4 Example"); | |
// Make the viewer window visible on screen. | |
glwidgetExample.show(); | |
// Run main loop. | |
return application.exec(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment