我有一个可以成功以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();
}
但是它没有显示任何错误,只是显示了一个黑色的窗口。
P.S。我正在使用OpenGL和freeglut
答案 0 :(得分:2)
场景被正交投影的近平面裁剪。
将地标的z
坐标设置为z=15;
,但将正投影设置为gluOrtho2D(0, 1000, 0, 500);
。 gluOrtho2D
将近平面设为-1,将远平面设为1。
视图空间z
坐标必须在近平面和远平面之间。
由于视图空间z轴指向视口之外,因此视图空间z坐标为-15。
这意味着,如果z=15
,则必须满足以下条件:
near < -15 < far
更改正射投影以解决该问题。使用glOrtho
:
例如
void initialize(void)
{
// [...]
glOrtho(0, 1000, 0, 500, -20, 1);
}
当然可以切换到透视投影。在这种情况下,您必须反转and坐标。
要在屏幕上(在剪辑空间中)获得所有几何图形,我建议增加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();
}