如何在OpenGL中绘制旋转的太阳(传统)

问题描述 投票:0回答:1

我需要画一个在其原始位置/枢轴点(位于位置 (450, 450) 上)不断旋转的太阳,但它现在不再在其原始点上旋转,而是在屏幕上继续旋转 360 度,一次又一次回到原来的位置。谁知道如何解决这个问题并使太阳保持在原来的位置旋转,请修改代码。感谢这些帮助。

太阳代码

#include <windows.h>  // for MS Windows
#include <GL/glut.h>  // GLUT, include glu.h and gl.h
#include <math.h>

float sunAngle = 0.0f; // Angle for sun rotation

 // Drawing Circle
void circle(GLfloat rx, GLfloat ry, GLfloat cx, GLfloat cy)
{
    glBegin(GL_POLYGON);
    glVertex2f(cx, cy);
    for (int i = 0; i <= 360; i++)
    {
        float angle = i * 3.1416 / 180;
        float x = rx * cos(angle);
        float y = ry * sin(angle);
        glVertex2f((x + cx), (y + cy));
    }
    glEnd();
}

// Drawing Sun 
void sun()
{
    glPushMatrix();
    glRotatef(sunAngle, 0.0, 0.0, 1.0); 

    // Draw the object
    glColor3f(1.0f, 1.0f, 0.0f); // Yellow color
    circle(20, 30, 450, 450);
    glTranslatef(-450, -450, 0.0);

    glPopMatrix();
}

void update(int value) {
    // Update sun angle for rotation
    sunAngle += 1.0f;
    if (sunAngle > 360) {
        sunAngle -= 360;
    }
    // tell GLUT to call update again in 20 milliseconds
    glutTimerFunc(20, update, 0);
}

void display (void){
    glClear(GL_COLOR_BUFFER_BIT);
    //Sky Color
    glColor3ub(30, 144, 255);
    glBegin(GL_POLYGON);
    glVertex2d(0, 0);
    glVertex2d(500, 0);
    glVertex2d(500, 500);
    glVertex2d(0, 500);
    glEnd();

    sun();
    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(900, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Sun");
    init();
    glutDisplayFunc(display);
    glutTimerFunc(16, update, 0);

    glutMainLoop();
    return 0;
}
c++ opengl glut
1个回答
0
投票

glRotatef
)的旋转中心是原点。
所以,可能是,

void sun()
{
    glPushMatrix();

    glTranslatef(450, 450, 0.0);  //translate center of sun to (450,450)
    glRotatef(sunAngle, 0.0, 0.0, 1.0); 

    // Draw the object
    glColor3f(1.0f, 1.0f, 0.0f);
    circle(20, 30, 0, 0);  //here (cx,cy) is origin
    

    glPopMatrix();
}
© www.soinside.com 2019 - 2024. All rights reserved.