Question

Im trying to minimize and simplify the code from this tutorial:

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

Albeit a very good tutorial, the code example is over-complex. I managed to pass simple vec3 and vec4's to GLSL, and now i want to pass the texture and texture normals to use their info on the color calculation.

This is my code:

Loading the textures...

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

In the "start" method...

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

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

in the "render" method...

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

Although my goal is to be able to apply the texture to the quad from within the shader, this is what appears in my console:

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.
#

Weirdly enough, if I change "GL_TEXTURE0" to "0" and "GL_TEXTURE1" to "1", the quad appears with a very weird and noisy texture (when i say noisy, i literally mean with dynamic noise going on). Here is the image:

enter image description here

I dont know if the image has anything to do, probably its not even supposed to use 0 and 1 as inputs so its understandable that the result is as unpredictable as this. Still, I dont know why the first code doesnt work.

Here's also the code from the shaders (there's some unused uniforms because im still debugging the passed info before i actually do the math)

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

Its probably something dumb but I am new to shaders so bear with my ignorance :)

Was it helpful?

Solution

Using 0 and 1 for the sampler uniforms to refer to GL_TEXTURE0 and GL_TEXTURE1 is actually the correct way.

I can see at least one additional mistake in your shaders which might explain your results:

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

You use gl_TexCoord[0] here. This is a deprecated builtin varying. You shouldn't use it in the first place. However, the fact that it is deprecated is not the main issue here, but the fact that its value is undefined, since you never write to it in your vertex shader. You actually have declared the varying vTexCoord in both of your shaders, and you even write the texture coords to it, but you just don't use it. You can simply change that line in your FS to:

gl_FragColor = texture2D(u_texture, vTexCoord);

As a side note: why are you using this rather old version of GLSL? Are you somehow limited to GL2.0, GLES2.0 or webgl? You should be aware that there are more modern versions. And you should avoid using the deprecated stuff like ftransform, which, btw. internally uses the gl_Vertex builtin attribute (which might or might not be identical to your Position attribute, that depends on your attribute setup). In general, without seeing the code of how you set up/query your attribute locations and how you specify your vertex array pointers, it is hard to tell if the correct values will end up in the shader and if my suggested fixed will be enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top