forked from zhanxiaokai/iOS-OpenGLRenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBAFrameCopier.mm
executable file
·161 lines (138 loc) · 4.71 KB
/
RGBAFrameCopier.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// RGBACopier.m
// OpenGLRenderer
//
// Created by apple on 2017/2/9.
// Copyright © 2017年 xiaokai.zhan. All rights reserved.
//
#import "RGBAFrameCopier.h"
#define STRINGIZE(x) #x
#define STRINGIZE2(x) STRINGIZE(x)
#define SHADER_STRING(text) @ STRINGIZE2(text)
NSString *const vertexShaderString = SHADER_STRING
(
attribute vec4 position;
attribute vec2 texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = position;
v_texcoord = texcoord.xy;
}
);
NSString *const rgbFragmentShaderString = SHADER_STRING
(
varying highp vec2 v_texcoord;
uniform sampler2D inputImageTexture;
void main()
{
gl_FragColor = texture2D(inputImageTexture, v_texcoord);
}
);
@implementation RGBAFrameCopier
{
NSInteger frameWidth;
NSInteger frameHeight;
GLuint filterProgram;
GLint filterPositionAttribute;
GLint filterTextureCoordinateAttribute;
GLint filterInputTextureUniform;
GLuint _inputTexture;
}
- (BOOL) prepareRender:(NSInteger)textureWidth height:(NSInteger)textureHeight;
{
BOOL ret = NO;
frameWidth = textureWidth;
frameHeight = textureHeight;
if([self buildProgram:vertexShaderString fragmentShader:rgbFragmentShaderString]) {
glGenTextures(1, &_inputTexture);
glBindTexture(GL_TEXTURE_2D, _inputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)frameWidth, (GLsizei)frameHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
ret = YES;
}
return ret;
}
- (BOOL) buildProgram:(NSString*) vertexShader fragmentShader:(NSString*) fragmentShader;
{
BOOL result = NO;
GLuint vertShader = 0, fragShader = 0;
filterProgram = glCreateProgram();
vertShader = compileShader(GL_VERTEX_SHADER, vertexShader);
if (!vertShader)
goto exit;
fragShader = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
if (!fragShader)
goto exit;
glAttachShader(filterProgram, vertShader);
glAttachShader(filterProgram, fragShader);
glLinkProgram(filterProgram);
filterPositionAttribute = glGetAttribLocation(filterProgram, "position");
filterTextureCoordinateAttribute = glGetAttribLocation(filterProgram, "texcoord");
filterInputTextureUniform = glGetUniformLocation(filterProgram, "inputImageTexture");
GLint status;
glGetProgramiv(filterProgram, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
NSLog(@"Failed to link program %d", filterProgram);
goto exit;
}
result = validateProgram(filterProgram);
exit:
if (vertShader)
glDeleteShader(vertShader);
if (fragShader)
glDeleteShader(fragShader);
if (result) {
NSLog(@"OK setup GL programm");
} else {
glDeleteProgram(filterProgram);
filterProgram = 0;
}
return result;
}
- (void) renderFrame:(uint8_t*) rgbaFrame;
{
glUseProgram(filterProgram);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, _inputTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)frameWidth, (GLsizei)frameHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, rgbaFrame);
static const GLfloat imageVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
GLfloat noRotationTextureCoordinates[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, imageVertices);
glEnableVertexAttribArray(filterPositionAttribute);
glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates);
glEnableVertexAttribArray(filterTextureCoordinateAttribute);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _inputTexture);
glUniform1i(filterInputTextureUniform, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
- (void) releaseRender;
{
if (filterProgram) {
glDeleteProgram(filterProgram);
filterProgram = 0;
}
if(_inputTexture) {
glDeleteTextures(1, &_inputTexture);
}
}
@end