繁体   English   中英

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

[英]Error passing LWJGL Texture to GLSL shader

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

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();
}

它可能有些愚蠢,但是我对着色器还是陌生的,所以请忍受我的无知:)

实际上,将采样器制服使用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 ,即btw。 在内部使用gl_Vertex内置属性(取决于您的属性设置,该属性可能与您的Position属性相同或不相同)。 通常,在没有看到如何设置/查询属性位置以及如何指定顶点数组指针的代码的情况下,很难分辨出正确的值是否会在着色器中出现,以及我建议的固定值是否足够。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM