gpt4 book ai didi

c++ - 在 OpenGL 中使用 Qt 拾色

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:45 25 4
gpt4 key购买 nike

我一直在阅读有关挑选的教程,颜色挑选是最流行和最简单的形式。

尝试了一些以雪人为例的教程,但它对我不起作用。当我运行该程序时,它只给我一个黑色的画面,上面没有任何东西。当我点击几次时没有任何反应,除非我关闭窗口然后它说“你没有点击雪人”。

不知道它有什么问题,有人可以帮助我吗?

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();
}

最佳答案

虽然您没有明确指定它,但您可能正在渲染到后台缓冲区双缓冲窗口。因此,您应该在调用 glReadPixels 之前调用 glReadBuffer 以设置从哪个缓冲区读取颜色。另请记住,屏幕上的窗口会受到像素所有权测试和其他可能会干扰您选择方法的因素的影响。

关于c++ - 在 OpenGL 中使用 Qt 拾色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10754568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com