#include <GL/glut.h>
#include <gl/glext.h>
#include <stdlib.h>
#include <stdio.h>
#define GL_DISTANCE_ATTENUATION_EXT 0x8129
#define GL_POINT_FADE_THRESHOLD_SIZE_EXT 0x8128
#define GL_POINT_SIZE_MIN_EXT 0x8126
#define GL_POINT_SIZE_MAX_EXT 0x8127
#define GL_POINT_FADE_THRESHOLD_SIZE_ARB 0x8128
#define GL_POINT_DISTANCE_ATTENUATION_ARB 0x8129
static GLfloat linearsize = 0.1;
static GLfloat psize = 7.0;
static GLfloat pmax;
static GLfloat constant[3] = {1.0, 0.0, 0.0};
static GLfloat linear[3] = {0.0, 0.12, 0.0};
static GLfloat quadratic[3] = {0.0, 0.0, 0.01};
PFNGLPOINTPARAMETERFARBPROC glPointParameterfEXT;
PFNGLPOINTPARAMETERFVARBPROC glPointParameterfvEXT;
void init(void)
{
glPointParameterfEXT = (PFNGLPOINTPARAMETERFARBPROC)wglGetProcAddress("glPointParameterfARB");
glPointParameterfvEXT = (PFNGLPOINTPARAMETERFVARBPROC)wglGetProcAddress("glPointParameterfvARB");
int i;
srand (12345);
glNewList(1, GL_COMPILE);
glBegin (GL_POINTS);
for (i = 0; i < 250; i++) {
glColor3f (1.0, ((rand()/(float) RAND_MAX) * 0.5) + 0.5, rand()/(float) RAND_MAX);
/* randomly generated vertices:
-5 < x < 5; -5 < y < 5; -5 < z < -45 */
glVertex3f ( ((rand()/(float)RAND_MAX) * 10.0) - 5.0,
((rand()/(float)RAND_MAX) * 10.0) - 5.0,
((rand()/(float)RAND_MAX) * 40.0) - 45.0);
}
glEnd();
glEndList();
glEnable(GL_DEPTH_TEST);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPointSize(psize);
glGetFloatv(GL_POINT_SIZE_MAX_EXT, &pmax);
glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, linear);
glPointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT, 2.0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCallList (1);
glutSwapBuffers ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (35.0, 1.0, 0.25, 200.0);
glMatrixMode (GL_MODELVIEW);
glTranslatef (0.0, 0.0, -10.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'b':
glMatrixMode (GL_MODELVIEW);
glTranslatef (0.0, 0.0, -0.5);
glutPostRedisplay();
break;
case 'c':
glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, constant);
glutPostRedisplay();
break;
case 'f':
glMatrixMode (GL_MODELVIEW);
glTranslatef (0.0, 0.0, 0.5);
glutPostRedisplay();
break;
case 'l':
glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, linear);
glutPostRedisplay();
break;
case 'q':
glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, quadratic);
glutPostRedisplay();
break;
case '+':
if (psize < (pmax + 1.0))
psize = psize + 1.0;
glPointSize(psize);
glutPostRedisplay();
break;
case '-':
if (psize >= 2.0)
psize = psize - 1.0;
glPointSize(psize);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
书中给出的代码运行报错,这是我查了很多资料之后自己改写的,可以运行。运行效果如下:
1. quadratic
2.constant
3.linear