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