Hello Guest

Shaders not working after compilation success... FIXED

  • 0 Replies
  • 2297 Views
*

Offline ste3e

  • *
  • 20
Shaders not working after compilation success... FIXED
« on: November 11, 2010, 01:11:37 »
Here is NetBean's output:
run:
Info log:
Vertex shader was successfully compiled to run on hardware.
Info log:
Fragment shader was successfully compiled to run on hardware.
Info log:
Fragment shader(s) linked, vertex shader(s) linked.
Validation successful.


On the back of which I get a black screen. Here are the shaders:
Vertex:
Code: [Select]
uniform mat4 g_WorldViewProjectionMatrix;
attribute vec3 inPosition;
varying vec4 vertColor;

void main(){
    gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
    vertColor = vec4(0.6, 0.3, 0.4, 1.0);
}

Fragment:
Code: [Select]
varying vec4 vertColor;

void main(){
    gl_FragColor = vertColor;
}

And here is the LWJGL code... draw() is called from within the game loop, and if useShader is set to if(!useShader) a white square appears.
Code: [Select]
package mygame;

import org.lwjgl.opengl.GL11;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.ARBVertexShader;
import org.lwjgl.opengl.ARBFragmentShader;
import org.lwjgl.opengl.Util;


/**
 *
 * @author ste3e
 */
public class Box {
    private boolean useShader=true;
    private int shader=0;
    private int vertShader=0;
    private int fragShader=0;

    public Box(){
        shader=ARBShaderObjects.glCreateProgramObjectARB();
        if(shader!=0){
            vertShader=createVertShader("shaders/screen.vert");
            fragShader=createFragShader("shaders/screen.frag");
        }else useShader=false;

        if(vertShader !=0 && fragShader !=0){
            ARBShaderObjects.glAttachObjectARB(shader, vertShader);
            ARBShaderObjects.glAttachObjectARB(shader, fragShader);
            ARBShaderObjects.glLinkProgramARB(shader);
            ARBShaderObjects.glValidateProgramARB(shader);
            printLogInfo(shader);
        }else useShader=false;
    }


    public void draw(){
              
        GL11.glLoadIdentity();
        GL11.glTranslatef(0.0f, 0.0f, -10.0f);
        GL11.glColor3f(1.0f, 1.0f, 1.0f);
        if(useShader) {
            ARBShaderObjects.glUseProgramObjectARB(shader);
        }
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
            GL11.glVertex3f(1.0f, 1.0f, 0.0f);
            GL11.glVertex3f(1.0f, -1.0f, 0.0f);
            GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
GL11.glEnd();

        ARBShaderObjects.glUseProgramObjectARB(0);
    }

    private int createVertShader(String filename){
        vertShader=ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
        if(vertShader==0){return 0;}
        String vertexCode="";
        String line;
        try{
            BufferedReader reader=new BufferedReader(new FileReader(filename));
            while((line=reader.readLine())!=null){
                vertexCode+=line + "\n";
            }
        }catch(Exception e){
            System.out.println("Fail reading vertex shading code");
            return 0;
        }
        ARBShaderObjects.glShaderSourceARB(vertShader, vertexCode);
        ARBShaderObjects.glCompileShaderARB(vertShader);
        printLogInfo(vertShader);

        return vertShader;
    }

    private int createFragShader(String filename){
        fragShader=ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
        if(fragShader==0){return 0;}
        String fragCode="";
        String line;
        try{
            BufferedReader reader=new BufferedReader(new FileReader(filename));
            while((line=reader.readLine())!=null){
                fragCode+=line + "\n";
            }
        }catch(Exception e){
            System.out.println("Fail reading fragment shading code");
            return 0;
        }
        ARBShaderObjects.glShaderSourceARB(fragShader, fragCode);
        ARBShaderObjects.glCompileShaderARB(fragShader);
        printLogInfo(fragShader);

        return fragShader;
    }

    private static void printLogInfo(int obj){
        IntBuffer iVal = BufferUtils.createIntBuffer(1);
ARBShaderObjects.glGetObjectParameterARB(obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);

int length = iVal.get();
        if (length > 1) {
            // We have some info we need to output.
            ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
            iVal.flip();
            ARBShaderObjects.glGetInfoLogARB(obj,  iVal, infoLog);
            byte[] infoBytes = new byte[length];
            infoLog.get(infoBytes);
            String out = new String(infoBytes);
            System.out.println("Info log:\n"+out);
}
            Util.checkGLError();
}
}

Do shaders not work in LWJGL? Problem was using jME3 shader speak in lwjgl... vertex shader should be:

Code: [Select]
varying vec4 vertColor;

void main(){
    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
    vertColor = vec4(0.6, 0.3, 0.4, 1.0);
}


« Last Edit: November 11, 2010, 02:38:10 by ste3e »