我正在使用 Qt 和 OpenGL 开发一个非常简单的应用程序。该应用程序包含一些用于交互的按钮和一个包含 3 个页面的小部件选项卡,前两个用于分析和处理一些数据,第三个包含GLWidget
应该在 3d 空间中绘制结果点的一个。
一开始 3d 空间不包含任何点,在处理完所有数据后,它应该绘制 3 个点。
到目前为止,我已经尝试调用一个外部函数来paintGL()
在场景中心绘制一个实心立方体。该方法被正确调用,但小部件不显示任何立方体。
这是代码glwidget.cpp
:
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL()));
timer.start(1000);
}
void GLWidget::initializeGL(int w, int h) {
glClearColor(0.2f, 0.2f, 0.2f, 1);
glViewport(0, 0, w, h);
// Setup light
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = {2.0f, 5.0f, 5.0f, 0.0f };
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_DOUBLE);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDepthFunc(GL_LESS);
glEnable(GL_COLOR_MATERIAL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 0.0001, 1000.0);
glMatrixMode(GL_MODELVIEW);
current_view= TOP_VIEW;
}
// Display
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
setView();
if (current_view == EXPLORING_VIEW)
calculate_lookpoint();
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
// Draw axis and add points
drawAxis();
int ind;
Skyline s;
if (s.skylineGenerated()) {
drawCube();
cout << "Generated" << endl;
}
swapBuffers();
}
void GLWidget::drawCube(void){
cout << "Drawn\n";
glTranslatef(0.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
glutSolidCube(2.0);
update();
}
// Reshape
void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (48.0, (GLfloat) w/(GLfloat) h, 10.0, 800.0);
glMatrixMode (GL_MODELVIEW);
}