ii = *ptr;
else if (j == 1) jj = *ptr; ptr++; }
printf (\
board[ii][jj] = (board[ii][jj] + 1) % 2; } }
#define BUFSIZE 512
void pickSquares(int button, int state, int x, int y) {
GLuint selectBuf[BUFSIZE]; GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return;
glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(0);
glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity ();
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),5.0, 5.0, viewport); gluOrtho2D (0.0, 2.0, 0.0, 2.0); drawSquares (GL_SELECT);
glMatrixMode (GL_PROJECTION); glPopMatrix (); glFlush ();
hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); glutPostRedisplay(); }
void display(void) {
glClear(GL_COLOR_BUFFER_BIT); drawSquares (GL_RENDER); glFlush(); }
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluOrtho2D (0.0, 2.0, 0.0, 2.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (200, 200); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init ();
glutMouseFunc (pickSquares); glutReshapeFunc (reshape); glutDisplayFunc(display); glutMainLoop(); return 0; }
6.10编写程序,绘制三个基本图元,并在反馈模式下输出这些图元的三维坐标反馈信息。 #include
glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); }
void drawGeometry (GLenum mode) {
glBegin (GL_LINE_STRIP); glNormal3f (0.0, 0.0, 1.0); glVertex3f (30.0, 30.0, 0.0); glVertex3f (50.0, 60.0, 0.0); glVertex3f (70.0, 40.0, 0.0); glEnd ();
if (mode == GL_FEEDBACK) glPassThrough (1.0); glBegin (GL_POINTS);
glVertex3f (-100.0, -100.0, -100.0); /* will be clipped */ glEnd ();
if (mode == GL_FEEDBACK) glPassThrough (2.0);
glBegin (GL_POINTS);
glNormal3f (0.0, 0.0, 1.0); glVertex3f (50.0, 50.0, 0.0); glEnd (); }
void print3DcolorVertex (GLint size, GLint *count, GLfloat *buffer) {
int i;
printf (\
for (i = 0; i < 7; i++) {
printf (\*count = *count - 1; }
printf (\}
void printBuffer(GLint size, GLfloat *buffer) {
GLint count; GLfloat token; count = size; while (count) {
token = buffer[size-count]; count--; if (token == GL_PASS_THROUGH_TOKEN) { printf (\
printf (\count--; }
else if (token == GL_POINT_TOKEN) { printf (\
print3DcolorVertex (size, &count, buffer); }
else if (token == GL_LINE_TOKEN) { printf (\
print3DcolorVertex (size, &count, buffer); print3DcolorVertex (size, &count, buffer); }
else if (token == GL_LINE_RESET_TOKEN) { printf (\
print3DcolorVertex (size, &count, buffer); print3DcolorVertex (size, &count, buffer); } } }
void display(void) {
GLfloat feedBuffer[1024]; GLint size;
glMatrixMode (GL_PROJECTION); glLoadIdentity ();
glOrtho (0.0, 100.0, 0.0, 100.0, 0.0, 1.0); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); drawGeometry (GL_RENDER);
glFeedbackBuffer (1024, GL_3D_COLOR, feedBuffer); (void) glRenderMode (GL_FEEDBACK); drawGeometry (GL_FEEDBACK);
size = glRenderMode (GL_RENDER); printBuffer (size, feedBuffer); }
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (100, 100); glutInitWindowPosition (100, 100); glutCreateWindow(argv[0]); init();
glutDisplayFunc(display); glutMainLoop(); return 0; }