OpenGL 学习笔记3_5(缓冲区相关) 模板缓冲区待详细分析

本文介绍了OpenGL中的缓冲区机制,包括单缓冲、双缓冲的使用,以及颜色缓冲区和深度缓冲区的概念。深度缓冲区用于处理图形的层次关系,而模板缓冲区则提供了类似镂空效果的功能。通过启用和配置相应的缓冲区,可以实现更复杂的图形渲染效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蓝宝书 第三章

单缓冲与双缓冲

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);单缓冲 相关代码见例3.12

直接将图像改变显示在画布上,使用glFlush()提交缓冲

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);双缓冲

一般图像改变在后台缓冲区,使用glutSwapBuffers()将后台缓冲提交至前台缓冲区

void glDrawBuffer(Glenum mode) 该函数可设置当前改变前台缓冲区还是后台缓冲区

glDrawBuffer(GL_FRONT)  前台

glDrawBuffer(GL_BACK)  后台

颜色缓冲区与深度缓冲区

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)

上述语句初始化了颜色缓冲区、双缓冲区以及深度缓冲区

若未启用深度缓冲区(glDisable(GL_DEPTH_TEST)),则深度数据也会写入颜色缓冲区,使用glDepthMask(GL_FALSE)禁止写入,第六章详细讲解

剪刀盒(类似于opencv的感兴趣区域)

只对剪刀盒范围内进行处理

glEnable(GL_SCISSOR_TEST);启用剪刀盒

void glScissor(GLint x, GLint y, GLsizei width, GLsizei height); 设置剪刀盒范围

相关代码见例3.13

模板缓冲区

类似于喷油漆,在纸上镂空一个模板,再喷上油漆,下面的纸上只有镂空部分才有颜色。

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL);初始化模板缓冲区
glEnable(GL_STENCIL_TEST);启用模板缓冲区

void glStencilFunc(GLenum func, GLint ref, GLuint mask);设置模板

参考:

点击打开链接

点击打开链接

相关代码见例3.14


例3.12 单缓冲区示例(单击窗口进行新的缓冲,增大了图形半径)

#include <windows.h>  
#include <math.h>  
#include <GL\GL.h>  
#include <GL\GLU.h>  
#include <GL\glut.h>  

#define GL_PI 3.1415f  
// This function does any needed initialization on the rendering  

void RenderScene(void)
{
	static GLdouble dRadius = 15;
	static GLdouble dAngle = 0.0;
	// Clear blue window
	glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
	if (dAngle == 0.0)
		glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POINTS);
	glVertex2d(dRadius * cos(dAngle), dRadius * sin(dAngle));
	glEnd();
	dRadius *= 1.01;
	dAngle += 0.2;
	if (dAngle > 30.0)
	{
		dRadius = 15;
		dAngle = 0.0;
	}
	glFlush();
}
void SetupRC()
{
	// Black background
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	// Set drawing color to red
	glColor3f(1.0f, 0.0f, 0.0f);
	//glPolygonMode(GL_BACK, GL_LINE);
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
	// Enable polygon stippling
	//glEnable(GL_POLYGON_STIPPLE);
	// Specify a specific stipple pattern
	//glPolygonStipple(fire);
}
void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat nRange = 100.0f;
	// Prevent a divide by zero  
	if (h == 0)
		h = 1;
	// Set Viewport to window dimensions  
	glViewport(0, 0, w, h);
	// Reset projection matrix stack  
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// Establish clipping volume (left, right, bottom, top, near, far)  

	if (w <= h)
		glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
	else
		glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
	// Reset Model view matrix stack  
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Bounce");
	glutDisplayFunc(RenderScene);//显示回调函数  
	glutReshapeFunc(ChangeSize);//窗口大小变形回调函数  

	SetupRC();
	glutMainLoop();

	return 0;
}
例3.13 剪刀盒示例

#include <windows.h>  
#include <math.h>  
#include <GL\GL.h>  
#include <GL\GLU.h>  
#include <GL\glut.h>  

#define GL_PI 3.1415f  
// This function does any needed initialization on the rendering  

void RenderScene(void)
{
	glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	// Now set scissor to smaller red sub region
	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
	glScissor(100, 100, 600, 400);
	glEnable(GL_SCISSOR_TEST);
	glClear(GL_COLOR_BUFFER_BIT);
	// Finally, an even smaller green rectangle
	glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
	glScissor(200, 200, 400, 200);
	glClear(GL_COLOR_BUFFER_BIT);
	// Turn scissor back off for next render
	glDisable(GL_SCISSOR_TEST);
	glutSwapBuffers();
}
void SetupRC()
{
	// Black background
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	// Set drawing color to red
	glColor3f(1.0f, 0.0f, 0.0f);
	//glPolygonMode(GL_BACK, GL_LINE);
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
	// Enable polygon stippling
	//glEnable(GL_POLYGON_STIPPLE);
	// Specify a specific stipple pattern
	//glPolygonStipple(fire);
}
void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat nRange = 100.0f;
	// Prevent a divide by zero  
	if (h == 0)
		h = 1;
	// Set Viewport to window dimensions  
	glViewport(0, 0, w, h);
	// Reset projection matrix stack  
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// Establish clipping volume (left, right, bottom, top, near, far)  

	if (w <= h)
		glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
	else
		glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
	// Reset Model view matrix stack  
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Bounce");
	glutDisplayFunc(RenderScene);//显示回调函数  
	glutReshapeFunc(ChangeSize);//窗口大小变形回调函数  

	SetupRC();
	glutMainLoop();

	return 0;
}
例3.14 模板示例
#include <windows.h>  
#include <math.h>  
#include <GL\GL.h>  
#include <GL\GLU.h>  
#include <GL\glut.h>  

#define GL_PI 3.1415f  
// This function does any needed initialization on the rendering  

void RenderScene(void)
{
	GLdouble dRadius = 0.1; // Initial radius of spiral
	GLdouble dAngle; // Looping variable
	// Clear blue window
	glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
	// Use 0 for clear stencil, enable stencil test
	glClearStencil(0.0f);
	glEnable(GL_STENCIL_TEST);
	// Clear color and stencil buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	// All drawing commands fail the stencil test, and are not
	// drawn, but increment the value in the stencil buffer.
	glStencilFunc(GL_NEVER, 0x0, 0x0);
	glStencilOp(GL_INCR, GL_INCR, GL_INCR);
	// Spiral pattern will create stencil pattern
	// Draw the spiral pattern with white lines. We
	// make the lines white to demonstrate that the
	// stencil function prevents them from being drawn
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (dAngle = 0; dAngle < 400.0; dAngle += 0.1)
	{
		glVertex2d(dRadius * cos(dAngle), dRadius * sin(dAngle));
		dRadius *= 1.002;
	}
	glEnd();
	// Now, allow drawing, except where the stencil pattern is 0x1
	// and do not make any further changes to the stencil buffer
	glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	// Now draw red bouncing square
	// (x and y) are modified by a timer function
	glColor3f(1.0f, 0.0f, 0.0f);
	glRectf(0, 0, 100, 100);
	// All done, do the buffer swap
	glutSwapBuffers();
}
void SetupRC()
{
	// Black background
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	// Set drawing color to red
	glColor3f(1.0f, 0.0f, 0.0f);
	//glPolygonMode(GL_BACK, GL_LINE);
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
	// Enable polygon stippling
	//glEnable(GL_POLYGON_STIPPLE);
	// Specify a specific stipple pattern
	//glPolygonStipple(fire);
}
void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat nRange = 100.0f;
	// Prevent a divide by zero  
	if (h == 0)
		h = 1;
	// Set Viewport to window dimensions  
	glViewport(0, 0, w, h);
	// Reset projection matrix stack  
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// Establish clipping volume (left, right, bottom, top, near, far)  

	if (w <= h)
		glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
	else
		glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
	// Reset Model view matrix stack  
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Bounce");
	glutDisplayFunc(RenderScene);//显示回调函数  
	glutReshapeFunc(ChangeSize);//窗口大小变形回调函数  

	SetupRC();
	glutMainLoop();

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值