shader compiling

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

shader compiling

Ale99
sorry for my bad english, but i'm Italian

I just started learning OpenGL. I'm studying with the "Red Book" and i use JOGL.
Now i'm trying to use shaders. This is the code


Shader loader class, i took a part of code from web:
package org.yourorghere;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
import javax.media.opengl.GL;

public class MyShaders {

    // The shader control class.
// loads and starts/stops shaders.
    private int vertexShaderProgram;
    private int fragmentShaderProgram;
    private int shaderprogram;
    public String[] vsrc;
    public String[] fsrc;

    // this will attach the shaders
    public void init(GL gl) {
        try {
            attachShaders(gl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   
    public String[] loadShader(String name) {
        StringBuilder sb = new StringBuilder("");
        try {
            FileReader is = new FileReader(name);
            BufferedReader br = new BufferedReader(is);
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append('\n');
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("Shader is " + sb.toString());
        //return new String[]{sb.toString()};
       
       
        return sb.toString().split("\n");
    }

   
    private void attachShaders(GL gl) throws Exception {
        IntBuffer intBuffer = IntBuffer.allocate(1);
       
        vertexShaderProgram = gl.glCreateShader(GL.GL_VERTEX_SHADER);
        fragmentShaderProgram = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
        System.out.println("glCreateShader(vertex)=" + vertexShaderProgram);
        System.out.println("glCreateShader(fragment)=" + fragmentShaderProgram);

for(String str : fsrc)
            System.out.println(str);
System.out.println("len-> "+Arrays.toString(getStrLen(fsrc)));
       
for(String str : vsrc)
            System.out.println(str);
System.out.println("len-> "+Arrays.toString(getStrLen(vsrc)));
       
 
       
        //gl.glShaderSource(vertexShaderProgram, 1, vsrc, null, 0);
        gl.glShaderSource(vertexShaderProgram, vsrc.length, vsrc, getStrLen(vsrc), 0);
       
        gl.glCompileShader(vertexShaderProgram);
        gl.glGetShaderiv(vertexShaderProgram,GL.GL_COMPILE_STATUS,intBuffer);
        System.out.println("glCompileShader(vertex)="+intBuffer.get(0));
       
        //gl.glShaderSource(fragmentShaderProgram, 1, fsrc, null, 0);
        gl.glShaderSource(fragmentShaderProgram, fsrc.length, fsrc, getStrLen(fsrc), 0);
       
        gl.glCompileShader(fragmentShaderProgram);
        gl.glGetShaderiv(fragmentShaderProgram,GL.GL_COMPILE_STATUS,intBuffer);
        System.out.println("glCompileShader(fragment)="+intBuffer.get(0));
       
        if(intBuffer.get(0)==GL.GL_FALSE){
            gl.glGetProgramiv(shaderprogram, GL.GL_INFO_LOG_LENGTH, intBuffer);
           
            int size = intBuffer.get(0);
            if (size > 0) {
                ByteBuffer byteBuffer = ByteBuffer.allocate(size);
                gl.glGetProgramInfoLog(shaderprogram, size, intBuffer, byteBuffer);
                for (byte b : byteBuffer.array()) {
                    System.err.print((char) b);
                }
            } else {
                System.out.println("Unknown");
            }
        }
       
        shaderprogram = gl.glCreateProgram();
        System.out.println("glCreateProgram="+shaderprogram);

        gl.glAttachShader(shaderprogram, vertexShaderProgram);
        gl.glAttachShader(shaderprogram, fragmentShaderProgram);
       
        gl.glLinkProgram(shaderprogram);
        gl.glGetProgramiv(fragmentShaderProgram,GL.GL_LINK_STATUS,intBuffer);
        System.out.println("glLinkProgram()="+intBuffer.get(0));
       
        gl.glValidateProgram(shaderprogram);
        gl.glGetProgramiv(fragmentShaderProgram,GL.GL_VALIDATE_STATUS,intBuffer);
        System.out.println("glValidateProgram()="+intBuffer.get(0));
       
        gl.glGetProgramiv(shaderprogram, GL.GL_LINK_STATUS, intBuffer);

        if (intBuffer.get(0) != GL.GL_TRUE) {

            System.out.println("GL_LINK_STATUS=" + intBuffer.get(0));

            gl.glGetProgramiv(shaderprogram, GL.GL_INFO_LOG_LENGTH, intBuffer);
            int size = intBuffer.get(0);

            System.out.println("GL_INFO_LOG_LENGHT=" + size);

            System.out.println("Program link error: ");
            if (size > 0) {
                ByteBuffer byteBuffer = ByteBuffer.allocate(size);
                gl.glGetProgramInfoLog(shaderprogram, size, intBuffer, byteBuffer);
                for (byte b : byteBuffer.array()) {
                    System.err.print((char) b);
                }
            } else {
                System.out.println("Unknown");
            }
        }
    }

    // this function is called when you want to activate the shader.
    // Once activated, it will be applied to anything that you draw from here on
    // until you call the dontUseShader(GL) function.
    public int useShader(GL gl) {
        gl.glUseProgram(shaderprogram);
        return shaderprogram;
    }

    // when you have finished drawing everything that you want using the shaders,
    // call this to stop further shader interactions.
    public void dontUseShader(GL gl) {
        gl.glUseProgram(0);
    }
   
    int[] getStrLen(String[] s){
        int[] a = new int[s.length];
       
        for(int i = 0; i<s.length ; i++){
            a[i] = s[i].length();
        }
       
        return a;
    }
}




vertex.glsl, from book :
#version 220 core

layout(location = 0) in vec4 vPosition;

void
main()
{
    gl_Position = vPosition;
}



fragment.glsl, from book :
#version 220 core

out vec4 fColor

void
main()
{
    fColor = vec4(0.0f,0.0f,0.0f,1.0f);
}



Console Output :
glCreateShader(vertex)=1
glCreateShader(fragment)=2
#version 220 core

out vec4 fColor

void
main()
{
    fColor = vec4(0.0f,0.0f,0.0f,1.0f);
}
len-> [17, 0, 15, 0, 4, 6, 1, 39, 1]
#version 220 core

layout(location = 0) in vec4 vPosition;

void
main()
{
    gl_Position = vPosition;
}
len-> [17, 0, 39, 0, 4, 6, 1, 28, 1]
glCompileShader(vertex)=0                
glCompileShader(fragment)=0
Unknown
glCreateProgram=3
glLinkProgram()=0
glValidateProgram()=0
GL_LINK_STATUS=0
GL_INFO_LOG_LENGHT=1
Program link error:
Start printing...




Problem :
why

glCompileShader(vertex)=0                
glCompileShader(fragment)=0

GL_LINK_STATUS=0
GL_INFO_LOG_LENGHT=1
Program link error:


Does it mean that there was an error while compiling the shaders?



PS: is there any "code" tag in the forum?

Reply | Threaded
Open this post in threaded view
|

Re: shader compiling

gouessej
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: shader compiling

Ale99
thanks very very much

i was using JOGL with NetBeans plugin, but it hasn't a lot of functions which i was searching!
i have just downloaded the new libs from Maven and tested them. They're working fine, so tomorrow i'll try with your example!

But did i do any terrible mistake in my original class? or what??

Reply | Threaded
Open this post in threaded view
|

Re: shader compiling

gouessej
Administrator
Sorry but the Netbeans plugin isn't maintained since several years.

To be honest, the compilation seemed to work but not the linking and I don't know why.
Julien Gouesse | Personal blog | Website