Jump to content
  • Advertisement
Sign in to follow this  
Gorgi

Simple Shader question

This topic is 4409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there , I just started with GLSL and I'm trying to write a very simple fragment shader , which copies the contents of one texture into another(which is attached to the framebuffer). Here's the shader code: uniform sampler2D sampler1; varying vec2 Texcoord; varying vec3 PixelData; void main(){ vec3 PixelData = texture2D(sampler1,TexCoord).rgb; gl_FragColor = vec4(PixelData, 1.0); } However when I try to run my application the following Infolog appears: InfoLog: (1) : error C1008: undefined variable "TexCoord" (1) : error C1102: incompatible type for parameter #2 ("c") (1) : error C1008: undefined variable "TexCoord" (1) : error C1102: incompatible type for parameter #2 ("c") (1) : error C1008: undefined variable "TexCoord" (1) : error C1102: incompatible type for parameter #2 ("c") From all the shader code examples I've read so far I cannot se something wrong with my code. Am I missing something? Thanx in advance :)

Share this post


Link to post
Share on other sites
Advertisement
I guess this is just your fragment shader right? You need to assign some values to TexCoord inside a vertex shader, else the shader doesn't know what values they would be. Also, PixelData doesn't have to be varying, you can just remove that line, as it is declared inside the function.

Also, note the Texcoord and TexCoord discrepancy, which is probably causing the actual error. The variable names are case-sensitive.

Share this post


Link to post
Share on other sites
Well don't know what to say, YES it was the case sensitivity, I probably should have noticed it myself :)
Apart from this , I would really like not to get involved with a vertex shader , is there another way to assign values to TexCoord? Cannot these values be assigned within the fragment shader itself? Or would it be better to use directly gl_TexCoord[0].st or gl_MultiTexCoord0.st ?

Share this post


Link to post
Share on other sites
Unfortunately you probably will have to use a vertex shader, because you can't use gl_MultiTexCoord in a fragment shader and gl_TexCoord is empty if you don't write some data into it.

So you should have a shader like this:

//*************************************
//vertex shader
//*************************************
void main(){
gl_TexCoord[0]= gl_MultiTexCoord0; //store the gl_MultiTexCoord data for later
gl_Position=ftransform(); //does the common vertices transformation
}

//*************************************
//and the fragment shader as follows:
//*************************************
uniform sampler2D sampler1;

void main(){
vec3 PixelData = texture2D(sampler1,gl_TexCoord[0]).rgb;
gl_FragColor = vec4(PixelData, 1.0);
}


But you could shorten your fragment shader to one line without saving the PixelData in an extra variable:
gl_FragColor=vec4(texture2D(sampler1,gl_TexCoord[0]));

Hope it helps..

Share this post


Link to post
Share on other sites
Quote:
Original post by Caste
Unfortunately you probably will have to use a vertex shader, because you can't use gl_MultiTexCoord in a fragment shader and gl_TexCoord is empty if you don't write some data into it.
...
I think that's only true if you are using a vertex shader. From the GLSL Specs...
Quote:
From section 7.6 in the GLSL Specs
The gl_TexCoord[] values are the interpolated gl_TexCoord[] values from a vertex shader or the texture coordinates of any fixed pipeline based vertex functionality.
I may be interpretting that wrong but I take it to mean that if you're using a vertex shader, the gl_TexCoord[] values will be interpolated based on what you set in the vertex shader or be undefined if you don't set them. But if you aren't using a vertex shader they will be set by the usual fixed function glTexCoord*/glMultiTexCoord* functions or automatic texture generation.

So I think you should be able to just use gl_TexCoord[0] in your fragment shader without needing a vertex shader.

Share this post


Link to post
Share on other sites
I'm not exactly sure about that TexCoord stuff without using a vertex shader. However, I urge you to write a simple vertex shader anyway, because I (and others as well) have experienced severe slowdown when we didn't have a fragment shader (I believe on ATI only, not certain though). This situation was the reverse to yours, but I can't imagine there to be much different from the hardwares point of view.

Share this post


Link to post
Share on other sites
Rick , Caste and Kalindor thanx for your replies. I tried out what both of you suggested and it seems that Kalindor is right because the result remains the same , whether I use a vertex shader and a variable TexCoord or just use TexCoord[0].st within the fragment shader. However my shader does not seem to work either way:(
What I am trying to do is very simple: I am trying to copy the contents of a W*H Texture into the color buffer(also W*H).
The vertex shader:

varying vec2 TexCoord;

void main(){
TexCoord= gl_MultiTexCoord0.st;
gl_Position=ftransform();
}



The fragment shader is :

uniform sampler2D sampler1;
varying vec2 TexCoord;

void main(){
vec3 PixelData = texture2D(sampler1,TexCoord).rgb;
gl_FragColor = vec4(PixelData, 1.0);
}


I succesfully (at least there are no syntax errors...) Compile , Create , Link , Use Shader Object/Program.

As a test I do glReadPixels in the whole Colorbuffer , and I do not get the expected values. Am I missing something? When does the actual execution of the fragment shader take place? Are any extra commands necessary?

Share this post


Link to post
Share on other sites
I should have put this in my last post but I too recommend using both a vertex and fragment shader. I didn't know about the problem rick_appleton posted about but that's just one more reason. My main motivation for that is because I simply dislike the fixed-function pipeline [grin]. I feel it's too bloated and overly confusing whereas shaders let you do only what you want and are much more flexible. I say forget about the fixed-function pipeline completely. The programmable pipeline is the way of the future, and indeed in D3D10 the fixed-function API is no more (it's been gone from hardware for a while now).

EDIT: I just saw your last post. I have to get lunch now but if it's not answered by the time I'm done I'll try to get back to it.

Share this post


Link to post
Share on other sites
Okay, sorry about the wait. Your shaders look fine to me.

I'm not sure exactly what it is you're doing. Do you mean that you are just rendering a full-screen quad to the framebuffer? If that's what you're doing why do you need to use glReadPixels to test, is your window not visible? If that's the case then the framebuffer's data is undefined because of the pixel ownership test (refer to section 4.1.1 of the OpenGL 2.0 Spec). The way to get around that is to render to a proper off-screen buffer, such as an FBO, and read the data from that.

If that's not the problem could you post more about what exactly you're doing, and maybe post some code as well?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!