我有一个代码,成功地绘制了二维的射弹,但我需要应用更改,使之在三维。

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

但它没有显示任何错误,只是显示一个黑色的窗口。
P.S.我正在使用OpenGL和freeglut

最佳答案

场景由正交投影的近平面剪裁。
大地测量的坐标设置为z,但正投影设置为z=15;gluOrtho2D(0, 1000, 0, 500);将近平面设置为-1,远平面设置为1。
视图空间gluOrtho2D坐标必须介于近平面和远平面之间。
由于视图空间z轴从视口中指向,视图空间z坐标为-15。
这意味着,如果z,则必须满足以下条件:

near < -15 < far

更改正交投影以解决问题。使用z=15
例如
void initialize(void)
{
    // [...]

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

当然可以切换到透视投影。在这种情况下,必须反转和z坐标。
要获得屏幕上的所有几何图形(在剪辑空间中),我建议增加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 );
}

不要在事件处理循环中实现呈现循环。使用glOrtho强制重新绘制显示器:
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();
}

关于c - 将射弹模拟转换为3D会导致图形消失?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56026511/

10-16 06:46