PaintGL() doesn't work
-
i want to run opengl in Qt and i used QGLWidget to do this but it doesn't work.
here is my code:
@
#include <QGLWidget>class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);virtual void initializeGL(); virtual void resizeGL(int w, int h); virtual void paintGL();
};
@@
#include "glwidget.h"
#include <gl/glut.h>GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{}void GLWidget::initializeGL()
{
setGeometry(300, 150, 640, 480);
glShadeModel(GL_FLAT);
glClearColor(0.5, 1.0, 0.2, 0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}void GLWidget::resizeGL(int w, int h)
{
if(0 == h)
h = 1;
glViewport(0, 0, (GLint)w, (GLint)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0);
//glMatirxMode(GL_MODELVIEW);
glLoadIdentity();
}void GLWidget::paintGL()
{
//qglClearColor(QColor(0,0,255,128));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.0,1.0,0.0);
glVertex3f(-1.0,-1.0,0.0);
glVertex3f(1.0,-1.0,0.0);
glEnd();
}
@@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);GLWidget *gl = new GLWidget(); gl->updateGL(); gl->show(); return a.exec();
}
@it seems that the fonction initializeGL works because the color of background changes. but i cant see my triangle.
could someone help me plz??? -
by the way, the version of my qt is 5.0.2
-
The background color is set by the glClear() call, not glClearColor(), so your paintGL() does work.
Btw. "doesn't work" is not very descriptive.
You didn't set a proper transformation matrix and you draw at depth 0.0 so your triangle falls outside of the view frustum.
For drawing a flat 2D triangle you can try this:
@
void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2f(0.0,1.0);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glEnd();
}
@ -
thx. i copied ur two functions.
but i cant still see the triangle...[quote author="Chris Kawa" date="1417448239"]The background color is set by the glClear() call, not glClearColor(), so your paintGL() does work.
Btw. "doesn't work" is not very descriptive.
You didn't set a proper transformation matrix and you draw at depth 0.0 so your triangle falls outside of the view frustum.
For drawing a flat 2D triangle you can try this:
@
void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2f(0.0,1.0);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glEnd();
}
@[/quote]