FEEDBACK.C
上传用户:tengyuc
上传日期:2007-08-14
资源大小:722k
文件大小:3k
-
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
// 初始化光照
void init(void)
{
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);
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 ("%4.2f ", buffer[size-(*count)]);
*count = *count - 1;
}
printf ("n");
}
// 输出整个缓冲区
- 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 ("GL_PASS_THROUGH_TOKENn");
- printf (" %4.2fn", buffer[size-count]);
- count--;
- }
- else if (token == GL_POINT_TOKEN) {
- printf ("GL_POINT_TOKENn");
- print3DcolorVertex (size, &count, buffer);
- }
- else if (token == GL_LINE_TOKEN) {
- printf ("GL_LINE_TOKENn");
- print3DcolorVertex (size, &count, buffer);
- print3DcolorVertex (size, &count, buffer);
- }
- else if (token == GL_LINE_RESET_TOKEN) {
- printf ("GL_LINE_RESET_TOKENn");
- 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);
glRenderMode (GL_FEEDBACK);
drawGeometry (GL_FEEDBACK);
size = glRenderMode (GL_RENDER);
printBuffer (size, feedBuffer);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (200, 200);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}