0

i currently work with some shader codes, but some of them makes me confused. it used incoming gl_vertex to calculate out eyevector,then refelect vector.finally pass to frag shader. in pass of frag shader, extracting a texl through textureCube. my question is is there getting only an pixel per gl_Vertex? where is the interpolation happened to those shaders?

 vertex shader:
    uniform vec4 eyepos;  
    varying vec3 reflectvec;
    void main(void)  { 

    vec4 pos = normalize(gl_ModelViewMatrix * gl_Vertex);     
    pos = pos / pos.w;          
    vec3 eyevec = normalize(eyepos.xyz - pos.xyz);     
    vec3 norm = normalize(gl_NormalMatrix * gl_Normal);          
    reflectvec = reflect(-eyevec, norm);          
    gl_Position = ftransform(); 

    }

    frag shader:
    uniform samplerCube cubemap;  
    varying vec3 reflectvec; 

    void main(void)  {     
    vec4 texcolor = textureCube(cubemap, reflectvec);      
    gl_FragColor = texcolor;  
    }
1
  • Where are your #version directives? Commented Mar 11, 2013 at 0:05

1 Answer 1

1

where is the interpolation happened to those shaders?

The fragment shader is executed for every fragment. A pixel consists of at least 1 fragment. Between the vertex and the fragment shader, the input varyings toward the fragment shader are barycentrically interpolated.

Sign up to request clarification or add additional context in comments.

4 Comments

hi, @datenwolf thanks. it seeme that i understand something wrong. isn't it that vertex shader will be executed on every vertex, and calculating out varying vec3 reflectvec as initial value. in pass of rasterization, GPU does interplolation using the initial value , in other words, using my varying variable in vertex shader, then pass interpolated value to fragment shader every fragment generated? am i right?
Yes, the vertex shader generates reflectvec for each vertex. The fragment stage then interpolates reflectvec and the other varyings between vertices before passing them per-fragment into the fragment shader.
thx @datenwolf , could you please tell me how does it interpolated between two varyings ,and even more.
@haohaochao: The values are interpolated barycentrically (Google for barycentric interpolation and you'll find a lot of information). The process of turning triangle geometry into fragments and from there pixels is called rasterization (Google scanline rasterizer and you'll get really much information). The interpolation is done by doing the barycentric calculation for the position of each fragment in relation to the viewport coordinates of the transformed vertices. The exact details how it's written in code depends on the OpenGL implementation (i.e. driver and GPU) and may be trade secrets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.