将LWJGL纹理传递给GLSL着色器时出错

时间:2014-03-14 22:48:18

标签: java opengl glsl shader lwjgl

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

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”,那么四边形会出现非常奇怪和嘈杂的纹理(当我说嘈杂时,我的意思是动态噪音正在进行) 。这是图像:

enter image description here

我不知道图像是否有任何事情要做,可能它甚至不应该使用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();
}

它可能是愚蠢的东西,但我是着色器的新手所以忍受我的无知:)

1 个答案:

答案 0 :(得分: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属性相同或不同,这取决于您的属性设置)。通常,在没有看到如何设置/查询属性位置以及如何指定顶点数组指针的代码的情况下,很难判断正确的值是否会在着色器中结束,以及我的建议修复是否足够。