【问题标题】:Error passing LWJGL Texture to GLSL shader将 LWJGL 纹理传递给 GLSL 着色器时出错
【发布时间】:2014-04-20 11:03:42
【问题描述】:

我试图最小化和简化本教程中的代码:

https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6

虽然是一个非常好的教程,但代码示例过于复杂。 我设法将简单的 vec3 和 vec4 传递给 GLSL,现在我想传递纹理和纹理法线以将它们的信息用于颜色计算。

这是我的代码:

正在加载纹理...

//textures
private Texture rock;
private Texture rockNormals;

rock = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("src/main/rock.png"));
rockNormals = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("src/main/rock_n.png"));

在“开始”方法中...

//Diffuse Texture
int diffLoc = glGetUniformLocation(shaderProgram, "u_texture");
glUniform1i(diffLoc, GL_TEXTURE0);

//Normals Image
int normalsLoc = glGetUniformLocation(shaderProgram, "u_normals");
glUniform1i(normalsLoc, GL_TEXTURE1);

在“渲染”方法中...

   private void render()
    {
        //activate shader and update variable uniforms
        glUseProgram(shaderProgram);

        //Light Position
        lightPos.x = Mouse.getX() / (float) Display.getWidth();
        lightPos.y = Mouse.getY() / (float) Display.getHeight();

        int lightPosLoc = glGetUniformLocation(shaderProgram, "LightPos");
        glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);

        //bind diffuse color to texture unit 0
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, rock.getTextureID());

        //bind normal map tp texture unit 1
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, rockNormals.getTextureID());

        glBegin(GL_QUADS);
            glVertex2f(-0.5f, -0.5f);
            glVertex2f(0.5f, -0.5f);
            glVertex2f(0.5f, 0.5f);
            glVertex2f(-0.5f, 0.5f);
        glEnd();

        glUseProgram(0);
    }

虽然我的目标是能够从着色器中将纹理应用到四边形,但这就是我的控制台中显示的内容:

Fri Mar 14 22:38:55 GMT 2014 INFO:Use Java PNG Loader = true
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000000cf36210, pid=9160, tid=600
#
# JRE version: 7.0_25-b17
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.25-b01 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [ig75icd64.dll+0x116210]  RegisterProcTableCallback+0x10cac0
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Hugo\Desktop\Domus\Programação\Java\workspace\LwjglShaders\hs_err_pid9160.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

奇怪的是,如果我将“GL_TEXTURE0”更改为“0”并将“GL_TEXTURE1”更改为“1”,四边形会出现非常奇怪和嘈杂的纹理(当我说嘈杂时,我的字面意思是动态噪声正在进行) .这是图片:

我不知道图像是否有任何关系,可能它甚至不应该使用 0 和 1 作为输入,因此结果如此不可预测是可以理解的。不过,我不知道为什么第一个代码不起作用。

这也是来自着色器的代码(有一些未使用的制服,因为在我真正做数学之前我仍在调试传递的信息)

.frag

//attributes from vertex shader
varying vec4 vColor;
varying vec2 vTexCoord;

//our texture samplers
uniform sampler2D u_texture;   //diffuse map
uniform sampler2D u_normals;   //normal map

//values used for shading algorithm...
uniform vec2 Resolution;      //resolution of screen
uniform vec3 LightPos;        //light position, normalized
uniform vec4 LightColor;      //light RGBA -- alpha is intensity
uniform vec4 AmbientColor;    //ambient RGBA -- alpha is intensity 
uniform vec3 Falloff;         //attenuation coefficients

void main() {
        gl_FragColor = texture2D(u_texture, gl_TexCoord[0].st);
}

.vert

//combined projection and view matrix
uniform mat4 u_projView;

//"in" attributes from our SpriteBatch
attribute vec2 Position;
attribute vec2 TexCoord;
attribute vec4 Color;

//"out" varyings to our fragment shader
varying vec4 vColor;
varying vec2 vTexCoord;

void main() {
    vColor = Color;
    vTexCoord = TexCoord;
    //gl_Position = u_projView * vec4(Position, 0.0, 1.0);
    gl_Position = ftransform();
}

这可能有点愚蠢,但我是着色器的新手,所以请忍受我的无知:)

【问题讨论】:

    标签: java opengl glsl shader lwjgl


    【解决方案1】:

    使用01作为采样器制服来引用GL_TEXTURE0GL_TEXTURE1实际上是正确的方式。

    我可以在您的着色器中看到至少一个额外的错误,这可以解释您的结果:

    gl_FragColor = texture2D(u_texture, gl_TexCoord[0].st);
    

    你在这里使用gl_TexCoord[0]。这是一个弃用的内置变量。你不应该首先使用它。然而,它被弃用的事实不是这里的主要问题,而是它的值是 undefined 的事实,因为你从来没有在你的顶点着色器中写入它。你实际上已经在你的两个着色器中声明了不同的vTexCoord,你甚至给它写了纹理坐标,但你只是不使用它。您只需将 FS 中的那一行更改为:

    gl_FragColor = texture2D(u_texture, vTexCoord);
    

    附带说明:您为什么要使用这个相当旧的 GLSL 版本?您是否以某种方式受限于 GL2.0、GLES2.0 或 webgl?您应该知道还有更现代的版本。你应该避免使用像ftransform这样的弃用的东西,顺便说一句。在内部使用 gl_Vertex 内置属性(可能与您的 Position 属性相同也可能不同,这取决于您的属性设置)。一般来说,如果没有看到如何设置/查询属性位置以及如何指定顶点数组指针的代码,很难判断正确的值是否最终会出现在着色器中,以及我建议的修复是否足够。

    【讨论】:

    • 我不限于gl2.0,但是我的大学教的是旧版本,我在网上找到的教程也有点旧,我不知道如何学习最新版本,但你是对的,我刚刚下载并导入了具有 OpenGL 4.4 的新 lwjgl 库,我想学习如何使用最新版本来做到这一点,如果你能解释我或指导我找到一个好的链接 id 欣赏(顺便说一句,你的修复还不够,我的意思是噪音停止了,但广场全是蓝色的)
    • @user2295607:经常推荐用于学习现代 GL 的是 arcsynthesis tutorialopen.gl guide。至于你的问题:我猜你可能会使用glTexCoordPointer(甚至glTexCoor2f() 调用)来指定纹理坐标。如果是这种情况,它们将不会出现在您的 TexCoord 属性中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2016-12-20
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多