0
我想從這website一些簡單的(和舊的)opengl教程。在gcc中編譯它們很不錯。現在我試圖將它們放在我真正需要的地方:在QGLWidget中的Qt應用程序中。而且..我有一些小故障(彩色像素,圖像應該全是黑白)。不知道爲什麼。OpenGL毛刺與Qt(而不是GLUT)
見附件圖片:
我在Mac OS/X 10.8,Qt的4.8與QtCreator,而我編纂的基本GLUT教程有:
gcc -o test -Wall -W test.c -framework GLUT -framework OpenGL
該程序主要是一樣的,從部分替換一些GLUT與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();
}
ps。 Ubuntu的12.04在相同Qt的程序有沒有問題
@datenwold:謝謝。 所以..我曾嘗試: 1)我試圖設置 '*紋理= 0' 爲前 'glTexImage2D(GL_TEXTURE_2D,0通過一個空指針,GL_RGB,尺寸,尺寸,0,GL_RGB, 2)'for(int i = 0; i <3 * SIZE * SIZE -1; i ++){ texture [i] = 0; GL_UNSIGNED_BYTE,texture);' ..沒有任何變化 }'給出一些初始化的數組數據和..以及壞的內存訪問程序崩潰(??) – nkint
@nkint:傳遞一個0指針會使紋理未初始化,這也可能導致垃圾。關於後者:你如何定義和初始化'* texture'?它必須靜態分配,使用'new',可以是'std :: vector'或'QVector'。 – datenwolf
我忘了用新的初始化它。是'unsigned int texture [3 * SIZE * SIZE];'現在是'unsigned int * texture; .... texture = new unsigned int [3 * SIZE * SIZE];' – nkint