gpt4 book ai didi

c++ - qt openGL - 球体变形

转载 作者:行者123 更新时间:2023-11-28 06:18:48 24 4
gpt4 key购买 nike

我正在尝试绘制一个球体和一个围绕它旋转的立方体。我能够完美地分别绘制它们。然而,一旦我将两者画在一起,我就会得到奇怪的结果。

  • 我的球体变形了。

  • 当我有意改变立方体的角度(通过按下按钮)时,它就从我的屏幕上消失了。当我只绘制立方体然后改变角度时一切正常。

为什么?

图片:http://imgur.com/dIngayh (立方体看起来没问题,但球体太拉伸(stretch)了)

我的代码:

void MyGLWidget::paintGL()
{
glScalef(1,1,1);
setLight();
drawSun();
drawCube();
}

void MyGLWidget::initializeGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
}
void MyGLWidget::drawSun()
{
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glScalef(1,1,1);
glLoadIdentity();
GLUquadricObj *quadric=gluNewQuadric();
gluQuadricNormals(quadric, GLU_SMOOTH);
glColor3f(1,1,0);
gluSphere(quadric, 0.25, 360,360);
gluDeleteQuadric(quadric);
glPopMatrix();
}


void MyGLWidget::drawCube()
{

glTranslatef(0, 0, 0);
glRotatef(getCubeAngle(), 1.0f, 0.0f, 0.0f);
glRotatef(0, 0.0f, 1.0f, 0.0f);
glRotatef(0, 0.0f, 0.0f, 1.0f);
glTranslatef( 0.5, 0, 0);

glBegin(GL_QUADS);

//back side of cube
glColor3f(255,0,0);
glVertex3f(-0.1, 0.1,-0.1 );//upper left corner
glVertex3f(0.1, 0.1,-0.1); //uper right
glVertex3f(0.1,-0.1,-0.1 ); // down left
glVertex3f(-0.1,-0.1,-0.1); // down right


/*draws other parts of the cube in the same way*/

glEnd();
glFlush();
}
/**/

void MyGLWidget::setLight()
{
// Prepare light parameters.
float SHINE_ALL_DIRECTIONS = 1;
float lightPos[4] = {-30, 0, 0, SHINE_ALL_DIRECTIONS};
float lightColorAmbient[4] = {50, 50, 0.1, 1};
float lightColorSpecular[4] = {150, 150, 0.5, 1};

// Set light parameters.
glLightfv(GL_LIGHT1, GL_POSITION, lightPos);
glLightfv(GL_LIGHT1, GL_AMBIENT, lightColorAmbient);
glLightfv(GL_LIGHT1, GL_SPECULAR, lightColorSpecular);

// Enable lighting in GL.
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);

// Set material properties.
float rgba[3] = {0.3, 0.5, 1};
glMaterialfv(GL_FRONT, GL_AMBIENT, rgba);
glMaterialfv(GL_FRONT, GL_SPECULAR, rgba);
glMaterialf(GL_FRONT, GL_SHININESS, 0.5f);

}

最佳答案

尝试添加这个:

void MyGLWidget::resizeGL(int width, int height);
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glScalef(height *1./width, 1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

and more read here (rus)

关于c++ - qt openGL - 球体变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29705568/

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