#OpenGL Kotlin reading shaders from txt file

3 messages · Page 1 of 1 (latest)

heady stratus
#

I'm trying to use txt files instead of just having the shader code in the code :)
But I get some odd errors I can't find much about:
glerror 1281 and glerror 1282

This is my shader code:

fragmentShader.txt

"""
    precision mediump float;    //we don't need high precision floats for fragments
    uniform vec4 color;         // a constant color to apply to all pixels
    void main() {               // The entry point for our fragment shader.
        gl_FragColor = color;   // Pass the color directly through the pipeline.
    }
"""

vertexShader.txt

"""
    uniform mat4 modelViewProjection;  // A constant representing the combined model/view/projection matrix.
    attribute vec4 position;  // Per-vertex position information that we will pass in.
    void main() {             // The entry point for our vertex shader.
        gl_Position = modelViewProjection  // gl_Position is a special variable used to store the final position.
            * position;       // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
        gl_PointSize = 8.0; //pixel width of points
    }
"""

Heres how I try to load it:

GLManager.kt


    private const val vertexShaderPath = "shaders/vertexShader.txt"

    private const val fragmentShaderPath = "shaders/fragmentShader.txt"

    fun buildProgram() {
        val vertex = compileShader(GLES20.GL_VERTEX_SHADER, vertexShaderPath)
        val fragment = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderPath)
        glProgramHandle = linkShaders(vertex, fragment)

        GLES20.glDeleteShader(vertex)
        GLES20.glDeleteShader(fragment)

        positionAttributeHandle = GLES20.glGetAttribLocation(glProgramHandle, "position")
        colorUniformHandle = GLES20.glGetUniformLocation(glProgramHandle, "color")
        MVPMatrixHandle = GLES20.glGetUniformLocation(glProgramHandle, "modelViewProjection");

        GLES20.glUseProgram(glProgramHandle)
        GLES20.glLineWidth(30f)
        checkGLError("buildProgram")
    }
    private fun compileShader(type: Int, shaderPath: String): Int {
        assert(type == GLES20.GL_VERTEX_SHADER || type == GLES20.GL_FRAGMENT_SHADER)

        val shaderCode : String = engine.getAssets().open(shaderPath).bufferedReader().use {
            it.readText()
        }
        Log.d(TAG, "code: $shaderCode")

        val handle = GLES20.glCreateShader(type)
        GLES20.glShaderSource(handle, shaderCode)
        GLES20.glCompileShader(handle)
        Log.d(TAG, "Shader Compile Log: ${GLES20.glGetShaderInfoLog(handle)}")
        checkGLError("compileShader")

        return handle
    }
heady stratus
#

Shader code in txt shouldn't have """
Now we know

rose helm