brainfreak
brainfreak

Reputation: 137

OpenGL ES 2.0: Shader Linking Fail

I am trying to make a simple android program that uses openGL. So far, the shader linking fails and the Log info is,

LOG

Link info
    ---------
    OpenGL ES programs must have both a vertex and a fragment shader or a compute shader.  

The shader compilation works fine. Here is the shader code:

Vertex Shader

attribute vec4 a_Position;

void main() {
    gl_Position = a_Position;
    gl_PointSize = 10.0;
}  

Fragment Shader
precision mediump float;

uniform vec4 u_Color;

void main()
{
    gl_FragColor = u_Color;
}  

The steps that i am taking are,

final int programObjectId = glCreateProgram();

if(programObjectId == 0)
   return 0;

glAttachShader(programObjectId, vertexShaderId);
glAttachShader(programObjectId, fragmentShaderId);
glLinkProgram(programObjectId);

Compilation Code

public static int compileShader(int type, String code) {

    final int shaderObjectId = glCreateShader(type);

    glShaderSource(shaderObjectId, code);
    glCompileShader(shaderObjectId);
    int compilationStatus[] = new int[1];
    glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compilationStatus, 0);

    if(compilationStatus[0] == 0)
        glDeleteShader(shaderObjectId);
    return shaderObjectId & compilationStatus[0];
}

I am not sure how to solve this problem.

Upvotes: 1

Views: 869

Answers (1)

BDL
BDL

Reputation: 22157

The problem lies in the last line of compileShader: Here you perform a bitwise and operation on the shaderObjectId (an arbitrary number > 0) and compilationStatus (GL_TRUE or GL_FALSE). Since GL_TRUE = 1 and GL_FALSE = 0, the result of this operation can only be 0 or 1.

Lets just have a look at the GL_TRUE case, since with GL_FALSE the result will be 0 anyway.

For every even number x:
x & 1 == 1
For every odd number x:
x & 1 == 0

This means, if shaderObjectId = 3 (for example), the function will return 1.

Upvotes: 1

Related Questions