OpenGL对这些标记的定义
名称 十六进制 十进制
#define GL_PASS_THROUGH_TOKEN 0x0700 (1792)
#define GL_POINT_TOKEN 0x0701 (1793)
#define GL_LINE_TOKEN 0x0702 (1794)
#define GL_POLYGON_TOKEN 0x0703 (1795)
#define GL_BITMAP_TOKEN 0x0704 (1796)
#define GL_DRAW_PIXEL_TOKEN 0x0705 (1797)
#define GL_COPY_PIXEL_TOKEN 0x0706 (1798)
#define GL_LINE_RESET_TOKEN 0x0707 (1799)
绘制模型:
一个300X300的视口中,绘制一条"┌"线,以及在中间绘制一个点
代码:
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
GLfloat feedBuffer[1024];
void init(void)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void drawGeometry (GLenum mode)
{
//绘制两条线段
glBegin (GL_LINE_STRIP);
glVertex3f (50.0, 50.0, 0.0);
glVertex3f (50.0, 250.0, 0.0);
glVertex3f (250.0, 250.0, 0.0);
glEnd ();
if (mode == GL_FEEDBACK)
{
glPassThrough (1.0);//在图元之间增加标记
}
// 绘制一个点 此点在视野之外
glPointSize(5);
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 (150.0, 150.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)
{
//获取反馈数据的第一个字节,是一个表示图元类型的代码
//可以是GL_POINT_TOKEN, GL_LINE_TOKEN等等
token = buffer[size-count];
count--;
if (token == GL_PASS_THROUGH_TOKEN)//如果图元是插入的标记
{
printf ("GL_PASS_THROUGH_TOKEN\n");
printf (" %4.2f\n", buffer[size-count]);
count--;
}
else if (token == GL_POINT_TOKEN)//如果图元是点
{
printf ("GL_POINT_TOKEN\n");
print3DcolorVertex (size, &count, buffer);
}
else if (token == GL_LINE_TOKEN)//如果图元是线
{
printf ("GL_LINE_TOKEN\n");
print3DcolorVertex (size, &count, buffer);
print3DcolorVertex (size, &count, buffer);
}
else if (token == GL_LINE_RESET_TOKEN)//如果图元是线,切对于点画模式进行了重置
{
printf ("GL_LINE_RESET_TOKEN\n");
print3DcolorVertex (size, &count, buffer);
print3DcolorVertex (size, &count, buffer);
}
}
}
void display(void)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0.0, 300.0, 0.0, 300.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawGeometry (GL_RENDER);
//指定保存反馈数据的数组,GL_3D_COLOR表示返回的顶点数据由坐标(x,y,z)和颜色(r,g,b,a)组成
//GLfloat feedBuffer[1024];
glFeedbackBuffer (1024, GL_3D_COLOR, feedBuffer);
glRenderMode (GL_FEEDBACK);//进入反馈模式
drawGeometry (GL_FEEDBACK);
GLint size = glRenderMode (GL_RENDER);//退出反馈模式,同时获得返回的图元的个数
printBuffer (size, feedBuffer);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (300, 300);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
运行结果:
反馈后,在feedBuffer的数据如下:
数据格式排列有点乱......