0
\$\begingroup\$

Currently my program can only render my vertices as white and changes in my vertex and fragment shader don't change what gets rendered for some reason i.e: (gl_Position = vec4(position+0.5,1); doesn't shift my triangle).

I'm not getting GL_FALSE from the glGetShaderi and glGetProgrami functions and I know my fragment shader and vertex shader are being read as glGetShaderi(shader,GL_COMPILE_STATUS) == GL_FALSE when I forget my semi-colon.

Anyone know how to solve this?

Vertex Shader:

#version 330

layout (location = 0) in vec3 position;

void main(){
    gl_Position = vec4(position+0.5,1);
}

Fragment Shader:

#version 330

out vec4 fragColor;

void main(){
    fragColor = vec4(1,1,0,1);
}

Main.java:

public class Main {
    static long window;
    private static int WIN_WIDTH = 500;
    private static int WIN_HEIGHT = 500;
    
    public static void main(String[] args) {
        // glfwInit
        if (!glfwInit()) {
            throw new IllegalStateException("Error for glfwInit");
        }
        
        // Hint
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,2);

        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GLFW_TRUE);
        
        glfwWindowHint(GLFW_VISIBLE,GLFW_FALSE);
        
        
        // Window
        // window create
        window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "warmulfollowalong", 0, 0);
        // window check
        if  (window == 0) {
            throw new IllegalStateException("Error creating window");
        }
        
        // CONTEXT
        glfwMakeContextCurrent(window);
        GL.createCapabilities();
        
        // window position
        
        GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, videoMode.width()/2-WIN_WIDTH/2, videoMode.height()/2-WIN_HEIGHT/2);
        
        // window display
        glfwShowWindow(window);
        
        
        
        // CREATE MESH
        Mesh testMesh = new Mesh();
        testMesh.create(new float[] {
                -1, -1, 0,
                0,1,0,
                1,-1,-1,
        });
        
        
        //
        Shader shader = new Shader();
        shader.create("shader");
        
        
        // LOOP
        while(!glfwWindowShouldClose(window)) {
            glfwPollEvents();
            
            // clear
            glClearColor(1, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT);
            
            // shader
            shader.useShader();
            
            // DRAW
            testMesh.draw(); 
            
            glfwSwapBuffers(window);
        }

        // destroy
        testMesh.destroy();
        shader.destroy();
        
        // close
        glfwTerminate();
    }
}

Mesh.java:

public class Mesh {
    private int vao;
    private int vbo;
    
    private int vertexCount;
    
    public void Mesh() {
        
    }
    
    public boolean create(float[] vertices) {
        // Create VAO and bind it
        vao = glGenVertexArrays();
        glBindVertexArray(vao);

        // Create empty buffer and bind it and bufferData
        vbo = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER,vertices,GL_STATIC_DRAW);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 0,0);
        
        // Unbind VAO
        glBindVertexArray(0);
        
        // Set vertexCount
        vertexCount = vertices.length / 3;
        
        return true;
    }
    
    public void destroy() {
        // delete vao & vbo
        glDeleteVertexArrays(vao);
        glDeleteBuffers(vbo);
    }
    
    public void draw() {    // Seems very inefficient
        // bind VAO
        glBindVertexArray(vao);
        // enable VBO (essentially) / enable vertex Attribute Array
        glEnableVertexAttribArray(0);   // !@$!@#!@# when to use???
        
        // DRAW
        glDrawArrays(GL_TRIANGLES, 0, vertexCount);

        // disable VBO (essentially) / enable vertex Attribute Array
        glDisableVertexAttribArray(0);
        // unbind VAO
        glBindVertexArray(0);
    }
}

Shader.java:

public class Shader {
    // stores int handle
    private int vertexShader, fragmentShader, program;
    
    public Shader(){
                
    }
    
    public boolean create(String shader) {
        // Create vertex shader
        int vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, readSource(shader+".vs"));
        glCompileShader(vertexShader);
        
        if(glGetShaderi(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
            throw new Error("ERROR: compiling vertex shader. Info: "+glGetShaderInfoLog(vertexShader));
        }
        
        // Create fragment shader
        int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader,readSource(shader+".fs"));
        glCompileShader(fragmentShader);
        
        if(glGetShaderi(fragmentShader,GL_COMPILE_STATUS) == GL_FALSE) {
            throw new Error("ERROR: compiling fragment shader. Info: "+glGetShaderInfoLog(fragmentShader));
        }
        
        // Program
        int program = glCreateProgram();
        glAttachShader(program, vertexShader);  // !@#!@#!@#
        glAttachShader(program, fragmentShader);
        
        // Program check
        glLinkProgram(program);
        if (glGetProgrami(program, GL_LINK_STATUS) == GL_FALSE) {
            throw new Error("ERROR: linking program. Info: "+glGetProgramInfoLog(program));
        }
        glValidateProgram(program);
        if (glGetProgrami(program,GL_VALIDATE_STATUS) == GL_FALSE) {
            throw new Error("ERROR: validating program. Info: "+glGetProgramInfoLog(program));
        }
        
        
        return true;    // !@#!@# Shouldn't you be passing down the program? Isn't this like an anti pattern?
        
    }
    
    public void destroy() {
        // detach
        glDetachShader(program, vertexShader);
        glDetachShader(program, fragmentShader);
        
        // delete shaders
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    
        // delete program
        glDeleteProgram(program);
    }
    
    public void useShader() {
        glUseProgram(program);
    }
    
    public String readSource(String file) {
        BufferedReader reader = null;
        StringBuilder sourceBuilder = new StringBuilder();
        
        try {
            reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/shaders/"+file)));

            String line;
            
            while((line = reader.readLine()) != null) {
                sourceBuilder.append(line+"\n");    //<= remember to add new line
            }
        }catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                //MY
                if(reader != null){
                    reader.close();
                }
            }catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        
        return sourceBuilder.toString();
    }
    
}

```
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The culprit was the int in int program = glCreateProgram();.

Using the below code ultimately helped solve this problem:

glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE );


// Place the below two after you've run `glfwMakeContextCurrent(window);` & `GL.createCapabilities();` and before the GL code
glEnable(org.lwjgl.opengl.KHRDebug.GL_DEBUG_OUTPUT_SYNCHRONOUS);
// store this in a field somewhere so it doesn't gets GC'd
Callback callbackA = GLUtil.setupDebugMessageCallback();
```
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .