OpenGL ES 2.0多个着色器程序-纹理渲染不再起作用 [英] OpenGL ES 2.0 Multiple shader programs - texture rendering not working anymore

查看:135
本文介绍了OpenGL ES 2.0多个着色器程序-纹理渲染不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序绘制了点精灵和标准四边形纹理的混合体.直到最近,我还为此使用了一个着色器程序-但是片段着色器中的一个分支(决定是否使用点sprite坐标/颜色)严重限制了我的渲染性能.我的片段着色器最初看起来像这样:

My app draws a mixture of point sprites and standard quad textures. Until recently, I was using a single shader program for this - however a branch in the fragment shader (to decide whether to use point sprite coords/colors) was seriously throttling the performance of my rendering. My fragment shader originally looked like this:

precision highp float;

uniform sampler2D u_textureSprite;
uniform bool u_isSprite;

varying vec4 v_color;
varying vec2 v_textureCoord;

void main()
{    
    vec4 textureColor = texture2D(u_textureSprite, u_isSprite ? gl_PointCoord : v_textureCoord);  
    if (u_isSprite) {
        gl_FragColor = v_color * textureColor;
    }
    else {
        gl_FragColor = textureColor;
    }
}

在OpenGL ES编程指南中阅读Apple的建议,听起来很容易使用多个着色器程序...只需正常创建另一个着色器程序,然后在相关绘制代码之前调用glUseProgram().但是,由于这样做,我无法使纹理渲染正常工作.两个新的片段着色器是:

Reading Apple's recommendations in the OpenGL ES Programming guide makes it sound like an easy job to use multiple shader programs... just create another one as normal and call glUseProgram() before the relevant draw code. However, since doing this I cannot get the texture rendering to work. The two new fragment shaders are:

pointSprite.fsh:

precision highp float;

uniform sampler2D s_textureSprite;

varying vec4 v_color;

void main()
{    
    vec4 textureColor = texture2D(s_textureSprite, gl_PointCoord);
    gl_FragColor = v_color * textureColor;
}

texture.fsh:

precision highp float;

uniform sampler2D s_texture;


varying vec2 v_textureCoord;

void main()

{    
    gl_FragColor = texture2D(s_texture, v_textureCoord);
}

我认为这很琐碎.如果我忽略所有绘制纹理的调用,则可以看到点精灵正在被很好地渲染.但是,纹理四边形只能渲染为纯灰色-顺便说一句,此灰色与任何glClearColor()颜色都不匹配,我认为它是从它要渲染的纹理的某个角度来看的颜色.

Pretty trivial, I think. If I ignore all calls to draw the texture, I can see that point sprites are being rendered just fine. Texture quads, however, just render to a solid grey color - incidentally this grey color does not match any of the glClearColor() colors, I believe it is a color from some point of the texture that it is trying to render.

纹理渲染的一些示例代码如下:

A little sample code for a texture render is as follows:

- (void)drawTexture:(GLuint)texture {
    glUseProgram(programs[PROGRAM_TEXTURE]);


    glBindTexture(GL_TEXTURE_2D, texture);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjects[VBO_TEXTURE_VERTICES]);

    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(ISTextureData), (void*)offsetof(ISTextureData, vertex));
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_TEXTURE_COORD, 2, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ISTextureData), (void*)offsetof(ISTextureData, textureCoord));
    glEnableVertexAttribArray(ATTRIB_TEXTURE_COORD);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexBufferObjects[VBO_TEXTURE_INDICIES]);
    glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

当不处理多个着色器时,此代码有效.还是我很粗心,纹理片段着色器代码错误?

This code works when not dealing with multiple shaders. Or am I being careless, and the texture fragment shader code is wrong?

谢谢

这是一个漫长而痛苦的时光,真正令人恼火的是,这在某种程度上不是代码问题,而是与项目目录的名称有关.

Well it's been a number of long and painful hours, and what's really irritating is that it's somehow not a code issue, but something to do with the name of the project directory.

我的Xcode项目目录的命名如下:"ProjectName(当前)".这是我遇到的项目.从那以后,我发现将此文件夹名称更改为任何其他名称都可以使应用程序正常工作并正确渲染背景纹理.需要澄清的是-该应用程序以前可以正常运行,除了在OpenGL中渲染背景纹理外,并且在重命名项目文件夹后,一切正常.

My Xcode project directory is named as such: "ProjectName (current)". This is the project I was having trouble with. I have since discovered that changing this folder name to anything different makes the app work, and properly render the background texture. Just to clarify - the app previously worked normally with the exception of the rendering of a background texture in OpenGL, and after renaming the project folder everything works fine.

为什么会这样?

推荐答案

奇怪的是,这与项目目录的名称有关.我的Xcode项目目录这样命名:"ProjectName(当前)".这是我遇到的项目.将此文件夹名称更改为任何其他名称都可以使应用程序正常工作,并正确渲染背景纹理.很奇怪.

This, strangely, turned out to be somehow related to the name of the project directory. My Xcode project directory was named as such: "ProjectName (current)". This is the project I was having trouble with. Changing this folder name to anything different made the app work, and properly render the background texture. Very strange.

这篇关于OpenGL ES 2.0多个着色器程序-纹理渲染不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆