Delta Zeta不适用于gltranslatef [英] Delta zeta not applied by gltranslatef

查看:75
本文介绍了Delta Zeta不适用于gltranslatef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序应模拟一个绕着另一个行星旋转的行星.
我使用gltranslatef来让行星绕更大的行星运动,但是问题是,当dz为-0.5时,行星应该隐藏在更大的行星上方.
但是,如果我测试该程序,我总会看到红色的星球超过蓝色的星球.
我还有另一个问题:行星自转太快,我该如何放慢速度?

The program should simulate a planet rotating around another planet.
I use gltranslatef to let the planet move around the bigger planet, but the problem is that the planet should hide when is over the bigger planet, because dz is -0.5.
But if I test the program I always see the red planet over the blue one.
Another problem I have: the planet rotates too fast, how do I slow it?

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "utility.h"

GLfloat dx=0.0;
GLfloat dz=-0.5;
bool plus=true;

void init()
{
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
    glEnable(GLUT_DEPTH);
}

void render()
{
    glClearColor(BLACK);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor4f(BLUE);
    glutWireSphere(0.25, 100, 100);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(-0.5+dx, 0.0, -dz);
    glColor4f(RED);
    glutWireSphere(0.05, 100, 100);
    glPopMatrix();
    glFlush();
}


void idle()
{
    if(plus)
    {
        dx+=0.05;
    }
    else
    {
        dx-=0.05;
    }
    if(dx>=1.0)
    {
        dx=0.5;
        plus=false;
    }
    else if(dx<=-0.0)
    {
        dx=0.0;
        plus=true;
    }
    glutPostRedisplay();
}



int main(int argc, const char * argv[])
{
    glutInit(&argc, (char**)argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(150, 150);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Simple");
    glutIdleFunc(idle);
    init();
    glutDisplayFunc(render);
    glutMainLoop();
    return 0;
}

我还不太了解空闲函数是如何工作的,为什么它被调用了那么多次?我不能选择调用空闲函数的时间间隔吗?

I haven't understood well how does the idle function work, why it gets called so many times? Can't I choose a time interval with which the idle function gets called?

更多信息:RED和BLUE是RGB浮点,在Utility.h头文件中定义.
加号是用来了解我是否必须降低或增加dx的布尔值.

More info: RED and BLUE are RGB floats, defined in the utility.h header file.
plus is a bool that is used to know if I have to decrease or increase dx.

推荐答案

试一下:

#include <GL/glut.h>

double GetSeconds()
{
    return glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
}

void render()
{
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3ub(0,0,255);
    glutWireSphere(0.25, 100, 100);

    glPushMatrix();
    glLoadIdentity();

    static double prv = GetSeconds();
    double cur = GetSeconds();
    double delta = cur - prv;
    prv = cur;

    const float DEG_PER_SEC = 60.0f;
    static float angle = 0.0f;
    angle += DEG_PER_SEC * delta;
    while( angle > 360 ) angle -= 360;

    glPushMatrix();
    glRotatef( angle, 0, 1, 0 );
    glTranslatef( 0.5, 0, 0);
    glColor3ub(255,0,0);
    glutWireSphere(0.05, 100, 100);
    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}

int main(int argc, const char * argv[])
{
    glutInit(&argc, (char**)argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(150, 150);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Simple");
    glutReshapeFunc(reshape);
    glutTimerFunc(0, timer, 0);
    glutDisplayFunc(render);

    glEnable( GL_DEPTH_TEST );

    glutMainLoop();
    return 0;
}

重要部分:

  • 显式glMatrixMode()通话

glutCreateWindow()

双缓冲需要glutSwapBuffers()

通过GL_DEPTH_BUFFER_BIT

glEnable( GL_DEPTH_TEST )

glRotatef()用于行星旋转

基于计时器的动画

这篇关于Delta Zeta不适用于gltranslatef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆