【发布时间】: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