PDA

View Full Version : OpenGL translation of a software pixel pipeline



kekko
03-19-2011, 07:59 AM
Hi all,
I'm new to this forum (and to opengl in general).
I need a hand with translating portions of a software rasterizer to opengl.
Up to now I made some progress, but I'm not skilled enough to front some of the advanced techniques, like correctly combining textures and other things.

My first question is about combining two textures in opengl.
I'm using glActiveTextureARB extension.

few definitions first:
cother - contains rgba values of texture1 texel
clocal - contains rgba values of texture0 texel
tc_ prefix applies to R,G and B channels.
tca_ prefix applies to A channel.
the possible path that the pipeline can take is determined by few selectors, named TEXMODE_*

Here's the code that combines the texels of the two textures:
http://pastebin.com/jTHZ1a51

this is basically what it should do.
to sum it up, it calculates
- the color tr,tg,tb,ta, starting from simple operations on texture1 texel (cother), texture0 texel (clocal) or constant color/alpha (zero)
- the color blendr/g/b/a from simple operations on texture1 texel (cother), texture0 texel (clocal) , constant color/alpha (zero), constant value for rgba (lod/detail stuff)
- does an average of the above two resulting color
- optionally adds clocal color or alpha to the result
- optionally inverts final color

I guess (hope) it's just a matter of correctly setting glTexEnv, without the need of using shaders or other recent technologies; the purpose it's not to perfectly translate everything, it's just to make it work :)

I'm looking at some sample code on multitexturing, like this (http://www.codesampler.com/oglsrc/oglsrc_4.htm), and I'm reading the online opengl documentation, but I find hard to understand how glTexEnv parameters and operands work.

Thanks in advance for any help.

ravage
03-19-2011, 11:26 AM
Here's something simple:

//sets current fragment to texture1
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);


//multiples texture1 with texture2
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);


You can give different or same texcoords per texture unit by using glMultiTexCoord instead of glTexCoord which I think only work on GL_TEXTURE0 (texture unit). So you should set up your texcoords even if they are the same with glMultiTexCoord if you want to do multitexturing. I use shaders which are much more easier to deal with and if you have time might want to study them.

remdul
03-19-2011, 12:11 PM
Depending on how much you want invest in learning OpenGL, you may want to use shaders (GLSL) from the start. Texture environment, register combiners etc. are very outdated, inflexible and IMO more complex than just getting GLSL working. Even if you want to do simple rendering, GLSL can be very 'direct' and convenient as you don't have to keep track of many GL states.

If GLSL is not an option and texture environment alone is too limited, another method is multi-pass rendering, where you draw the geometry several times with different textures and blend modes. Tip to get you started: glDepthFunc(GL_EQUAL).

kekko
03-30-2011, 02:30 PM
Thank you both for your answers.
It looks like the better option is to use shaders; unfortunately I've limited knowledge of how to use them, but I'd like to learn.
Could you point me to a very very simple code, as portable as possible (ARB?), without third party libs, that loads and uses basic shaders?
thanks.

Alfonse Reinheart
03-30-2011, 02:49 PM
Could you point me to a very very simple code, as portable as possible (ARB?), without third party libs, that loads and uses basic shaders?

There are the tutorials in my signature. They do use "third party libs," but those libraries are included with the distribution, and are for dealing with issues outside of OpenGL (creating the GL window, image loading, math, etc).

mhagain
03-30-2011, 04:47 PM
Depending on how much you want invest in learning OpenGL, you may want to use shaders (GLSL) from the start. Texture environment, register combiners etc. are very outdated, inflexible and IMO more complex than just getting GLSL working. Even if you want to do simple rendering, GLSL can be very 'direct' and convenient as you don't have to keep track of many GL states.
I'll second this and add that you might find that a lot of per-pixel tricks that are possible in a pure software renderer are just not possible in hardware without either (a) performance-killing stuff like heavy tesselation, multiple texture updates per frame and/or framebuffer readbacks; or (b) using shaders.

kekko
04-10-2011, 11:54 AM
thank you all for your suggestions.
I'm trying to translate that texture combiner to GLSL; up to now, I got this:
http://pastebin.com/jTHZ1a51
translated into this:
http://pastebin.com/RCySvMDL

lod-based combiners are still not converted.
could you please give me some comments/critiques on this ?

thanks :)