PDA

View Full Version : shaders, simple to well a bit more than simple :)



Andy Gilbert
04-21-2010, 02:13 PM
Ok, ive decide if im going to stick with opengl then i need to learn shaders. So tonight i followed these tutorials as they came up on google and seemed easy enough.

http://www.swiftless.com/glsltuts.html

But. im now like stuck, as ive done them and understand the use of them more but still cant like go ahead and start writnig my own, or using otheres quite yet. for instance, from them tuts i now know how to do per pixel lighting and texture a object using shaders, but not both together? Is there another site with good GLSL tutorials on?

Some questions i do have though are...

When you decide to use shaders what like commands do they actually take over and make redundant using openGL? For instance, can i use shaders to texture an object, but then still scale the texturing inside opengl? Or would you not even want to?

And other things like... If i say wanted, to have an object, textured with normal maps and per pixel lighting, is that all done in one shader or can it be done with like 2 or 3 shaders?

Thanks
Andy

V-man
04-22-2010, 06:31 AM
You can answer that yourself if you know when scaling happens. Scaling of the texcoords happen at the vertex stage and we know that the vertex shader takes over the vertex stage. So, you need to upload your own texture matrix to your shader.

attribute vec2 MyTexCoordIn;
uniform mat4 MyTexMatrix;
varying vec2 MyTexCoordOut;

MyTexCoordOut = vec2(MyTexMatrix * vec4(MyTexCoordIn, 0.0, 1.0));


And other things like... If i say wanted, to have an object, textured with normal maps and per pixel lighting, is that all done in one shader or can it be done with like 2 or 3 shaders?

This is like asking if a program can have a File menu and also a toolbar and some status bar. Well, I guess it depends if you can program or not. Maybe you should consider buying a book.

Dark Photon
04-22-2010, 07:05 AM
Ofor instance, from them tuts i now know how to do per pixel lighting and texture a object using shaders, but not both together?

Perhaps it would be helpful if you explain what your mental hang-up is? If you can do X, and you can do Y. Why couldn't you do X & Y sequentially in the same shader?

That's exactly what you do.


When you decide to use shaders what like commands do they actually take over and make redundant using openGL? For instance, can i use shaders to texture an object, but then still scale the texturing inside opengl? Or would you not even want to?

When you pop in a vertex shader you replace:
* vertex position transformation using the modelview and projection matrices,
* normal transformation/normalization,
* Texture coordinate generation and transformation.
* Per-vertex lighting

When you pop in a fragment shader you replace:
* Texture application,
* Fog application,
* Color sum (if separate specular enabled).


For instance, can i use shaders to texture an object, but then still scale the texturing inside opengl? Or would you not even want to?
You have to be careful with combinations. Often its easier to just do things 0% shaders or 100% shaders. Not try to use fixed-function (built-in) vertex shader with user-defined fragment shader (or vice versa).

For your example though, it'd probably work, as you'd pop in a fragment shader to do fragment/pixel texturing, but the scale (texgen) happens in the vertex shader.


And other things like... If i say wanted, to have an object, textured with normal maps and per pixel lighting, is that all done in one shader or can it be done with like 2 or 3 shaders?
You could do it all with one shader program -- sure.

However, I'd suggest you walk before you run. Try simple things. Implement some basic vertex lighting (in the vertex shader), just like OpenGL does with built-in functionality. Then move that to the fragment shader. You'll learn a lot just by doing that. The Orange Book (http://www.3dshaders.com/) will be useful here. There are also GLSL tutorials out there that show you how to do this. Try: this (http://www.lighthouse3d.com/opengl/glsl/index.php?lights[/url).