gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:16:37 24 4
gpt4 key购买 nike

我正在尝试最小化和简化本教程中的代码:

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"));

在“start”方法中...

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

这可能是一些愚蠢的事情,但我对着色器很陌生,所以请忍受我的无知:)

最佳答案

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

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

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

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

gl_FragColor = texture2D(u_texture, vTexCoord);

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

关于java - 将 LWJGL 纹理传递到 GLSL 着色器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22417140/

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