Kotlin OpenGL GLSL using GLEW and GLFW

Submitted by Dickens A S on Mon, 11/11/2019 - 19:08

Kickstart Kotlin OpenGL GLSL using GLEW and GLFW using below github code

C code vs kotlin code analysis

OpenGL C Code

Kotlin Code

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

 

val vao = memScoped {
    val output = alloc<UIntVar>()
    glGenVertexArrays!!(1, output.ptr)
    output.value
}
glBindVertexArray!!(vao)
GLuint vbo[1];
glGenBuffers(1, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
val vbo = memScoped {
    val output = alloc<UIntVar>()
    glGenBuffers!!(1, output.ptr)
    output.value
}
glBindBuffer!!(GL_ARRAY_BUFFER.toUInt(), vbo)
static const GLfloat vertex_array[] = {
   -0.8f, -0.8f, 0.0f,
    0.8f, -0.8f, 0.0f,
    0.0f,  0.8f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertex_array, GL_STATIC_DRAW);

 

val vertex_array = floatArrayOf(
    -0.8f, -0.8f, 0.0f,
     0.8f, -0.8f, 0.0f,
     0.0f,  0.8f, 0.0f
)
    
vertex_array.usePinned {
     glBufferData!!(GL_ARRAY_BUFFER.toUInt(), 
                           vertex_array.size.toLong() * 4, it.addressOf(0), 
                           GL_STATIC_DRAW.toUInt())
}

 

static const char* vertex_shader =
"#version 410 core\n"
"layout(location = 0) in vec3 vertexPosition_modelspace;\n"
"void main() {\n"
"    gl_Position.xyz = vertexPosition_modelspace;\n"
"    gl_Position.w = 1.0;\n"
"}\n";

 

const val vertex_shader = """
#version 410 core
layout(location = 0) in vec3 vertexPosition_modelspace;
void main() {
    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;
}
"""

 

static const char* fragment_shader =
"#version 410 core\n"
"out vec3 color;\n"
"void main(){\n"
"  color = vec3(1,0,0);\n"
"}\n";

 

const val fragment_shader = """
#version 410 core
out vec3 color;
void main(){
  color = vec3(1,0,0);
}
"""

 

unsigned int vsId;
vsId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vsId, 1, &vertex_shader, NULL);
glCompileShader(vsId);


unsigned int fsId;
fsId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fsId, 1, &fragment_shader, NULL);
glCompileShader(fsId);

unsigned int pId;
pId = glCreateProgram();
glAttachShader(pId, vsId);
glAttachShader(pId, fsId);
glLinkProgram(pId);

glDetachShader(pId, vsId)
glDetachShader(pId, fsId)

glDeleteShader(vsId)
glDeleteShader(fsId)

 

val pId = memScoped {
        val vsId = glCreateShader!!(GL_VERTEX_SHADER.toUInt())
        glShaderSource!!(vsId.toUInt(), 1, 
                                  arrayOf(vertex_shader).toCStringArray(memScope), null)
        glCompileShader!!(vsId.toUInt())
        
        val fsId = glCreateShader!!(GL_FRAGMENT_SHADER.toUInt())
        glShaderSource!!(fsId.toUInt(), 1, 
                                  arrayOf(fragment_shader).toCStringArray(memScope), null)
        glCompileShader!!(fsId.toUInt())

        val pId = glCreateProgram!!()
        
        glAttachShader!!(pId.toUInt(), vsId.toUInt())
        glAttachShader!!(pId.toUInt(), fsId.toUInt())
        glLinkProgram!!(pId)

        glDetachShader!!(pId.toUInt(), vsId.toUInt())
        glDetachShader!!(pId.toUInt(), fsId.toUInt())

        glDeleteShader!!(vsId.toUInt())
        glDeleteShader!!(fsId.toUInt())

        pId
    }

 

The methods are called with !! for kotlin compiler null safe check
memScoped gives a C pointer with allocation to satisfy the parameters of the GLEW functions

Output 640x480 window with GLEW "3" version printed on the title

OpenGL 3 Triangle

 

Download MSYS2

This is required for windows to download pre-compiled libraries and its headers

pacman -S mingw-w64-x86_64-glew mingw-w64-x86_64-glfw

Source Code

End of Article

Add new comment