Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

opengl - Shader Texture Always Facing The Camera

I have this basic shader, though I ran into some trouble which really bugs me!

I'm applying the texture using the fragment shader though however I move or rotate the camera, the texture on the face will always be facing the camera (I've added a GIF image as an example).

gif

So as you can see on the image above the texture keeps facing the camera, also if I move away of closer it will keep its scale.

I'm trying to achive that I can move around and the texture will keep it's position, rotation and scale according to the face and vertices, and not according to the camera as it is doing now.

Here is the desired result, though using the deprecated methods glVertex2f and the glTexCoord2f which is the same result I want though through shaders, I guess my problem is calculating the correct UV coordinates on the texture.

desired result
(source: gyazo.com)

Important I want to achive this using shaders so I can make/calculate changes into the colors of the actual texture. (For instance make it a black and white).

Here is my current Vertex & Fragment Shader Code.

Vertex Shader Code

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment Shader Code

uniform vec2 size;
uniform sampler2D texture;

void main() {
    gl_FragColor = texture2D(texture, gl_FragCoord.xy / size.xy);
}

The uniform vec2 size; within the fragment shader, is the width and height of the face itself where the texture is applied on. The uniform sampler2D texture; is of course the grass texture itself.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It does that because that is whay you've programmed. You derive the texcoords directly from the coordinates of the fragment. If you want the texture to "stick to the geometry", you need to define a mapping between your geometry and texture space. Usually, this is done through a 2D texture coordinate attribute per vertex - just as the fixed function pipeline did with the glTexCoord2f() you mentioned.

In your specific case, (assuming the host program still specifies texcoords the same deprectaed way you are using for vertex positions with gl_Vertex), you could let the GL interpolate the texcoords per fragment and use these to access the texture in the fragment shader:

Vertex Shader Code

varying vec2 vTexCoord;

void main() {
    vTexCoord = gl_MultiTexCoord0.st;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment Shader Code

uniform sampler2D texture;
varying vec2 vTexCoord;

void main() {
    gl_FragColor = texture2D(texture, vTexCoord);
}

I would suggest to switch to generic vertex attributen, and more modern GLSL versions, though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...