将射弹模拟转换为 3D 会导致绘图消失?

Converting projectile simulation to 3D causes drawing to disasppear?

我有一个代码可以在 2D 模式下成功绘制射弹,但我需要应用更改才能在 3D 模式下进行绘制。

#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <unistd.h>

#define g 9.8
#define PI 3.14
#define ESC 27



void initialize(void)
{
    glClearColor(0, 0, 0, 0);
    glColor3f(0.0, 1.0, 0.0);
    glPointSize(3.0);
    glEnable(GL_POINT_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 1000, 0, 500);
}

static void keyPressFunc(unsigned char key, int x, int y)
{
    switch(key) {
        case ESC:
            exit(1);
    }
}

void display(void)
{
    float Pheta, Pheta2, Pheta3,Pheta4, Vo, time, time_top, d1, d2, d3, Uox1, Uox2, Uox3;
    Vo = 60;
    Pheta = 60;
    Pheta2 = 30;
    Pheta3 = 40;
    Pheta4 = 50;
    time = (2 * Vo * sin(Pheta * PI / 180)) / g;
    time_top = time/2;
    d1 = 500;
    d2 = 650;
    d3 = 800;

    Uox1 = (d1 - Vo * cos(Pheta * PI / 180) * 2)/2;
    Uox2 = (d2 - Vo * cos(Pheta * PI / 180)*  time_top)/time_top;
    Uox3 = (d3 - Vo * cos(Pheta * PI / 180) * 8)/8;


    for(float t=0; t < 12 ; t += 0.0005)
    {
        float x1 = (Vo * cos(Pheta * PI / 180) * t);
        float y1 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        float x2 = (d1 - Uox1 * t);
        float y2 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        float x3 = (d2 - Uox2 * t);
        float y3 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        float x4 = (d3 - Uox3 * t);
        float y4 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        glBegin(GL_POINTS);
        glVertex2d(x1, y1);
        glVertex2d(x2, y2);
        glVertex2d(x3, y3);
        glVertex2d(x4, y4);

        if  (x1+0.1 >= x4 && x4+0.1 >= x1)
        {
            break;
        }

        glEnd();
        glFlush();
    }

}

int main(int argc, char** argv)
{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowSize(800, 600);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("C-Lang-Project");

    glutKeyboardFunc(keyPressFunc);

    initialize();
    glutDisplayFunc(display);
    glutMainLoop();
}

为了使其成为 3D,我将 glVertex2d 更改为 glVertex3d,设置变量 z 并将其添加到 glVertex3d。 我得到的最终代码:

#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <unistd.h>

#define g 9.8
#define PI 3.14
#define ESC 27


void initialize(void)
{
    glClearColor(0, 0, 0, 0);
    glColor3f(0.0, 1.0, 0.0);
    glPointSize(3.0);
    glEnable(GL_POINT_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(0, 1000, 0, 500);
}

static void keyPressFunc(unsigned char key, int x, int y)
{
    switch(key) {
        case ESC:
            exit(1);
    }
}

void display(void)
{
    float Pheta, Pheta2, Pheta3,Pheta4, Vo, time, time_top, d1, d2, d3, Uox1, Uox2, Uox3, z;
    Vo = 60;
    Pheta = 60;
    Pheta2 = 30;
    Pheta3 = 40;
    Pheta4 = 50;
    time = (2 * Vo * sin(Pheta * PI / 180)) / g;
    time_top = time/2;
    d1 = 500;
    d2 = 650;
    d3 = 800;
    z = 15;

    Uox1 = (d1 - Vo * cos(Pheta * PI / 180) * 2)/2;
    Uox2 = (d2 - Vo * cos(Pheta * PI / 180)*  time_top)/time_top;
    Uox3 = (d3 - Vo * cos(Pheta * PI / 180) * 8)/8;

    for(float t=0; t < 12 ; t += 0.0005)
    {
        float x1 = (Vo * cos(Pheta * PI / 180) * t);
        float y1 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        float x2 = (d1 - Uox1 * t);
        float y2 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        float x3 = (d2 - Uox2 * t);
        float y3 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        float x4 = (d3 - Uox3 * t);
        float y4 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

        glBegin(GL_POINTS);
        glVertex3d(x1, y1, z);
        glVertex3d(x2, y2, z);
        glVertex3d(x3, y3, z);
        glVertex3d(x4, y4, z);

        if  (x1+0.1 >= x4 && x4+0.1 >= x1)
        {
            break;
        }

        glEnd();
        glFlush();
    }

}
int main(int argc, char** argv)
{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowSize(800, 600);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("C-Lang-Project");

    glutKeyboardFunc(keyPressFunc);

    initialize();
    glutDisplayFunc(display);
    glutMainLoop();
}

但是没有显示任何错误,只是显示黑色window。

P.S。我正在使用 OpenGL 和 freeglut

场景被正射投影的近平面裁剪。

几何的 z 坐标设置为 z=15;,但正射投影设置为 gluOrtho2D(0, 1000, 0, 500);gluOrtho2D 设置近平面为-1,远平面为1。
视图 space z 坐标必须在近平面和远平面之间。
由于视图 space z 轴指向视口外,因此视图 space z 坐标为 -15.

这意味着,如果 z=15 则必须满足以下条件:

near < -15 < far 

更改正交投影以解决问题。使用 glOrtho:

例如

void initialize(void)
{
    // [...]

    glOrtho(0, 1000, 0, 500, -20, 1);
}

当然可以切换到透视投影。在这种情况下,您必须反转 z 坐标。
要在屏幕上显示所有几何图形(在剪辑 space 中),我建议增加 z 坐标的数量和(当然)到远平面的距离:

例如

void display(void)
{
    float z = -500;

    // [...]
}
void initialize(void)
{
    // [...]


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective( 90.0, 1000.0 / 500.0, 0.1, 1000.0 );
}

不要在事件处理循环中实现渲染循环。使用glutPostRedisplay强制重绘显示:

float t=0;

void display(void)
{
    float Pheta, Pheta2, Pheta3,Pheta4, Vo, time, time_top, d1, d2, d3, Uox1, Uox2, Uox3, z;
    Vo = 60;  Pheta = 60; Pheta2 = 30; Pheta3 = 40; Pheta4 = 50;
    time = (2 * Vo * sin(Pheta * PI / 180)) / g;
    time_top = time/2; d1 = 500; d2 = 650; d3 = 800;
    z = 15;

    Uox1 = (d1 - Vo * cos(Pheta * PI / 180) * 2)/2;
    Uox2 = (d2 - Vo * cos(Pheta * PI / 180)*  time_top)/time_top;
    Uox3 = (d3 - Vo * cos(Pheta * PI / 180) * 8)/8;

    float x1 = (Vo * cos(Pheta * PI / 180) * t);
    float y1 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);
    float x2 = (d1 - Uox1 * t);
    float y2 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);
    float x3 = (d2 - Uox2 * t);
    float y3 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);
    float x4 = (d3 - Uox3 * t);
    float y4 = (Vo * sin(Pheta * PI / 180) * t - 0.5 * g * t * t);

    glBegin(GL_POINTS);
    glVertex3d(x1, y1, z);
    glVertex3d(x2, y2, z);
    glVertex3d(x3, y3, z);
    glVertex3d(x4, y4, z);
    glEnd();

    t += 0.0005;

    glFlush();
    glutPostRedisplay();
}