designing my first shader

Started by
5 comments, last by Atrix256 13 years, 11 months ago
I recently bought the Orange Book. I know OpenGL 2 fairly well. I am moving into the programmable pipeline and various OpenGL 3 techniques. So, for my first attempt at a shader, I am looking to apply six unique textures to all faces on a cube. My understanding is that this would require a fragment shader since that is where texture logic generally happens. The reason I'm looking into a shader is that the various vertices would have to carry multiple texture coordinates and point to differing textures. Is a shader an appropriate solution to this? Can you offer me any further guidance? Am I correct in targeting a fragment shader? All I need is general guidance. I am preparing to do all the necessary research myself. Point me in the right direction!
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Advertisement
You don't necessarily need a shader to put a texture on each face of a cube, but yeah you should learn shaders. Fixed function pipeline (ie not shaders) is going the way of the dodo, and shaders are way more fun anyways. (:

I'm confused about what you are trying to accomplish though.

Do you want one texture per face on the cube? Or do you want multiple textures on each face of the cube? I ask because you talk about a unique texture per face, but then talk about multiple texture coordinates, which is kind of confusing.

Anyhow, where you ask "Am i correct in targeting a fragment shader?", fragment (aka pixel) shaders and vertex shaders generally come in pairs.

Check out how vertex and fragment shaders work in conjunction with eachother:

#1 - You send vertices to your GPU when rendering.
#2 - For each vertex, it runs your vertex shader.
#3 - for each pixel on each triangle, it runs your pixel shader. But, For all the values that you output from your vertex shader, it interpolates those values across the triangle for your pixel shader.

Think about what this means for a second.

You have ALOT of pixels to deal with, but alot less vertices to deal with right?

If you could do math per vertex instead of per pixel, you'd be doing a lot less math right?

So for instance, lets say you had LINEAR FOG in your world (ie fog that makes things look foggier linearly based on their distance in the fog).

What you could do is calculate that fog amount per vertex and send that out as output of the vertex shader.

The GPU interpolates your fog amount across each triangle based on the values you spit out from your vertex shader, and your pixel shader gets the interpolated fog value as an input parameter.

This is WAY more efficient than calculating your fog amount in your pixel shader.

But, this doesn't work for everything. Some things you need to calculate can't be interpolated linearly (such as exponential fog or specular lighting) and these things you unfortunately do have to do in the fragment shader.

Basically, if there's anything you can move out of your pixel shader into your vertex shader, you should do so because it will optimize your rendering.

I started learning shaders about a year ago and it really is hard to find good info on how the basics work so i know what you are going through (;

Hope this helps!
Thank you for the guidance! I will give more detail.

I want each face of the cube to have a distinct texture image. For instance, I have aaa.png, bbb.png, ccc.png, ddd.png, eee.png, and fff.png; they are each 128x128 images. I want each one to take each face. I have no intention (at this point) of applying multiple textures to the same face.

Does that help?

My issue, given my current pre-shader knowledge, is that the cube consists of 8 points. Each of those points has vertex data and texture coordinate data, but I need a different texture on each face. A crude solution would be to render 6 distinct quads (each with 4 distinct vertices), but that would lead to data duplication (24 vertices that occupy only 8 points in space). I figured learning a shader would be a more elegant step forward rather than just barraging my problem with old/bad solutions.
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Just wanted to say that you don't really need a shader for what you're doing because all you really want to draw is 6 quads with different textures on them. It would probably not be wise to try to use 6 pairs of texture coordinates for this because each pixel only really needs one texture, and you don't want to be sampling 6 textures for every pixel.

Certainly don't let that stop you from learning GLSL though!

The Orange Book is a great reference and you should read as much of it as you can. Also I like this site for GLSL introductory tutorials.

EDIT in response to your latest post:
You mostly have the right idea there, although probably the best thing to do would be to either combine 6 textures in one or to duplicate the vertices. Like I said if you're supplying six sets of texcoords you need some way to determine which texture you want to use for a given side.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Well, I'll be honest and say that I am forming some pretty complex geometry (mathematically), and I need to be able to have textures on certain faces but different textures on other faces (that happen to share vertices with neighboring faces). The model is loaded into a VBO.
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Instead of having 6 different texture coordinates, what i would recommend is having just a single set of texture coordinates that all the images used.

The issue then just becomes figuring out which image to use for each side of the cube.

I'm not a shader expert at all, but i can see 2 paths i personally would follow (hopefully someone else will speak up if there is a better way).

#1 - instead of rendering the cube all at once, render each face by itself, changing the texture of the shader each time.

#2 - use the polygon normal to select which of the 6 textures to use. IE if you are on the face of the cube that points down the positive X axis, use texture #1. If you are on the face that points down negative Z axis, use texture #2 etc.

Whether you go #1 or #2, using a single set of texture coordinates should be fine. You don't need to change the texture coordinates, you just want to change what texture it uses after all (:

And you know this sounds silly, but there is something called "cube mapping". I have a hunch this might actually help in what you are trying to do, even though it's usually used for other things.
For your more complex shapes, what are the rules for what texture needs to be shown on what face?

Does have have a unique texture or are there rules about what is used where?

This topic is closed to new replies.

Advertisement