C语言 我的立方体在opengl中显示不正确

rmbxnbpk  于 2023-04-05  发布在  其他
关注(0)|答案(1)|浏览(100)

我有代码,模拟投掷立方体,但它没有绘制正确(更远的一面是悬停近侧)。

怎么了?我尝试使用不同的函数和参数,这与z缓冲区,但它没有帮助。它的黑屏或不正确显示。

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
float z = 7.0f, y = 0, x = 0;
float v0 = 0.2;
float alfa = 5;
float fi = 5;
float r = 1;
float dt = 0.01;
float g = 9.80665f;
float t = 0;
float omega = 0;
float vY = 0, vX = 0, vZ = 0, Y = 0, X = 0, Z = 0;
float ymax = 0, xmax = 0, zmax = 0, tD = 0, tm = 0;
void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
gluLookAt(xmax + 10, zmax, ymax+10, 
    0.0, 0.0, 0.0,    
    0.0, 2, 0.0);   

glPushMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(333, 0.0f);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.001f, 0.0f);
glVertex2f(0.001f, 333);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.001f, 0.001f, 0.0f);
glVertex3f(0.001f, 0.001f, 333);
glEnd();
glPopMatrix();

glPushMatrix();
static float angle = 0.0f;
angle += omega;

// glTranslatef(X-xmax-r, Z-zmax, Y-(xmax+ymax+zmax));
glTranslatef(X, Z, Y);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glScalef(r, r, r);
glBegin(GL_QUADS);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);

glColor3f(0.5f, 1.0f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);

glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);

glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);

glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);

glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);

glEnd();
//   glPopMatrix();
glutSwapBuffers();
}

void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(15, timer, 0);
if (Z >= 0) {
    vX = v0 * cos(alfa * (3.14 / 180)) * cos(fi * (3.14 / 180));
    vY = v0 * cos(alfa * (3.14 / 180)) * sin(fi * (3.14 / 180));
    vZ = v0 * sin(alfa * (3.14 / 180))- g * t;
    Z = z + (v0 * sin(alfa * (3.14 / 180))) * t - 1 / 2.0 * g * t * t; 
    X = t * vX;
    Y = t * vY;
}
t += dt;

}
void obsluhaReshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width == 0) width++;
const float aspect_ratio = (float)(width) / (height);
gluPerspective(100, aspect_ratio, 0, zmax + 10);
}
int main(int argc, char** argv)
{
z = atof(argv[1]);
v0 = atof(argv[2]);
alfa = atof(argv[3]);
fi = atof(argv[4]);
r = atof(argv[5]);
omega = atof(argv[6]);

tD = (v0 * sin(alfa * (3.14 / 180)) + sqrt(pow(v0 * sin(alfa * (3.14 / 180)), 2) + 2 * g * z)) / g;
vX = v0 * cos(alfa * (3.14 / 180)) * cos(fi * (3.14 / 180));
vY = v0 * cos(alfa * (3.14 / 180)) * sin(fi * (3.14 / 180));
zmax = z + (v0* v0* sin(alfa * (3.141592 / 180))* sin(alfa * (3.141592 / 180))) / (2 * g);
xmax = tD*vX;
ymax = tD * vY;

printf("tD = %.2fs\nxmax = %.2fm\nymax = %.2fm\nzmax = %.2fm\n", tD, xmax, zmax, ymax);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 640);
glutCreateWindow("DU6");
glClearDepth(100.0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glutReshapeFunc(obsluhaReshape);
glutMainLoop();
return 0;
q8l4jmvw

q8l4jmvw1#

gluPerspective(100, aspect_ratio, 0, zmax + 10);
                                  ^ nope

永远不要设置gluPerspective()to zerozNear参数:
深度缓冲区精度受为zNearzFar指定的值的影响。zFarzNear的比率越大,深度缓冲区在区分彼此靠近的表面时的效果越差。如果
r = zFar / zNear
深度缓冲区精度的大约log2(r)位丢失。因为当zNear接近0时r接近无穷大,所以zNear永远不能设置为0。
因此,为了最大化深度缓冲精度,您应该拍摄不会导致剪切的最大值。对于像您这样的小的近距离场景,0.10.01应该可以工作。

相关问题