Question

Solved did a mistake when setting up the ortho graphic view:

Schould be like this:

glOrtho(-1, 1, 1, -1, -1, 1);

Instead of this:

glOrtho(0, 1000, 600, 0, -1, 1)

My screeen width is 1000 and height 600.

Thanks for all the help

I'm making a 2d game in java with OpenGL(lwjgl). I could draw a triangle, but when using my shader it dosen't work. The shaders compiles no errors what so ever, but the screeen goes black.

I have absolutely no idea of what to do, do i have to enable something?

This is my vertexShader code in GLSL

#version 430

layout (location = 0) in vec2 position;


void main()
{
    color = vec4(1, 0, 0, 1);

    gl_Position = vec4(position, -1, 1);
}

This is the fragment shader:

#version 430 

void main()
{ 
    gl_FragColor = vec4(1, 1, 1, 1); 
}

I recently discovered that if i only run the fragment shader it works, i get the specified color. But if i also run the vertex shader it all goes black so the problem is in the vertex shader.

Shader class:

` import static org.lwjgl.opengl.GL20.; import static org.lwjgl.opengl.GL32.;

public class Shader {

private int program;

public Shader() {
    program = glCreateProgram();

    if (program == 0) {
        System.err.println("ShaderProgram creation failed!!!, couldn't find valid memory location!");
    }
}

public void addVertexShader(String text) {
    addProgram(text, GL_VERTEX_SHADER);
}

public void addFragmentShader(String text) {
    addProgram(text, GL_FRAGMENT_SHADER);
}

public void addGeometryShader(String text) {
    addProgram(text, GL_GEOMETRY_SHADER);
}

public void addProgram(String shaderText, int type){
    int shader = glCreateShader(type);
    
    if (shader == 0){
        System.err.println("Shader creation failed!!!");
    }
    
    glShaderSource(shader, shaderText);
    glCompileShader(shader);
    
    if(glGetShader(shader, GL_COMPILE_STATUS) == 0){
        System.err.println(glGetShaderInfoLog(shader, 2048));
    }
    
    glAttachShader(program, shader);
    
}

public void compile(){
    glLinkProgram(program);
    
    if (glGetProgram(program, GL_LINK_STATUS) == 0){
        System.err.println(glGetProgramInfoLog(program, 2048));
    }
    
    glValidateProgram(program);
    
    if(glGetProgram(program, GL_VALIDATE_STATUS) == 0){
        System.err.println(glGetProgramInfoLog(program, 2048));
    }
}

public void bind(){
    glUseProgram(program);
}

}`

Main java class:

`public class Main {

Mesh mesh;
Shader shader;

public Main() {
    init();
    mesh = new Mesh();
    Vertex[] data = new Vertex[]{new Vertex(new Vector2f(500, 0)),
                                 new Vertex(new Vector2f(1000, 600)),
                                 new Vertex(new Vector2f(0, 600))};
    
    mesh.addVertices(data);
    shader = new Shader();
    shader.addVertexShader(Util.loadShader("vertexShader.vs"));
    shader.addFragmentShader(Util.loadShader("fragmentShader.fs"));
    shader.compile();
    
    renderLoop();
}

private void MainLoop() {
    new Thread("Update"){
        public void run(){
            long lastTime = System.nanoTime();
            final double ns = 100000000 / 60;
            double delta = 0;
            while (!Display.isCloseRequested()) {
                long now = System.nanoTime();

                delta += (now - lastTime) / ns;

                lastTime = now;
                while (delta > 1) {
                    delta--;
                    update();
                }
            }
        }
    }.start();
}   

private void renderLoop() {
    MainLoop();
    long Timer = System.currentTimeMillis();
    int fps = 0;
    while (!Display.isCloseRequested()) {
        glClear(GL_COLOR_BUFFER_BIT);
        render();
        Display.update();
        fps++;
        if(System.currentTimeMillis() - Timer > 1){
            System.out.println("FPS: " + fps);
            Display.setTitle("The Unknown Path || FPS: " + fps);
            fps = 0;
            Timer += 1000;
        }
    }

}

private void update() {
    
}

private void render() {
    shader.bind();
    mesh.render();

}

private void init() {
    try {
        Display.setDisplayMode(new DisplayMode(1000, 600));
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1000, 600, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    // glEnable(GL_TEXTURE_2D);

    System.out.println(glGetString(GL_VERSION));
}

public static void main(String[] args) {
    Main m = new Main();

    System.exit(0);
}

}`

Was it helpful?

Solution

Solved did a mistake when setting up the ortho graphic view:

Schould be like this:

glOrtho(-1, 1, 1, -1, -1, 1);

Instead of this:

glOrtho(0, 1000, 600, 0, -1, 1)

My screeen width is 1000 and height 600.

Thanks for all the help

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