Created
January 30, 2020 13:24
-
-
Save geotavros/d39644457f4f39605dd39d12591d1f99 to your computer and use it in GitHub Desktop.
Qt OpenGL Minimal QGLWidget Implementation, Drawing Triangle
This file contains 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
#include <QApplication> | |
#include <QGLWidget> | |
class GLWidget : public QGLWidget { | |
public: | |
GLWidget(QWidget *parent = 0); | |
~GLWidget(); | |
QSize sizeHint() const { return QSize(400, 400); } | |
protected: | |
void initializeGL(); | |
void paintGL(); | |
void resizeGL(int width, int height); | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc, argv); | |
GLWidget glWidget; | |
glWidget.show(); | |
return app.exec(); | |
} | |
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {} | |
GLWidget::~GLWidget() { } | |
void GLWidget::initializeGL() { } | |
void GLWidget::paintGL() | |
{ | |
QSize viewport_size = size(); | |
glViewport(0, 0, viewport_size.width(), viewport_size.height()); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glFrustum(-1, 1, -1, 1, 5, 7); // near and far match your triangle Z distance | |
glMatrixMode(GL_MODELVIEW); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
glTranslatef(-1.5f, 0.0f, -6.0f); | |
glBegin(GL_TRIANGLES); | |
glColor3f(1, 0, 0); | |
glVertex3f(0.0f, 1.0f, 0.0f); | |
glColor3f(0.0, 1, 0); | |
glVertex3f(-1.0f, -1.0f, 0.0f); | |
glColor3f(0.0, 0.0, 1); | |
glVertex3f(1.0f, -1.0f, 0.0f); | |
glEnd(); | |
} | |
void GLWidget::resizeGL(int w, int h) | |
{ | |
QGLWidget::resize(w, h); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment