我一直在阅读有关采摘的教程,而颜色采摘是迄今为止最流行和最简单的形式。
以雪人为例尝试了一些教程,但它对我不起作用。当我运行该程序时,它只给我一个黑色的画面,没有任何绘画。当我单击几次时,除了关闭窗口时,什么都没有发生,然后它说“你还没有点击雪人”。
不知道有什么问题可以帮助我吗?
void GLWidget::paintGL() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(camPosx ,camPosy ,camPosz,
camPosx + camViewx,camViewy,camPosz + camViewz,
camUpx, camUpy, camUpz );
draw(); //draw the normal scene
if (mode == SELECT)
drawPickingMode();
else
drawPickingMode();
if (mode == SELECT) {
processPick();
mode = RENDER;
}
else
QGLWidget::swapBuffers();
// restore current matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix( );
}
void GLWidget::mousePressEvent(QMouseEvent * e)
{
if(e->button() == Qt::LeftButton)
{
qDebug("mouse");
qDebug("%d %d",QCursor::pos().x(),QCursor::pos().y());
this->cursorX = QCursor::pos().x(); // set x and y cord from mouse
this->cursorY = QCursor::pos().y();
mode = SELECT; // set the mode to select
}
}
void GLWidget::draw() {
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();
// Draw 4 Snowmen
glColor3f(1.0f, 1.0f, 1.0f);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++) {
glPushMatrix();
glTranslatef(i*3.0,0,-j * 3.0);
glColor3f(1.0f, 1.0f, 1.0f);
glCallList(snowman_display_list);
glPopMatrix();
}
}
void GLWidget::processPick ()
{
GLint viewport[4];
GLubyte pixel[3];
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(cursorX,viewport[3]-cursorY,1,1,
GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);
printf("%d %d %d\n",pixel[0],pixel[1],pixel[2]);
if (pixel[0] == 255)
printf ("You picked the 1st snowman on the 1st row");
else if (pixel[1] == 255)
printf ("You picked the 1st snowman on the 2nd row");
else if (pixel[2] == 255)
printf ("You picked the 2nd snowman on the 1st row");
else if (pixel[0] == 250)
printf ("You picked the 2nd snowman on the 2nd row");
else
printf("You didn't click a snowman!");
printf ("\n");
}
void GLWidget::drawPickingMode() {
// Draw 4 SnowMen
glDisable(GL_DITHER);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++) {
glPushMatrix();
// A different color for each snowman
switch (i*2+j) {
case 0: glColor3ub(255,0,0);break;
case 1: glColor3ub(0,255,0);break;
case 2: glColor3ub(0,0,255);break;
case 3: glColor3ub(250,0,250);break;
}
glTranslatef(i*3.0,0,-j * 3.0);
glCallList(snowman_display_list);
glPopMatrix();
}
glEnable(GL_DITHER);
}
void GLWidget::initializeGL() {
loadGLTextures();
//LoadXml();
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glEnable(GL_LIGHT0); // Quick And Dirty Lighting (Assumes Light0 Is Set Up)
glEnable(GL_LIGHTING); // Enable Lighting
glEnable(GL_COLOR_MATERIAL); // Enable Material Coloring
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Perspective Calculations
// buildLists(2); // Creating displaylist #
glLoadIdentity();
timer->start(50);
qDebug("Init");
}
投影矩阵设置如下:
void GLWidget::resizeGL(int width, int height) {
//set viewport
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//set persepective
//change the next line order to have a different perspective
aspect_ratio=(GLdouble)width/(GLdouble)height;
gluPerspective(45.0f, aspect_ratio, 0.1 , 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}