ZBLOG

Ubuntu OpenGL ES实现一个矩形 Shader中v_texCoord赋值的完整代码例子

在Ubuntu上使用OpenGL ES实现一个简单的矩形,并在着色器中为顶点纹理坐标(v_texCoord)赋值,通常需要进行以下步骤:创建一个OpenGL ES上下文,编写顶点和片段着色器,设置顶点数据,以及渲染矩形。以下是一个简化的完整代码示例。

1. 安装依赖

确保你的系统上已经安装了必要的开发工具和库,例如Mesa、libgl1-mesa-dev等。

sudo apt-get install mesa-utils libgl1-mesa-dev

2. 创建 OpenGL ES 示例程序

下面是一个使用OpenGL ES来绘制矩形的简单示例:

#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <iostream>
#include <cstring>

// Vertex shader source code
const char* vertexShaderSource = R"(
    attribute vec4 a_position;
    attribute vec2 a_texCoord;
    varying vec2 v_texCoord;

    void main() {
        gl_Position = a_position;
        v_texCoord = a_texCoord; // Pass the texture coordinate to the fragment shader
    }
)";

// Fragment shader source code
const char* fragmentShaderSource = R"(
    precision mediump float; // Set default precision for floats
    varying vec2 v_texCoord; // Receive texture coordinates from vertex shader

    void main() {
        gl_FragColor = vec4(v_texCoord, 0.0, 1.0); // Use texture coordinates for color output (for demonstration)
    }
)";

void checkGLError(const char* operation) {
    GLenum error;
    while ((error = glGetError()) != GL_NO_ERROR) {
        std::cerr << "Error during " << operation << ": " << error << std::endl;
    }
}

GLuint createShader(GLenum type, const char* source) {
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &source, nullptr);
    glCompileShader(shader);
    
    GLint isCompiled = 0;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
    
    if (isCompiled == GL_FALSE) {
        GLint maxLength = 0;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
        
        // The compiler failed; get the info log.
        std::vector<char> errorLog(maxLength);
        glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
        
        // Provide the infolog in whatever manner you deem best.
        std::cerr << "Shader compilation error: " << &errorLog[0] << std::endl;

        // Cleanup
        glDeleteShader(shader);
        return 0;
    }

   return shader;
}

GLuint createProgram() {
     GLuint vertexShader = createShader(GL_VERTEX_SHADER, vertexShaderSource);
     GLuint fragmentShader = createShader(GL_FRAGMENT_SHADER, fragmentShaderSource);

     GLuint program = glCreateProgram();
     glAttachShader(program, vertexShader);
     glAttachShader(program, fragmentShader);
     glLinkProgram(program);

     // Clean up shaders as they're no longer needed after linking.
     glDeleteShader(vertexShader);
     glDeleteShader(fragmentShader);

     return program;
}

int main() {
   EGLDisplay display;
   EGLSurface surface;
   EGLContext context;

   // Initialize EGL and create an OpenGL context here...
   // Setup and initialize your EGL environment (omitted for brevity)

   GLuint program = createProgram();
   
   GLfloat vertices[] = { 
       -0.5f, -0.5f,
       0.5f, -0.5f,
       -0.5f,  0.5f,
       0.5f,  0.5f 
   };
   
   GLfloat texCoords[] = { 
       0.0f, 1.0f,
       1.0f, 1.0f,
       0.0f, 0.0f,
       1.0f, 0.0f 
   };

   GLuint positionBufferObject;
   GLuint texCoordBufferObject;

   glGenBuffers(1, &positionBufferObject);
   glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
   glBufferData(GL_ARRAY_BUFFER,sizeof(vertices), vertices , GL_STATIC_DRAW);

   glGenBuffers(1,&texCoordBufferObject);  
   glBindBuffer(GL_ARRAY_BUFFER , texCoordBufferObject );
  	glBufferData( GL_ARRAY_BUFFER , sizeof(texCoords), texCoords , GL_STATIC_DRAW );

   
	// Rendering Loop (not complete)
	// clear screen
	glClearColor(1.f , .8 , .8 , .9 );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	GLuint posAttribLocation=glGetAttribLocation(program,"a_position");
	GLuint texAttribLocation=glGetAttribLocation(program,"a_texCoord");

	glEnableVertexAttribArray(posAttribLocation);     
	glBindBuffer(GL_ARRAY_BUFFER , positionBufferObject );    
	glVertexAttribPointer(posAttribLocation ,2 ,GL_FLOAT ,false,sizeof(float)*2,(void*)nullptr); 

	glEnableVertexAttribArray(texAttribLocation);  
	glBindBuffer(GL_ARRAY_BUFFER ,texCoordBufferObject );
	glVertexAttribPointer(texAttribLocation ,2 ,GL_FLOAT,false,sizeof(float)*2,(void*)nullptr);


	// Draw rectangle using two triangles
	glDrawArrays(GL_TRIANGLE_STRIP ,	/* first */	 	/* count */	 		 /* offset */ 		/* count*/			/* Primitive Type */
			  	   	    	   	    	   	    		   	   	    		               ) ;
	
	checkGLError("Drawing");

	EGLBoolean swapSuccess=eglSwapBuffers(display,surface );// Swap buffers here
   
	return exit_code;	 
}

注意事项:

  • 此代码省略了初始化EGL和其他窗口系统设置。
  • 确保在运行该代码之前,你已经设置好适当的环境(例如X11或其他窗口系统),以便能够创建OpenGL上下文。
  • 在生产代码中,你可能还要处理更多错误检查和清理资源等任务。

希望这个示例能帮到你!如果你有任何进一步的问题,请随时询问。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=6590

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?