Hello Guest

glDrawArrays causes coredump with SIGSEGV

  • 2 Replies
  • 123 Views
glDrawArrays causes coredump with SIGSEGV
« on: November 28, 2020, 14:29:45 »
I'm loosely following this tutorial https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter04/chapter4.html to get familiar with lwjgl and OpenGL. The goal is to just get this simple triangle on screen but my code keeps on crushing on glDrawArrays call with coredump and SIGSEGV. Looks like OpenGL can't get access to the vbo data created but I can't see what and where could go wrong with that.

Code: [Select]
import org.lwjgl.glfw.GLFWErrorCallback;

import org.lwjgl.opengl.GL;

import java.nio.FloatBuffer;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Main {
    private long window;
    private ShaderProgram shaderProgram;
    private int vaoId;

    public void start() {
        GLFWErrorCallback.createPrint(System.err).set();

        if (!glfwInit())
            throw new IllegalStateException("Error");

        glfwDefaultWindowHints();

        glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

        window = glfwCreateWindow(1920, 1080, "OpenGL", NULL, NULL);

        if (window == NULL)
            throw new RuntimeException("Couldn't create a window");

        glfwSetKeyCallback(window, (window, key, scancode, action, mode) -> {
            // TODO
        });

        glfwMakeContextCurrent(window);

        glfwSwapInterval(1);

        glfwShowWindow(window);

        GL.createCapabilities();

        shaderProgram = new ShaderProgram();
        shaderProgram.attachShader(new Shader(Res.string("fragment.frag"), GL_FRAGMENT_SHADER));
        shaderProgram.attachShader(new Shader(Res.string("vertex.vert"), GL_VERTEX_SHADER));
        shaderProgram.link();

        float[] verts = new float[]{
                0.0f, 0.5f, 0.0f,
                -0.5f, -0.5f, 0.0f,
                0.5f, -0.5f, 0.0f
        };

        FloatBuffer vertBuffer = memAllocFloat(verts.length);
        vertBuffer.put(verts).flip();

        vaoId = glGenVertexArrays();
        glBindVertexArray(vaoId);

        int vboId = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glBufferData(GL_ARRAY_BUFFER, vertBuffer, GL_STATIC_DRAW);
        memFree(vertBuffer);

        glVertexAttribPointer(0, 3, GL_FLAT, false, 0, 0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

        glViewport(0, 0, 1920, 1080);
        glClearColor(1.f, 0.f, 1.f, 1.f);

        loop();
    }

    void loop() {
        while (!glfwWindowShouldClose(window)) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            shaderProgram.bind();

            glBindVertexArray(vaoId);
            glEnableVertexAttribArray(0);

            glDrawArrays(GL_TRIANGLES, 0, 3);

            glDisableVertexAttribArray(0);
            glBindVertexArray(0);

            shaderProgram.unbind();

            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    }

    public static void main(String[] args) {
        new Main().start();
    }
}

All the shader stuff seem to be working fine. But in case it can be relevant here's the gist with the rest of the code including shaders: https://gist.github.com/miriti/c578f4f4c000a4a84f1af260fa4ba28e

*

Offline KaiHH

  • ****
  • 310
Re: glDrawArrays causes coredump with SIGSEGV
« Reply #1 on: November 28, 2020, 14:49:57 »
The error is with the call:
Code: [Select]
glVertexAttribPointer(0, 3, GL_FLAT, false, 0, 0);
This call is generating an OpenGL error (and its effect is therefore ignored), because you pass the invalid constant `GL_FLAT`. It should be `GL_FLOAT`.
This causes glDrawArrays() to access undefined memory (because the vertex buffer has not been specified as the vertex attribute source).

EDIT:
Besides that:
Code: [Select]
glEnableVertexAttribArray(0);
can be executed within the VAO binding just once in start(). Vertex array enable state is part of the VAO and you don't have to enable/disable constantly.
« Last Edit: November 28, 2020, 14:51:44 by KaiHH »

Re: glDrawArrays causes coredump with SIGSEGV
« Reply #2 on: November 28, 2020, 15:14:07 »
OMG. That's embarrasing  :-[ Probably just choosen a wrong auto-complete item and it costed me hours of wondering.

Thank you! I see the triangle now. Also noted the glEnableVertexAttribArray remark. Thanks!