Sign in to follow this  
Aeluned

Refraction

Recommended Posts

Aeluned    196
I have a background image taking up the whole viewport and I want to render a 3D object atop it that performs refraction. I don't care about chromatic abberation, I just want to warp the background image. I've seen this done places, but can't find a moronically simple demo in C++. I think I need cube maps for this? I'm trying to achieve something like this: clicky see how the background is warped through the metaballs? can anyone point me to a link or something?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
i beleive your looking for fragment shaders (aka pixel shaders)

Share this post


Link to post
Share on other sites
Aeluned    196
Yes,I realize there's probably a fragment shader going on...and I know how to use those. Could you be more specific?

I'm going thru the demo that generated the pic I posted, but it's sorta involved, and the shader is written in ASM type language - unfortunately, I'm only familiar with high level shader languages.

Share this post


Link to post
Share on other sites
dimebolt    440
Quote:
Original post by Anonymous Poster
i beleive your looking for fragment shaders (aka pixel shaders)


I believe the anonymous poster is right. Actually, the demo that generated the picture you linked to gives a good example. I doubt it will get any simpler than that demo. You'll need a good graphics card though.

Tom

edit: sorry, you posted while I was writing...

Share this post


Link to post
Share on other sites
cwhite    586
It's actually a pretty simple concept. You store the background image in a cubemap, then instead of using the eye vector to look up the cubemap, you perturb the eye vector based on the normal of the surface, the index of refraction, and snell's law, then use the new vector as the lookup into the cube map. If you want to extend this, you can also add fresnel effects.

Share this post


Link to post
Share on other sites
Aeluned    196
My biggest problem is getting the cube map working.
(Sorry, I don't know the source tag).

GreecePosX = g_pTextureMgr->LoadImageRec("C:\\Images\\GreeceCubeMap\\GreecePX.png");
GreeceNegX = g_pTextureMgr->LoadImageRec("C:\\Images\\GreeceCubeMap\\GreeceNX.png");
GreecePosY = g_pTextureMgr->LoadImageRec("C:\\Images\\GreeceCubeMap\\GreecePY.png");
GreeceNegY = g_pTextureMgr->LoadImageRec("C:\\Images\\GreeceCubeMap\\GreeceNY.png");
GreecePosZ = g_pTextureMgr->LoadImageRec("C:\\Images\\GreeceCubeMap\\GreecePZ.png");
GreeceNegZ = g_pTextureMgr->LoadImageRec("C:\\Images\\GreeceCubeMap\\GreeceNZ.png");

glGenTextures(1,&M_iTexName);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, M_iTexName);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
0, 4, GreecePosX->sizeX, GreecePosX->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, GreecePosX->data);

GLint err = 0;
err = glGetError();

glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
0, 4, GreeceNegX->sizeX, GreeceNegX->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, GreeceNegX->data);

err = glGetError();
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
0, 4, GreecePosY->sizeY, GreecePosY->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, GreecePosY->data);

err = glGetError();
glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
0, 4, GreeceNegY->sizeX, GreeceNegY->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, GreeceNegY->data);

glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
0, 4, GreecePosZ->sizeX, GreecePosZ->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, GreecePosZ->data);

glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
0, 4, GreeceNegZ->sizeX, GreeceNegZ->sizeY, 0,
GL_RGBA, GL_UNSIGNED_BYTE, GreeceNegZ->data);

glDisable(GL_TEXTURE_CUBE_MAP_ARB);


.....

then later, before I render I call

glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, M_iTexName);
glEnable(GL_TEXTURE_CUBE_MAP_ARB);

there are no errors on the texture upload and the data seems to be fine.

however, when I sample the texture in the fragment shader I get black.
So, somehow the the texture isn't making it to the board?

If i don't use a shader and just enable(GL_TEXTURE_CUBE_MAP_ARB), I get the object untextured.

...I can't believe I can't use a cube map.

any ideas?

Added source and code tags, please check the Forum FAQ for the tags in use on this board

[Edited by - _the_phantom_ on June 29, 2005 6:51:36 PM]

Share this post


Link to post
Share on other sites
Aeluned    196
nvm, I didn't declare the uniform cubeSampler in the vertex program (only in the fragment program) so I suppose the compiler threw it away.


It sorta works now.

Share this post


Link to post
Share on other sites
Ademan555    361
That actually brings up an interesting question. I know with, say, water in half life 2 and such, their refraction doesnt bother with a cubemap, i remember seeing something describing it something like this:

1. Render underwater with the water plane as a clip plane
2. render normals (or some kind of perturbation values) into a texture for the water
3. render this somehow perturbing texture coordinates with the perturbation values

how do they deal with the perturbation values?

thanks
cheers
-Dan

Share this post


Link to post
Share on other sites
adam17    227
Quote:
Original post by Ademan555
1. Render underwater with the water plane as a clip plane
2. render normals (or some kind of perturbation values) into a texture for the water
3. render this somehow perturbing texture coordinates with the perturbation values


follow step 1 as you said and store into an image. step 2, render the interpolated normals into a normal map. step 3 use the normal map and the underwater image, using the normal map as an offset map, access the underwater map. it sounds kinda confusing but read it a few times and it will make perfect sense

Share this post


Link to post
Share on other sites
Aeluned    196
Quote:
Original post by James Trotter
There's an example shader in glsl for a reflection/refraction effect in RenderMonkey. You might like to have a look at how it's done.

Hope this helps.


Sorry, I couldn't quite find the demo you were referring to. Got a link?

@Ademan555: that would be ideal. For my purposes, I'm always just trying to refract a background image...it's seems wasteful to have to upload 6 textures for that.

Share this post


Link to post
Share on other sites
James Trotter    432
Quote:
Original post by Aeluned
Quote:
Original post by James Trotter
There's an example shader in glsl for a reflection/refraction effect in RenderMonkey. You might like to have a look at how it's done.

Hope this helps.


Sorry, I couldn't quite find the demo you were referring to. Got a link?


Actually, it might be a little awkward. You need to download and install RenderMonkey. It's a pretty big download as well. But, if you choose to do so, then load the "Glass.rfx" file, and have a look at how they implemented reflection/refraction. I believe they use a cube map, so it might not be what you're looking for.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this