我正在从这个网站尝试一些简单的(和旧的)opengl 教程。在 gcc 中编译它们效果很好。现在我试着把它们放在我真正需要的地方:在 QGLWidget 内的 Qt 应用程序中。而且..我有一些小故障(彩色像素,图像应该全是黑白的)。不知道为什么。
见附图:
我在 Mac OS/X 10.8、Qt 4.8 和 QtCreator 上,我正在编译基本的 GLUT 教程:
gcc -o test -Wall -W test.c -framework GLUT -framework OpenGL
该程序主要是相同的,部分来自替换一些 GLUT vs Qt .. 无论如何,这里是 cpp 文件:
#include "glwidget.h"
#include <QTimer>
#include <iostream>
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
QTimer *t = new QTimer(this);
connect(t, SIGNAL(timeout()), this, SLOT(update()));
t->start(1000.0f/30.0f); // 30 FPS
}
void GLWidget::initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(1, 1, 1, 0);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glEnable(GL_LINE_SMOOTH);
//glEnable(GL_POINT_SMOOTH);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SIZE, SIZE, 0, GL_RGB,
GL_UNSIGNED_BYTE, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
void GLWidget::resizeGL(int w, int h)
{
h = h ? h : 1;
glViewport(0, 0, w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w/(GLfloat)h, 0.1, 8000.0f);
glMatrixMode(GL_MODELVIEW);
}
float rotation=20;
void GLWidget::paintGL()
{
makeCurrent();
//glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//glLoadIdentity();
glLoadIdentity();
glTranslatef(0, 0, -10);
glRotatef(rotation, 1, 0, 0);
glRotatef(20 , 0, 1, 0);
/* Define a view-port adapted to the texture */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20, 1, 5, 15);
glViewport(0, 0, SIZE, SIZE);
glMatrixMode(GL_MODELVIEW);
/* Render to buffer */
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Cube();
glFlush();
/* Copy buffer to texture */
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 5, 5, 0, 0, SIZE - 10, SIZE - 10);
/* Render to screen */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20, width() / (float) height(), 5, 15);
glViewport(0, 0, width(), height());
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Cube();
/* End */
glFlush();
/* Update again and again */
rotation += 0.1;
}
void GLWidget::Cube()
{
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, -1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, -1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, 1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, 1);
glEnd();
}
附言。Ubuntu 12.04下同样的Qt程序没问题