Problem creating shaders

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

Problem creating shaders

exessuz
Hi, currently I am trying to port an C++/GL app that I made some time ago to JOGL, everything was going great till I got to the shaders part heres the code

I am using GL2, I have tried with glCreateShader and also ARB Extension and every time I get a Invalid Operation 1282
when I call glUserProgram I know that there's a chance that ther error is stacked and is not related to the gluseprogram, but I have checked for GLErrors in every step. Thanks in advanced for your help.

                        int vs = gl.glCreateShaderObjectARB(GL2.GL_VERTEX_SHADER);
                        int ps = gl.glCreateShaderObjectARB(GL2.GL_FRAGMENT_SHADER);
                       
                       
                        String[] tt  = Tools.readFile(vssource);
                        gl.glShaderSourceARB( vs, 1, tt,(int[]),0);
                        gl.glCompileShaderARB(vs);
                       
                        tt  = Tools.readFile(fssource);
                        gl.glShaderSourceARB(ps, 1, tt,(int[]),0);
                        gl.glCompileShaderARB(ps);

                        program = gl.glCreateProgramObjectARB();
                       
                        gl.glAttachShader(program,vs);
                        gl.glAttachShader(program,ps);
                       
                        gl.glLinkProgramARB(program);
                        gl.glValidateProgramARB(program);
                       
                        gl.glUseProgramObjectARB(program);

Reply | Threaded
Open this post in threaded view
|

Re: Problem creating shaders

jmaasing
I attached some sample code from my project, I decided to use some utility classes from jogl to help out with my shader compilation, maybe those could be useful in your case. I'm using GL4 and NEWT so adjust accordingly.

final String vertShaderString = "..." ;
final String fragShaderString = "..." ;

private ShaderCode compileShader(final GL4 gl4, final String source, final int shaderType)
                throws IOException, ShaderCompilationException {

        final String[][] sources = new String[1][1];
        sources[0] = new String[]{source};
        ShaderCode shaderCode = new ShaderCode(shaderType, sources.length, sources);
        final boolean compiled = shaderCode.compile(gl4, System.err);
        if (!compiled) {
                throw new ShaderCompilationException("Unable to compile " + source);
        }
        return shaderCode;
}

private int linkShader(GL4 gl, final ShaderCode vertexShader, final ShaderCode fragmentShader)
                throws GLException, ShaderCompilationException {
        ShaderProgram program = new ShaderProgram();
        program.init(gl);
        program.add(vertexShader);
        program.add(fragmentShader);
        program.link(gl, System.out);

        final boolean validateProgram = program.validateProgram(gl, System.out);
        if (!validateProgram) {
                throw new ShaderCompilationException("Unable to link");
        }
        return program.program();
}

final ShaderCode vertexShader = compileShader(gl, vertShaderString, GL4.GL_VERTEX_SHADER);
final ShaderCode fragmentShader = compileShader(gl, fragShaderString, GL4.GL_FRAGMENT_SHADER);
final int textureShaderProgram = linkShader(gl, vertexShader, textureFragmentShader);

// Operate on the shader program
final int shaderPosition = gl.glGetAttribLocation(textureShaderProgram, "position");

---

// Using NEWT I get a GLAutoDrawable, this is how I get a lot of debug and
// error checking for all GL operations

@Override
public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        if (gl.isGL4core()) {
                drawable.setGL(new TraceGL4(new DebugGL4(gl.getGL4()), System.out));
                GL4 gL4 = drawable.getGL().getGL4();
 
Reply | Threaded
Open this post in threaded view
|

Re: Problem creating shaders

exessuz
jmassing thanks for your quick help, well i tried your code and this time I am getting a different error, related to not setting the precision, I solved it by calling the addDefaultShaderPrecision on the shadercode object, and it worked..! so I went back to my previous implementation and added the "precision mediump float" on the shader file and it worked also, so thanks for the help.                
Reply | Threaded
Open this post in threaded view
|

Re: Problem creating shaders

jmaasing