• Advertisement
Sign in to follow this  

Refraction

This topic is 4618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
Guest Anonymous Poster
i beleive your looking for fragment shaders (aka pixel shaders)

Share this post


Link to post
Share on other sites
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
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
Yeah, I'm deep in the code now with a 6800.
I guess I'll have to figure it out from this...thanks.

Share this post


Link to post
Share on other sites
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
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
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
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
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
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
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
Yay. Well, I got it working using nothing more than a GL_TEXTURE_2D.
Thanks for the help/references/discussion.

I think it looks OK: screenshot

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement