当前位置:网站首页>Simple implementation of gpuimage chain texture
Simple implementation of gpuimage chain texture
2022-06-12 13:40:00 【Li. CQ】
The chain shader The implementation of the
Realize chain shader The core idea of : Buffer objects with frames , The texture unit generated by the frame buffer object is the next shader Texture input ;
Source code
The core of frame buffer
/** Set the frame buffer object */
-(void)setuptextureSize:(CGSize)size{
glGenFramebuffers(1, &_frameBufferID);
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &_textureID);
glBindTexture(GL_TEXTURE_2D, _textureID);
// - there size It is filled with rendering view Of size, Not the picture size
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
// - Framebuffer object
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, _textureID, 0);
/* Untie the texture , Unbind to rebind a new texture unit ; If there is no unbinding operation , Then the output texture unit of the frame buffer is GL_TEXTURE1, But if it is the output of one frame buffer and the input of another frame buffer , It will appear that the input and output are the same texture unit ;( As below shaderCompiler2, frameBuffer1 The output texture unit of is used as shaderCompiler2 The input unit of is GL_TEXTURE1, the shaderCompiler2 After frameBuffer2 The output texture unit of is also GL_TEXTURE1, This is not possible ) glActiveTexture() : Set the output texture unit of the frame buffer ; glUniform1i() : Set up shader The input of texture ; Input and output cannot be the same texture unit ; */
glBindTexture(GL_TEXTURE_2D, 0);
}
- (GLuint)textureID{
return _textureID;
}
- (GLuint)framebufferID{
return _frameBufferID;
}
/** Activate frame buffer */
-(void)activityFrameBuffer{
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
}
Implementation of chain texture
// - Input texture
-(void)setupInputTexture{
GLuint texture;
UIImage *image = [UIImage imageNamed:@"gyy.jpg"];
size_t width = CGImageGetWidth(image.CGImage);
size_t height = CGImageGetHeight(image.CGImage);
_bufferSize = CGSizeMake(width, height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context = CGBitmapContextCreate(imageData,
width,
height,
8,
4 * width,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM (context, 1.0,-1.0);
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );
CGContextRelease(context);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
(GLint)width,
(GLint)height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
imageData);
free(imageData);
}
// - Three shader programs ( Original texture ->shaderCompiler1->frameBuffe1, frameBuffe1->shaderCompiler2->frameBuffe2, frameBuffe2->shaderCompiler3-> Render on screen )
-(void)setupShader{
self.shaderCompiler1 = [[QGShaderCompiler alloc]initWithvshaderFileName:@"VertextShader2" fshaderFileName:@"FragmentShader2_06"];
_position1 = [self.shaderCompiler1 addAttribute:@"position"];
_texture1 = [self.shaderCompiler1 addAttribute:@"textCoordinate"];
_uni1 = [self.shaderCompiler1 addUniform:@"colorMap"];
self.shaderCompiler2 = [[QGShaderCompiler alloc]initWithvshaderFileName:@"VertextShader2" fshaderFileName:@"FragmentShader2_04"];
_position2 = [self.shaderCompiler2 addAttribute:@"position"];
_texture2 = [self.shaderCompiler2 addAttribute:@"textCoordinate"];
_uni2 = [self.shaderCompiler2 addUniform:@"colorMap"];
self.shaderCompiler3 = [[QGShaderCompiler alloc]initWithvshaderFileName:@"VertextShader2" fshaderFileName:@"FragmentShader2_14"];
_position3 = [self.shaderCompiler3 addAttribute:@"position"];
_texture3 = [self.shaderCompiler3 addAttribute:@"textCoordinate"];
_uni3 = [self.shaderCompiler3 addUniform:@"colorMap"];
}
// - Two frame buffer objects that provide output
-(void)setupFrameBufferObj{
self.frameBuffer1 = [[QGFrameBuffer alloc]initWithSize:self.frame.size];
self.frameBuffer2 = [[QGFrameBuffer alloc]initWithSize:self.frame.size];
}
// - Original texture ->shaderCompiler1->frameBuffe1
-(void)render1{
[self.shaderCompiler1 glUseProgram];
[self.frameBuffer1 activityFrameBuffer];
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
/* The output texture unit of the known source texture is GL_TEXTURE0; The lower one glUniform1i(_uni1, 0) Yes, it will GL_TEXTURE0 As self.shaderCompiler1 Input texture of , QGFrameBuffer Medium glActiveTexture(GL_TEXTURE1); Yes, it will GL_TEXTURE1, As a frame buffer texture output unit ; */
glUniform1i(_uni1, 0);
GLfloat vertices[] = {
1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, -1.0, 0.0,
-1.0, 1.0, 0.0};
GLfloat texturecoords[] ={
1.0, 1.0,
1.0, 0.0,
0.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0};
glVertexAttribPointer(_position1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, vertices);
glVertexAttribPointer(_texture1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, texturecoords);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
// - frameBuffe1->shaderCompiler2->frameBuffe2
-(void)render2{
[self.shaderCompiler2 glUseProgram];
[self.frameBuffer2 activityFrameBuffer];
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
/* The front is known self.frameBuffer1 The output texture unit of is GL_TEXTURE1; glActiveTexture(GL_TEXTURE2); and glBindTexture(GL_TEXTURE_2D, [self.frameBuffer1 textureID]); Yes, it will self.frameBuffer1 The output texture unit of is from GL_TEXTURE1 Change it to GL_TEXTURE2; glUniform1i(_uni2, 2) Yes, it will GL_TEXTURE2 As self.shaderCompiler2 Input texture of ; QGFrameBuffer Medium glActiveTexture(GL_TEXTURE1); Yes, it will GL_TEXTURE1, As a frame buffer texture output unit ; */
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, [self.frameBuffer1 textureID]);
glUniform1i(_uni2, 2);
GLfloat vertices[] = {
1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, -1.0, 0.0,
-1.0, 1.0, 0.0};
GLfloat texturecoords[] ={
1.0, 1.0,
1.0, 0.0,
0.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0};
glVertexAttribPointer(_position2, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, vertices);
glVertexAttribPointer(_texture2, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, texturecoords);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
// - frameBuffe2->shaderCompiler3-> Render on screen
-(void)render3{
[self.shaderCompiler3 glUseProgram];
[self activityFrameBuffer];
glClearColor(0, 0, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
/* The front is known self.frameBuffer2 The output texture unit of is GL_TEXTURE1; glActiveTexture(GL_TEXTURE2); and glBindTexture(GL_TEXTURE_2D, [self.frameBuffer1 textureID]); Yes, it will self.frameBuffer2 The output texture unit of is from GL_TEXTURE1 Change it to GL_TEXTURE2; glUniform1i(_uni2, 2) Yes, it will GL_TEXTURE2 As self.shaderCompiler3 Input texture of ; The frame buffer texture output unit is rendered to the screen ; */
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, [self.frameBuffer2 textureID]);
glUniform1i(_uni3, 2);
GLfloat vertices[] = {
1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, -1.0, 0.0,
-1.0, 1.0, 0.0};
GLfloat texturecoords[] ={
1.0, 1.0,
1.0, 0.0,
0.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0};
glVertexAttribPointer(_position3, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, vertices);
glVertexAttribPointer(_texture3, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, texturecoords);
glDrawArrays(GL_TRIANGLES, 0, 6);
[[QGEAGLContext sharedInstance] presentRenderbuffer];
}
边栏推荐
- import torch_ Geometric loads some common datasets
- 【刷题篇】抽牌获胜的概率
- 2067: [example 2.5] circle
- Application of binary search -- finding the square root sqrt of a number
- Codeforces 1629 C. Mexico array - simple greed
- Successfully rated Tencent t3-2, 10000 word parsing
- AWLive 结构体的使用
- 将字符串转为16进制字符串并显示出来
- Informatics Olympiad all in one 2059: [example 3.11] buy a pen
- Codeforces 1629 E. grid XOR - simple thinking
猜你喜欢
![[brush title] probability of winning a draw](/img/f5/7e8dac944876f920db10508ec38e92.png)
[brush title] probability of winning a draw

Cocoapods的相关知识点

Encryptor and client authenticate with each other

Successful job hopping Ali, advanced learning

【刷题篇】抽牌获胜的概率

Codeforces 1637 D. yet another minimization problem - Mathematics, DP

Automatic Generation of Visual-Textual Presentation Layout

Pytorch to onnx, onnxruntime reasoning in mmclas

leetcode 47. Permutations II full permutations II (medium)

C#DBHelper_ FactoryDB_ GetConn
随机推荐
【刷题篇】超级洗衣机
Application of binary search -- finding the square root sqrt of a number
go-zero 微服务实战系列(二、服务拆分)
[wechat applet development] Part 1: development tool installation and program configuration
事件的传递和响应以及使用实例
Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
Install RPM package offline using yum
苹果电脑上MySQL安装完成找不到怎么办
1001:Hello,World
创新实训(十一)开发过程中的一些bug汇总
上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
C语言【23道】经典面试题【下】
开发中使用的语言技巧
Codeforces 1637 B. mex and array - reading, violence
Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
Dameng database DM8 Windows environment installation
章鱼网络进展月报 | 2022.5.1-5.31
torch_geometric mini batch 的那些事
Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s
Symbolic constant, const qualifier