《OpenGL ES 2.0 Programming Guide》第12章“最简单的FBO Depth Texture”示例代码【C语言版】

由于《OpenGL ES 2.0 Programming Guide》原书并没有提供第12章的示例代码,书上的代码也只提到关键的步骤,而网上大多是Android/iOS版本的示例,C/C++的大都基于OpenGL或OpenGL ES 3.0,为了加深理解,遂自己实现了一份C语言版本的,希望能够帮助到同样喜欢OpenGL ES 2.0的同学。

废话不多说,直接上代码:


#include <stdlib.h>
#include <stdio.h>
#include "esUtil.h"

#define SIZE 512

typedef struct 
{
	GLuint programFBOObject;
	GLuint programObject;
	GLuint frameBuffer;
	GLuint texture;
	GLuint depthTexture;

	GLint positionFBOLoc;
	GLint mvpFBOLoc;

	GLint positionLoc;
	GLint mvpLoc;
	GLint texcoordLoc;
	GLint samplerLoc;

	GLfloat *vertices;
	GLfloat *texcoords;
	GLuint *indices;

	int numIndices;

	GLfloat angle;

	ESMatrix mvpMatrix;
} UserData;

int initFBO(ESContext *esContext, GLint width, GLint height)
{
	GLenum status;
	//GLint maxRenderbufferSize;

	UserData *userData = (UserData *)esContext->userData;

	//glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxRenderbufferSize);
	 check if GL_MAX_RENDERBUFFER_SIZE is >= texWidth and texHeight
	//if ((maxRenderbufferSize <= width) || (maxRenderbufferSize <= height))
	//{
	//	// cannot use framebuffer objects as we need to create
	//	// a depth buffer as a renderbuffer object
	//	printf("Cannot use framebuffer objects!\n");
	//	exit(EXIT_FAILURE);
	//	return FALSE;
	//}

	// generate the framebuffer
	glGenFramebuffers(1, &userData->frameBuffer);

	// Depth Texture
	glGenTextures(1, &userData->depthTexture);
	glBindTexture(GL_TEXTURE_2D, userData->depthTexture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 
		0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

	// Color Texture
	glGenTextures(1, &userData->texture);
	glBindTexture(GL_TEXTURE_2D, userData->texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, 
		GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

	// bind the framebuffer
	glBindFramebuffer(GL_FRAMEBUFFER, userData->frameBuffer);
	// specify texture as color attachment 
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, userData->texture, 0);

	// ☆specify depth_texture as depth attachment
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, userData->depthTexture, 0);

	// check for framebuffer complete
	status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (status != GL_FRAMEBUFFER_COMPLETE)
	{
		printf("Framebuffer object is not complete!\n");
		exit(EXIT_FAILURE);
		return FALSE;
	}

	//glBindFramebuffer(GL_FRAMEBUFFER, 0);
	return TRUE;
}

int InitFBOShader(ESContext *esContext)
{
	UserData *userData = (UserData *)esContext->userData;

	const char vShaderStr[] =
		"uniform mat4 u_mvpMatrix;                   \n"
		"attribute vec4 a_position;                  \n"
		"void main()                                 \n"
		"{                                           \n"
		"   gl_Position = u_mvpMatrix * a_position;  \n"
		"}                                           \n";

	const char fShaderStr[] =
		"precision mediump float;                            \n"
		"void main()                                         \n"
		"{                                                   \n"
		"  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);          \n"
		"}                                                   \n";

	userData->programFBOObject = esLoadProgram(vShaderStr, fShaderStr);

	if (!userData->programFBOObject) 
		return FALSE;

	// Bind vPosition to attribute 0   
	//glBindAttribLocation(userData->programFBOObject, 0, "a_position");
	userData->positionFBOLoc = glGetAttribLocation ( userData->programFBOObject, "a_position" );
	userData->mvpFBOLoc = glGetUniformLocation( userData->programFBOObject, "u_mvpMatrix" );

	return TRUE;
}

int initShader(ESContext *esContext)
{
	UserData *userData = (UserData *)esContext->userData;

	const char vShaderStr[] =  
		"uniform mat4 u_mvpMatrix;                   \n"
		"attribute vec4 a_position;                  \n"
		"attribute vec2 a_texCoord;                  \n"
		"varying vec2 v_texCoord;                    \n"
		"void main()                                 \n"
		"{                                           \n"
		"   gl_Position = u_mvpMatrix * a_position;  \n"
		"   v_texCoord = a_texCoord;                 \n"
		"}                                           \n";

	const char fShaderStr[] =  
		"precision mediump float;                            \n"
		"varying vec2 v_texCoord;                            \n"
		"uniform sampler2D s_texture;                        \n"
		"void main()                                         \n"
		"{                                                   \n"
		"  vec4 color = texture2D(s_texture, v_texCoord);    \n"
		"  gl_FragColor = color;                             \n"
		"}                                                   \n";

	// Load the shaders and get a linked program object
	userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );

	// Get the attribute locations
	userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );

	// Get the uniform locations
	userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" );

	// Get the texture  attribute locations
	userData->texcoordLoc = glGetAttribLocation(userData->programObject, "a_texCoord");
	userData->samplerLoc = glGetUniformLocation(userData->programObject, "s_texture");

	return TRUE;
}

int Init(ESContext *esContext)
{
	UserData *userData = (UserData *)esContext->userData;

	if (!InitFBOShader(esContext))
	{
		printf("initFBOShader exception ! \n");
		return FALSE;
	}

	if (!initShader(esContext))
	{
		printf("initShader exception ! \n");
		return FALSE;
	}

	initFBO(esContext, SIZE, SIZE);

	// Generate the vertex data
	userData->numIndices = esGenCube( 1.0, &userData->vertices,
		NULL, &userData->texcoords, &userData->indices );

	// Starting rotation angle for the cube
	userData->angle = 45.0f;

	glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
	glClearDepthf( 1.0f );

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	return TRUE;
}

// Update the mvp matrix
void Update(ESContext *esContext, float deltaTime)
{
	UserData *userData = (UserData*) esContext->userData;
	ESMatrix perspective;
	ESMatrix modelview;
	float    aspect;

	// Compute a rotation angle based on time to rotate the cube
	userData->angle += ( deltaTime * 40.0f );
	if( userData->angle >= 360.0f )
		userData->angle -= 360.0f;

	// Compute the window aspect ratio
	aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;

	// Generate a perspective matrix with a 60 degree FOV
	esMatrixLoadIdentity( &perspective );
	esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );

	// Generate a model view matrix to rotate/translate the cube
	esMatrixLoadIdentity( &modelview );

	// Translate away from the viewer
	esTranslate( &modelview, 0.0, 0.0, -2.0 );

	// Rotate the cube
	esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );

	// Compute the final MVP by multiplying the 
	// modevleiw and perspective matrices together
	esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );

}

void DrawToFBO(ESContext *esContext)
{
	UserData *userData = (UserData *)esContext->userData;

	glBindFramebuffer(GL_FRAMEBUFFER, userData->frameBuffer);

	// Set the viewport
	glViewport ( 0, 0, esContext->width, esContext->height );

	// Clear the color buffer
	glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Use the program object
	glUseProgram ( userData->programFBOObject );

	// -----------------------------------------------------------
	// Load the vertex position
	glVertexAttribPointer ( userData->positionFBOLoc, 3, GL_FLOAT, 
		GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );
	glEnableVertexAttribArray ( userData->positionFBOLoc );
	// -----------------------------------------------------------
	// Load the MVP matrix
	glUniformMatrix4fv( userData->mvpFBOLoc, 1, GL_FALSE, 
		(GLfloat*) &userData->mvpMatrix.m[0][0] );
	// -----------------------------------------------------------

	// Draw the cube
	glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userData->indices );
}

void Draw(ESContext *esContext)
{
	UserData *userData = (UserData *)esContext->userData;

	DrawToFBO(esContext);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	// Set the viewport
	glViewport ( 0, 0, esContext->width, esContext->height );

	// Clear the color buffer
	glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Use the program object
	glUseProgram ( userData->programObject );

	// -----------------------------------------------------------
	// Load the vertex position
	glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 
		GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );
	glEnableVertexAttribArray ( userData->positionLoc );
	// -----------------------------------------------------------
	// Load the texture coordinate
	glVertexAttribPointer(userData->texcoordLoc, 2, GL_FLOAT,
		GL_FALSE, 2 * sizeof(GLfloat), userData->texcoords);
	glEnableVertexAttribArray(userData->texcoordLoc);
	// -----------------------------------------------------------
	// Load the MVP matrix
	glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, 
		(GLfloat*) &userData->mvpMatrix.m[0][0] );
	// -----------------------------------------------------------
	// Bind the texture unit 0
	// Set the sampler texture unit to 0
	glActiveTexture(GL_TEXTURE0);
	// Bind a texturing target.While a texture is bound, GL operations on th    
	// -e target to which it is bound affect the bound texture, and queries                                                 
	// of the target to which it is bound return state from the bound texture
	//glBindTexture(GL_TEXTURE_2D, userData->texture);
	glBindTexture(GL_TEXTURE_2D, userData->depthTexture); // ☆
	
	glUniform1i(userData->samplerLoc, 0);
	// -----------------------------------------------------------
	// Draw the cube
	glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userData->indices );
	eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
	glBindTexture(GL_TEXTURE_2D, 0);
}

void ShutDown(ESContext *esContext)
{
	UserData *userData = (UserData *)esContext->userData;

	if ( userData->vertices != NULL )
	{
		free ( userData->vertices );
	}

	if ( userData->texcoords != NULL )
	{
		free ( userData->texcoords );
	}

	if ( userData->indices != NULL )
	{
		free ( userData->indices );
	}

	// Delete program object
	glDeleteProgram (userData->programObject);
	glDeleteProgram(userData->programFBOObject);
	// Delete texture
	glDeleteTextures(1, &userData->texture);
	glDeleteTextures(1, &userData->depthTexture);
	glDeleteFramebuffers(1, &userData->frameBuffer);
}

int main ( int argc, char *argv[] )
{
	ESContext esContext;
	UserData  userData;

	esInitContext ( &esContext );
	esContext.userData = &userData;

	esCreateWindow ( &esContext, "Simple FBO", SIZE, SIZE, ES_WINDOW_RGB );

	if ( !Init ( &esContext ) )
		return 0;

	esRegisterDrawFunc ( &esContext, Draw );
	esRegisterUpdateFunc ( &esContext, Update );

	esMainLoop ( &esContext );

	ShutDown ( &esContext );
}


效果图:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 很抱歉,由于我的输入和输出都是基于文本的,所以我无法直接呈现完整的OpenGL ES离屏渲染代码。不过,我可以向您提供一些有用的资源和指导,帮助您了解OpenGL ES离屏渲染,并且编写出自己的完整代码OpenGL ES是一种开放标准的图形API,可以用于在移动设备和嵌入式设备上进行图形渲染。离屏渲染是一种技术,可以在不将渲染结果显示在屏幕上的情况下进行渲染操作。这种技术通常用于图像处理、后期处理和离线渲染等方面。 要使用OpenGL ES进行离屏渲染,您需要创建一个离屏渲染目标,并将渲染操作发送到该目标。离屏渲染目标通常是一个帧缓冲对象(Framebuffer Object,FBO),可以将渲染操作发送到其中,然后将渲染结果保存到纹理或渲染缓冲区中。 以下是一些有用的资源,可以帮助您进一步了解OpenGL ES离屏渲染: 1. OpenGL ES官方文档:https://www.khronos.org/registry/OpenGL-Refpages/es3.0/ 2. OpenGL ES教程:http://www.learnopengles.com/ 3. OpenGL ES代码样例:https://github.com/learnopengles 4. Android OpenGLES官方文档:https://developer.android.com/guide/topics/graphics/opengl 5. iOS OpenGLES官方文档:https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/Introduction/Introduction.html 希望这些资源能够帮助您了解OpenGL ES离屏渲染,并编写出自己的完整代码。如果您有任何其他问题或疑问,请随时向我提问。 ### 回答2: opengles离屏渲染是指将渲染结果绘制到一个离屏的缓冲区中,而不是直接绘制到屏幕上。这个技术在一些特定的应用场景中很有用,比如生成纹理、实现后期处理效果等。 下面是一个使用opengles进行离屏渲染的完整代码示例: 1. 首先,我们需要创建一个新的Framebuffer对象和Renderbuffer对象。 GLuint framebuffer; glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); GLuint renderbuffer; glGenRenderbuffers(1, &renderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer); 2. 将Renderbuffer对象附加到Framebuffer对象的颜色附件上。 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer); 3. 调用glRenderbufferStorage函数为Renderbuffer对象分配内存。 glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); 4.创建和编译着色器程序。 // 创建顶点着色器 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); // 创建片段着色器 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); // 创建着色器程序 GLuint program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); glLinkProgram(program); 5. 将Fragment Shader指定为离屏渲染的目标。 // 绑定Framebuffer对象 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); // 使用离屏渲染的Framebuffer进行渲染 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer); // 指定渲染目标为离屏渲染 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); // 使用离屏渲染的Framebuffer进行渲染 glViewport(0, 0, width, height); glUseProgram(program); // 其他渲染操作 6. 清除OpenGL ES环境。 glDeleteShader(vertexShader); glDeleteShader(fragmentShader); glDeleteProgram(program); glDeleteRenderbuffers(1, &renderbuffer); glDeleteFramebuffers(1, &framebuffer); 以上代码展示了一个简单的使用OpenGL ES进行离屏渲染的过程。在实际应用中,可能需要进一步配置和设置OpenGL ES环境,并根据具体需求编写对应的顶点和片段着色器代码。 ### 回答3: OpenGLES离屏渲染的完整代码如下: ```java // 导入必要的库 import android.opengl.GLES20; import android.opengl.GLUtils; // 创建离屏渲染的FrameBuffer int[] frameBuffer = new int[1]; GLES20.glGenFramebuffers(1, frameBuffer, 0); GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffer[0]); // 创建离屏渲染的纹理 int[] texture = new int[1]; GLES20.glGenTextures(1, texture, 0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]); GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texture[0], 0); // 检查FrameBuffer状态 if (GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER) != GLES20.GL_FRAMEBUFFER_COMPLETE) { // 离屏渲染失败 return; } // 开始离屏渲染 GLES20.glViewport(0, 0, width, height); GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // 绘制离屏渲染的内容 // ... // 读取离屏渲染结果 ByteBuffer buffer = ByteBuffer.allocate(width * height * 4); GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer); // 恢复默认的FrameBuffer GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); // 清除离屏渲染的FrameBuffer和纹理 GLES20.glDeleteTextures(1, texture, 0); GLES20.glDeleteFramebuffers(1, frameBuffer, 0); ``` 上述代码首先创建了一个离屏渲染的FrameBuffer,然后创建了一个纹理用于存储渲染结果。接下来,通过绘制的相关操作,将渲染结果绘制到离屏渲染的FrameBuffer中。最后,通过`glReadPixels`函数将离屏渲染的结果读取到一个ByteBuffer中。然后,代码恢复默认的FrameBuffer,并清除离屏渲染的FrameBuffer和纹理。 需要注意的是,离屏渲染的尺寸由`width`和`height`确定,绘制的内容需要根据具体需求进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShaderJoy

您的打赏是我继续写博客的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值