OpenGL: 反馈

    使用glRenderMode设定模式,glFeedbackBuffer进行反馈,glPassThrough使用标记。

#include <stdio.h>
#include <math.h>
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>
#include <gl/GLAux.h>
#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "GLU32.lib")
#pragma comment(lib, "Glut32.lib")

int board[3][3];

void init()
{
	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();
	glFlush();
}
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_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()
{
	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);
}
void reshape(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, 100.0, 0.0, 100.0, 0.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
	case 'c':
	case 'C':
		glutPostRedisplay();
		break;
	case 27:
		exit(0);
		break;
	default:
		break;
	}
}
int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(100, 100);
	glutCreateWindow(argv[0]);
	init();
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}
运行效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值