Search code examples
opengl-esglslshaderfragment-shaderlighting

3D object is colored in a way that looks like a 2D object


I'm using the following shader sources for vertex and fragment.

Vertex shader source:

#define highp
attribute highp vec3 position;
uniform highp mat4 mvp;
void main(void)
{
    gl_Position = mvp * vec4(position, 1.0);
}

Fragment shader source:

#define highp
uniform highp vec3 color;
void main(void)
{
    gl_FragColor = vec4(color, 1.0);
}

However, the shader is not working. As shown in the following screen-shot, a 3D object is just colored like a 2D object:

3D object is colored like a 2D object


glGetShaderiv with GL_COMPILE_STATUS returns success == TRUE, so there is no shader compile error.

glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);

I didn't try glGetError() in the code. I'm going to try it. But I suspect I'm not receiving any OpenGL errors.


I believe I need to adjust the color in vertex and fragment shader. How should I adjust the color in shader sources? Can anyone help me by giving a hint. So far, I couldn't resolve the issue by modifying the sources.


UPDATE

With the help of @Rabbid76 now the 3D object looks good:

3D object looks good now :)

I'm using @Rabbid76 code with a small modification: adding the #version 130 to the top of both vertex and fragment shader sources. Looks like my Intel graphics card requires the #version 130 directive, otherwise it throws some warnings and errors:

warning: extension "GL_OES_standard_derivative" unsupported in fragment shader

The #version 130 directive resolves the above warning and its subsequent errors.


Solution

  • Your shader is working correctly. It looks like it's 2D because you don't have any lighting in your shader. An object that is a uniform color will look 2D because there are no depth cues such as self shadowing, or specular highlights, etc.