-1

I am following "Interactive Computer Graphics" by Ed Angel, specifically the code for rotating cube with texture. The vertex shader for this (as in the book) is as follows:

#version 150
in vec4 vPosition;
in vec4 vColor;
in vec2 vTexCoord;

out vec4 color;
out vec2 texCoord;
uniform vec3 theta;
void main()
{
........
........
// code for rotation
........
color = vColor;
texCoord = vTexCoord;
gl_Position = rz * ry * rx * vPosition;
}

And the fragment shader code is:

#version 150
in vec4 color;
in vec2 texCoord;
out vec4 fColor;
uniform sampler2D texture;
void main()
{
fColor = color * texture2D( texture, texCoord );
}

[The link for the complete code is here. Just look at the code for example 8.

I am trying to implement this using #version 440 core. When I run this, I get only a black cube. The texture is not shown.

What change to the above code should I make to display the texture correctly?

BDL
  • 21,052
  • 22
  • 49
  • 55
Majis
  • 203
  • 1
  • 2
  • 6
  • Change texture2D to texture (but that is a minor thing). Acutally, if the shaders are not compatible with the version, you would get a compiler error. – BDL Nov 15 '17 at 10:28
  • @BDL I have tried with that as well. But the result is the same. – Majis Nov 15 '17 at 10:29
  • Try with 'fColor = vec4(1.0);' If you get a white cube this way, the problem is probably with the way your texture is created/uploaded/bound. – bernie Nov 15 '17 at 18:46
  • @bernie I get the white cube as you have mentioned. Moreover, if I use `fcolor=color` I still get a colored cube. However, if I use `fColor = color * texture(checker_texture, texCoord)`, I get a black cube. I have copied the the texture part from the links I have given. Can you please check whether anything is missing in that code? – Majis Nov 16 '17 at 10:57
  • That confirms the problem is with the texture setup. I don't have time to crawl through a source tree like that (and you should not expect such help). Add the relevant parts (and only those) to your question to get help. – bernie Nov 16 '17 at 13:41

1 Answers1

0

This line in the cpp code:

glutInitContextVersion( 3, 2 );

is your problem. glsl version 440 requires an OpenGL 4.4 context. Change it to :

glutInitContextVersion( 4, 4 );

And modernise your shader code according to:

GLSL Specification

And it should work fine.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Ian Young
  • 1,712
  • 1
  • 16
  • 33
  • I have already done that in my code but the result is the same. If the code for the texture loading is the same for glsl version `440`, I think, I have coded it correctly. With the shader code, please see my response to @bernie. – Majis Nov 16 '17 at 11:03
  • Which dev tools are you using (and which gpu do you have)? – Ian Young Nov 17 '17 at 12:56
  • And in line with the other responses, post your texture loading, set up and usage code – Ian Young Nov 17 '17 at 12:58