5
votes

I have got problem with my shaders. I am trying to put textures and phong shading in my game using glsl but I can't get any good effect.

I've been searching google for a long time, and I can't find any info how connect ligh and texture together, so I've decided to wrote and ask here.

This is my game without texture: without texture

and this is with texture:

with texture

What I want to achive is to make this pinkish texture make better visible with spot on a center - just like without texture, and also repair those per vertex shading on gutters - make it per pixel shading, I don't know what is wrong now.

I have checked about 10 shaders with phong shading and I have got also per vertex not per pixel shading.

This is my fragment vertex code mayby someone can see there something?

    varying vec3 N;
    varying vec3 v; 
    uniform sampler2D myTexture;

    varying vec2 vTexCoord;    

    void main (void)  
    {  
       vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);

vec3 L = normalize(gl_LightSource[0].position.xyz - v);   
           vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)  
           vec3 R = normalize(-reflect(L,N));  

           //calculate Ambient Term:  
           vec4 Iamb = gl_FrontLightProduct[0].ambient;    

           //calculate Diffuse Term:  
           vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);    

           // calculate Specular Term:
           vec4 Ispec = gl_FrontLightProduct[0].specular 
                        * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);

            finalColor+=Iamb + Idiff + Ispec;
       // write Total Color:
       gl_FragColor = gl_FrontLightModelProduct.sceneColor + (texture2D(myTexture, vTexCoord)) + finalColor;
    }

and my vertex shader

varying vec3 N;
varying vec3 v;
varying vec2 vTexCoord;

void main(void)
{

   v = vec3(gl_ModelViewMatrix * gl_Vertex);       
   N = normalize(gl_NormalMatrix * gl_Normal);
   vTexCoord = vec2(gl_MultiTexCoord0);

   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;


}

I'll be glad for help.

EDIT:

this is how I put my texture into shader. It works fine ( I think ) because I can edit texture values in shader - in some way.

int my_sampler_uniform_location = glGetUniformLocation(brickProg, "myTexture");

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glUniform1i(my_sampler_uniform_location,0);
CUTIL::drawBox();
glBindTexture(GL_TEXTURE_2D,0);

EDIT 2:

After Nicol Bolas suggestion about colors add I have edited my shader and change it like this:

gl_FragColor = (texture2D(myTexture, vTexCoord)) + finalColor;

Now it is only to bright, but now I have to chenge a little light on stage and would be great. But still I haven't got per pixel shading instead I have got per vertex shading. This is my current screen and I have marked on example what I mean talking about shading:

enter image description here

1
Can you show code where you set up and pass to GL texture coordinates?Mārtiņš Možeiko
@MārtiņšMožeiko edited and addedsebap123
What happens if you just render the texture without lighting at all?Nicol Bolas
@NicolBolas I have got normall texture - in good colorsebap123
@sebap123 you were supposed to do finalColor+=Iamb + ((texture2D(myTexture, vTexCoord)) * Idiff) + Ispec; instead of adding, you are supposed to multiply.PeterT

1 Answers

12
votes

This equation:

gl_FragColor = gl_FrontLightModelProduct.sceneColor + (texture2D(myTexture, vTexCoord)) + finalColor;

Does not make any sense for most textures. You are taking the color produced from the lighting equation and adding it to the color sampled from the texture. This would only make sense if the values stored in the texture represented light emitting properties of the surface.

Generally, color values of a texture represent the diffuse reflectance of a surface. Which means you need to incorporate them into the lighting equation directly. The texture's color should either fully replace the diffuse color from the material or it should be combined with the material diffuse color in some way.