多重采样背景不显示

MultiSampling Background Not Displaying

本文关键字:显示 背景 重采样      更新时间:2023-10-16

我试图用恒星的背景来说明海卫一和普罗透斯围绕海王星的轨道。我决定通过对白点(星星(数组进行多重采样作为背景来绘制模板背景。有人可以解释为什么我的数组没有显示吗?

*背景在Init()中创建Display()调用。

完整代码在这里:

int triton = 0;
int proteus = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
// Material Specs
GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
GLfloat mat_shininess[] = { 40.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };
// Light 0 Initialized.
GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Light 0 implementation
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);

// Ambient surrounding light on object.
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

// Background (Stars: In Progress)
glNewList(1, GL_COMPILE);   
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 1000; j++)
{
if (((i + j) % 2) == 0)
{
glVertex2f(2*i, 2*j);
}
}
}
glEnd();
glEndList();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
// Neptune
glColor3f(0.1, 0.1, 0.3); 
glutSolidSphere(2.0, 100, 100);
// Triton
glPushMatrix();
glColor3f(0.9, 0.7, 0.8); 
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(2.5, 0.0, 0.0);
glRotatef((GLfloat)triton, 1.0, 0.0, 0.0);
glutSolidSphere(0.35, 100, 100);
glPopMatrix();
// Proteus
glPushMatrix();     
glColor3f(1.0, 1.0, 1.0); 
glRotatef((GLfloat)proteus, 0.7, -0.4, 1.0);
glTranslatef(3.5, 0.0, 0.0);
glRotatef((GLfloat)proteus, 1.0, 0.0, 0.0);
glutSolidSphere(0.1, 100, 100);
glPopMatrix();
// Stars Background
glEnable(GL_MULTISAMPLE);
glPushMatrix();
glCallList(1); // Not Coding
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
// Triton + Proteus Orbit. 
case 'a':
triton = (triton - 2) % 360;
proteus = (proteus - 5) % 360;
glutPostRedisplay();
break;
case 'd':
triton = (triton + 2) % 360;
proteus = (proteus + 5) % 360;
glutPostRedisplay();
break;
case ',': 
glTranslatef(- 0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case 27: 
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Neptune and Space");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

你画了起点,然后设置透视投影矩阵和视图矩阵。"星星"不在视口上。

使用大约 1/500 的比例将点放入剪辑空间:

glVertex2f(2*i / 500.0f, 2*j  / 500.0f);

或者在绘制星星之前设置正交投影:

// Stars Background
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 1000, 0, 1000 );
glCallList(1);
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();