Refraction

Started by
12 comments, last by Aeluned 18 years, 9 months ago
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?
Advertisement
i beleive your looking for fragment shaders (aka pixel shaders)
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.
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...
Yeah, I'm deep in the code now with a 6800.
I guess I'll have to figure it out from this...thanks.
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.
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]
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.
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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.

This topic is closed to new replies.

Advertisement