gpt4 book ai didi

ios - OpenGL ES glFragColor 取决于 iOS 中片段着色器的条件

转载 作者:行者123 更新时间:2023-11-28 18:59:34 25 4
gpt4 key购买 nike

我正在 iOS 上编写一个应用程序,允许自由绘制样式(使用手指)并在屏幕上绘制图像。我使用OpenGL ES来实现。我有2个功能,一个是画free style,一个是画贴图

---代码绘制自由风格

- (void)drawFreeStyle:(NSMutableArray *)pointArray {

//Prepare vertex data
.....

// Load data to the Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);

**GLuint a_ver_flag_drawing_type = glGetAttribLocation(program[PROGRAM_POINT].id, "a_drawingType");
glVertexAttrib1f(a_ver_flag_drawing_type, 0.0f);

GLuint u_fra_flag_drawing_type = glGetUniformLocation(program[PROGRAM_POINT].id, "v_drawing_type");
glUniform1f(u_fra_flag_drawing_type, 0.0);**

glUseProgram(program[PROGRAM_POINT].id);
glDrawArrays(GL_POINTS, 0, (int)vertexCount);

// Display the buffer
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}

---代码绘制纹理

- (void)drawTexture:(UIImage *)image atRect:(CGRect)rect {

GLuint a_ver_flag_drawing_type = glGetAttribLocation(program[PROGRAM_POINT].id, "a_drawingType");

GLuint u_fra_flag_drawing_type = glGetUniformLocation(program[PROGRAM_POINT].id, "v_drawing_type");

GLuint a_position_location = glGetAttribLocation(program[PROGRAM_POINT].id, "a_Position");
GLuint a_texture_coordinates_location = glGetAttribLocation(program[PROGRAM_POINT].id, "a_TextureCoordinates");
GLuint u_texture_unit_location = glGetUniformLocation(program[PROGRAM_POINT].id, "u_TextureUnit");

glUseProgram(PROGRAM_POINT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texName);
glUniform1i(u_texture_unit_location, 0);
glUniform1f(u_fra_flag_drawing_type, 1.0);

const float textrect[] = {-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f};

glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(textrect), textrect, GL_STATIC_DRAW);

glVertexAttrib1f(a_ver_flag_drawing_type, 1.0f);

glVertexAttribPointer(a_position_location, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(0));
glVertexAttribPointer(a_texture_coordinates_location, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));

glEnableVertexAttribArray(a_ver_flag_drawing_type);
glEnableVertexAttribArray(a_position_location);
glEnableVertexAttribArray(a_texture_coordinates_location);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}

注意 2 个变量 a_ver_flag_drawing_type(属性)和 u_fra_flag_drawing_type(统一)。它们用于在顶点着色器和片段着色器上设置标志以确定两个文件上的绘制自由样式或纹理

--- 顶点着色器

//Flag
attribute lowp float a_drawingType;

//For drawing
attribute vec4 inVertex;

uniform mat4 MVP;
uniform float pointSize;
uniform lowp vec4 vertexColor;

varying lowp vec4 color;

//For texture
attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;

varying vec2 v_TextureCoordinates;

void main()
{
if (abs(a_drawingType - 1.0) < 0.0001) {
//Draw texture
v_TextureCoordinates = a_TextureCoordinates;
gl_Position = a_Position;
} else {
//Draw free style
gl_Position = MVP * inVertex;
gl_PointSize = pointSize;
color = vertexColor;
}

}

--- 片段着色器

precision mediump float;

uniform sampler2D texture;
varying lowp vec4 color;

uniform sampler2D u_TextureUnit;
varying vec2 v_TextureCoordinates;

uniform lowp float v_drawing_type;

void main()
{
if (abs(v_drawing_type - 1.0) < 0.0001) {
//Draw texture
gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);
} else {
//Drawing free style
gl_FragColor = color * texture2D(texture, gl_PointCoord);
}

}

我的想法是在绘图时通过绘图代码设置这些标志。属性 a_drawingType 用于顶点着色器,而 Uniform v_drawing_type 用于片段着色器。根据这些标志了解绘制自由样式或纹理。

但如果我独立运行,每次只需要一种类型(如果运行绘图自由样式,在顶点着色器和片段着色器文件上注释代码配置绘图纹理,反之亦然)它可以按照我的意愿绘制。如果我将它们组合起来,它不仅无法绘制,还会使应用程序崩溃,因为

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

我是 OpenGL ES 和 GLSL 语言的新手,所以我不确定我这样设置标志的想法是对还是错。谁能帮帮我

最佳答案

那么为什么不构建 2 个单独的着色器程序并在其中一个上使用 Program(),而不是将标志值发送到 GL 并在顶点尤其是片段着色器中创建昂贵的条件分支?

关于ios - OpenGL ES glFragColor 取决于 iOS 中片段着色器的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com