PDA

View Full Version : Texture projection with stenciling



gib
01-20-2003, 05:56 PM
Hi,
I want to project a single copy of a texture image, i.e. with no repetition. So far, I haven't been able to get the effect I want using GL_CLAMP unless I add a black border. I don't want to use a border because that removes a 1-texel strip from around the texture image.

Since what I want to do, effectively, is project through a stencil, it seems that maybe I should be using the stencil buffer. My attempts to do this have failed so far though. Is the stencil buffer the right way to go, or is there a better method? Any useful code examples?

thanks
Gib

suo_alex
01-20-2003, 06:46 PM
Hi,

I tried to search documentation before. It seems the stencil buffer is NOT available under windows.

Or anybody can tell me how to simulate the similar effect?

Mezz
01-20-2003, 06:51 PM
I don't know where you get the idea that stencil isn't available under Windows...
It does depend on you requesting an appropriate pixel format though.

-Mezz

Coriolis
01-20-2003, 06:56 PM
Stencil buffer is supported on windows.

You cannot project through it though... it is a screen buffer, like the color buffer or the depth buffer. It can be used for information about individual pixels on the screen, at the time of writing to those pixels.

gib
01-20-2003, 07:24 PM
So it seems that the stencil buffer is _not_ the way to do what I want. Is there a way?

Gib

ScottManDeath
01-20-2003, 11:33 PM
Originally posted by gib:
So it seems that the stencil buffer is _not_ the way to do what I want. Is there a way?

Gib

Hi

setting the wrapping mode to GL_CLAMP_TO_EDGE should do it. Its part of at least GL 1.4 (or lower) core or an GL_ARB_texture_clamp _ extension (don't know the exact name)

It assures that sampling does not use border colors.

Bye
ScottManDeath

JustHanging
01-21-2003, 01:21 AM
Hi,

I think you should put zero alpha to texels on the edge of the texture, use GL_CLAMP and use alpha test to reject pixels outside the texture. Another option are clipping planes. Which one is faster depends on whether you are fill or geometry limited. On new cards, especially NVidia, alpha test is faster.

-Ilkka

cass
01-21-2003, 02:11 AM
Be careful with clipping planes for multi-pass algorithms. Clipped geometry is not generally invariant with unclipped geometry.

Ostsol
01-21-2003, 05:54 AM
Originally posted by ScottManDeath:
Hi

setting the wrapping mode to GL_CLAMP_TO_EDGE should do it. Its part of at least GL 1.4 (or lower) core or an GL_ARB_texture_clamp _ extension (don't know the exact name)

It assures that sampling does not use border colors.

Bye
ScottManDeath



ARB_texture_border_clamp. -Very- useful, as long as one's video card supports it. It's also incredibly simple to use.

PH
01-21-2003, 06:14 AM
You *can* use the stencil buffer if you really wanted to. If what you want to do is to project a texture, render the frustum planes as a "light volume" to tag pixels, thus limiting the texture projection to those pixels.
It's a bit overkill but it works.

I prefer clamp_to_border or clamp_to_edge ( for textures where that makes sense, like pre-generated spotlight textures ).

Edit: That is, the frustum for the texture projection, not the view frustum.


[This message has been edited by PH (edited 01-21-2003).]

Gorg
01-21-2003, 06:15 AM
Originally posted by Ostsol:
ARB_texture_border_clamp. -Very- useful, as long as one's video card supports it. It's also incredibly simple to use.


It's in core Ogl1.2. Most current card should have it now.

gib
01-21-2003, 11:51 AM
The current project I'm working on is on Windows, and GL_CLAMP_TO_EDGE is not recognized. I have a recent GeForce 4. Do I need to upgrade the nVidia driver or some other library?

Gib

SirKnight
01-21-2003, 02:44 PM
Originally posted by gib:
The current project I'm working on is on Windows, and GL_CLAMP_TO_EDGE is not recognized. I have a recent GeForce 4. Do I need to upgrade the nVidia driver or some other library?

Gib

It works fine for me so I think you need to get newer opengl headers. I think you can find them on opengl.org or maybe even at cvs1.nvidia.com. I'm not sure about the cvs site but you never know, I havn't looked on there in quite a while.

Just for the record, GL_GLAMP_TO_EDGE is defined as 0x812F. So you can always just write that define in your code. But I would try to find newer headers first if I were you. If all else fails just define it like above. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

-SirKnight

Gorg
01-21-2003, 02:49 PM
SirKnight is right. Windows headers are for GL1.1.

You can use many of the available library to load extensions or just create a new header yourself.

To use GL_CLAMP_TO_EDGE, you need to have the enumerant(which sirkngiht gave) and a driver supporting opengl 1.2 (reading the GL_VERSION string) or a driver that supports GL_EXT_texture_egde_clamp( from GL_EXTENSION string).

Actually, this leads me to a question :

If a driver supports Opengl1.2, does it have to also expose the ext or arb extensions that was promoted in the extension strings?

[This message has been edited by Gorg (edited 01-21-2003).]

gib
01-21-2003, 03:24 PM
I added a define for GL_CLAMP_TO_EDGE, but it seems to behave just like GL_CLAMP. The edge texels on the texture image are used outside the texture. I can suppress this by modifying the 1-texel strip around the texture to have color (0 0 0) or to have alpha=0 then using an alpha test. These two methods are equivalent for my purposes, and they both remove a 1-texel border from my texture image, which I don't want to do.

How can I use GL_CLAMP_TO_EDGE and not lose the edge of my image?

Gib

ScottManDeath
01-22-2003, 12:16 AM
Originally posted by gib:
I added a define for GL_CLAMP_TO_EDGE, but it seems to behave just like GL_CLAMP. The edge texels on the texture image are used outside the texture. I can suppress this by modifying the 1-texel strip around the texture to have color (0 0 0) or to have alpha=0 then using an alpha test. These two methods are equivalent for my purposes, and they both remove a 1-texel border from my texture image, which I don't want to do.

How can I use GL_CLAMP_TO_EDGE and not lose the edge of my image?

Gib

Hi

Im not sure if it helps but nVidia drivers have a driver setting to enable conformant textur clamping. It is somewhere in the OpenGL driver settings panel. Perhaps this helps.

Bye
ScottManDeath

Tom Nuydens
01-22-2003, 12:40 AM
What you need is GL_CLAMP_TO_BORDER, not GL_CLAMP_TO_EDGE. I believe this is only accelerated on GeForce3 and up, though.

-- Tom

JustHanging
01-22-2003, 12:43 AM
Is losing that 1-texel strip really that serious? You seem to be going through a lot of trouble to avoid it. Maybe if you have very low-res textures, otherwise it just can't make that big a difference.

-Ilkka

gib
01-22-2003, 10:26 AM
In some cases 1 texel is significant, in others not (remember, my textures are really images for projecting). For generality I need to take care of it. And with GL_CLAMP_TO_BORDER (thanks Tom!!) it is actually very easy.

An minor observation: I now have two ways of "stenciling" the texture projection, one using clipping planes, the other using GL_CLAMP_TO_BORDER. Both work fine, but in the case of clipping the edges show staircasing, which is mostly absent in the other case.

I have a related question. Is it still the case that a texture must be 2^n x 2^m?

Gib

Coriolis
01-22-2003, 01:07 PM
There are some extensions for texture rectangles, but they usually have restrictions like they can only be used for hud-like stuff.

A 1-texel strip usually won't work if you use mipmapping... because you need a 2-pixel strip at mipmap level 0 to get a 1-pixel strip at mipmap level 1, a 4-pixel strip at 0 to get a 1 pixel strip at 2, and so on until you have to have a completely transparent image. So, you either have to disable mipmapping or live with the artifacts of whatever strip size you chose.

sanjo
01-23-2003, 12:36 AM
whats the difference between GL_CLAMP_TO_BORDER and GL_CLAMP_TO_EDGE ?

sounds similar though.

Tom Nuydens
01-23-2003, 01:55 AM
CLAMP_TO_EDGE repeats the edge pixels of your texture image. CLAMP_TO_BORDER repeats the border pixels of your images. The border pixels are the ones that you can add to a power-of-two image, e.g. to go from 256x256 to 258x258, whereas the edge pixels would be part of your 256x256 image.

If you don't specify border pixels, it will use the constant border color instead.

-- Tom