2

I've written a simple "hello word triangle" in OpenGL/C++, it works perfectly. Then, when i was sure that the code worked, I decided to switch to dart and convert all my code.

These are the the pulgins that i've used: OpenGL - GLFW(mingw x64 dll)

And this is the OpenGL code without all the GLFW abstraction:

    int shader;

    @override
    void onCreate() {

        List<double> pos = [
            -0.5, -0.5,
             0.0,  0.5,
             0.5, -0.5,
        ];

        Pointer<Float> pPos = allocate<Float>(count: pos.length);
        for(int i = 0; i < pos.length; i++)
            pPos.elementAt(i).value = pos[i];

        Pointer<Uint32> buffer = allocate();

        glGenBuffers(1, buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer.value);
        glBufferData(GL_ARRAY_BUFFER, 6 * sizeOf<Float>(), pPos, GL_STATIC_DRAW);     
        
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeOf<Float>(), 0);
        glEnableVertexAttribArray(0);

        String vs =
"""
#version 330 core

layout(location = 0) in vec4 position;

void main()
{
    gl_Position = position;
}
""";

        String fs =
"""
#version 330 core

layout(location = 0) out vec4 color;

void main()
{
    color = vec4(1.0, 0.0, 0.0, 1.0);
}
""";

        shader = _createShader(vs, fs);
    }

    @override
    void onUpdate() {
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }

These are the functions to compile the shaders:

int _compileShader(String source, int type) {
    int id = glCreateShader(type);
    
    Pointer<Pointer<Utf8>> ptr = allocate<Pointer<Utf8>>();
    ptr.elementAt(0).value = NativeString.fromString(source).cast();

    glShaderSource(id, 1, ptr, nullptr);
        glCompileShader(id);
    
    Pointer<Int32> result = allocate<Int32>();
    glGetShaderiv(id, GL_COMPILE_STATUS, result);
    
    if(result.value == GL_FALSE) {
        Pointer<Int32> length = allocate<Int32>();
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, length);

        Pointer<Utf8> message = allocate<Utf8>(count: length.value);
        glGetShaderInfoLog(id, length.value, length, message);
        glDeleteShader(id);

        print("Failed to compile ${type == GL_VERTEX_SHADER ? "vertex" : "fragment"} shader");
        print(message.ref);
        return 0;
    }

    return id;
}

int _createShader(String vertexShader, String fragmentShader) {
    int program = glCreateProgram();
        int vs = _compileShader(vertexShader, GL_VERTEX_SHADER);
    int fs = _compileShader(fragmentShader, GL_FRAGMENT_SHADER);

    glAttachShader(program, vs);
        glAttachShader(program, fs);
        glLinkProgram(program);
        glValidateProgram(program);

    glDeleteShader(vs);
        glDeleteShader(fs);
    
    return program;
}

Since this exact code works perfectly in C++ the only thing I can think is that I've messed un some poiner witht ffi. Can you find any mistake I don't see?

EDIT: One could think that OpenGL cannot interact with glfw but glClear does work

This is the github repository if you need to investigate more code

  • @Rabbid76 It was a test: since the function can gen multiple buffers at once even if in c++ i can just provide one buffer I've thought to this. Obviously it didn't work in either of cases – Giovanni Kalone Sep 14 '20 at 16:42
  • *"even if in c++ i can just provide one buffer [...]"* - no, you' wrong: `GLuint buffers[2];` `glGenBuffers(2, buffers);`. You can do something similar in _dart_. – Rabbid76 Sep 14 '20 at 16:47
  • @Rabbid76 Yes, the allocate function works just like new in c++ and i can specify the amount like in new[].I've tried also this but it does not work, the buffer value is assigned after glGenBuffers so that is surelly not the problem – Giovanni Kalone Sep 14 '20 at 16:51
  • @Rabbid76 The OpenGL Context is created with `glfwMakeContextCurrent` from GLFW and then i call `initOpenGL()` with no errors – Giovanni Kalone Sep 14 '20 at 16:53
  • Do you get any OpenGL error ([`glGetError`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetError.xhtml))? Have you tried to get [Debug Output](https://www.khronos.org/opengl/wiki/Debug_Output)? – Rabbid76 Sep 14 '20 at 17:01
  • @Rabbid76 I've placed a glGetError after each gl-function of the project but none of them returns an error – Giovanni Kalone Sep 14 '20 at 17:06
  • @Rabbid76 I've linked the github repository if needed – Giovanni Kalone Sep 14 '20 at 17:18

1 Answers1

0

I've been doing something very similar and have the same problem. I thought I'd tracked it down to the call to glDrawArrays which crashed with a blue screen of death on Windows and a thread stuck in device driver error message. However, with further investigation I believe it is caused by the attribute array:

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeOf<Float>(), 0);
glEnableVertexAttribArray(0);

Commenting out those lines lets it run without crashing but no triangle appears. Uncommenting results in the BSOD.

I also found an interesting post from 2013 which appears to be relevant: Link

That's as far as I know at the moment.

iBob101
  • 1,460
  • 3
  • 14
  • 20