Jump to content

Transparency And Refraction tutorial


Thareh
 Share

Recommended Posts

Hi :blink:

I just followed the Transparency and Refraction tutorial and got it working -

except for the depth information which causes objects in front of the refraction to get refracted aswell.

Is the tutorial a bit broken or have I messed up somewhere? :blink:

 

Oh, and I am clearing the GBuffer after I've copied the LightBuffer and GBuffer to the Backbuffer.

Thanks!

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

I really don't have a clue on what could be wrong, I've tried 'everything'. :blink:

Anyone have a clue?

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

Can you please check again Josh? :(

I really can't get refraction to work without objects in front of the refraction getting refracted aswell.

I've experimented with it a bit, and I actually got the tutorial working now but only if I DON'T clear the depth information (?) and it'll only work with objects under VERY limited conditions.

And by VERY limited I mean, if you move the cube or the camera back 1 unit you'll get the refraction artifact around the cube again.

 

Thanks :D

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

Okey,

And what happens if you change the line:

 

PositionEntity(cam,Vec3(0,0,-1.5));

 

with

 

PositionEntity(cam,Vec3(0,0,-2.5));

 

Still working fine? :D

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

Yes there is :lol:

I've tried setting the 'MainCamera' and 'ForegroundCamera' to the same position etc, still doesn't work if that's where you're trying to point me ^^

 

SetEntityMatrix(fgcam,GetEntityMatrix(cam));

copies the 'MainCamera's position etc anyway, so just do as I said up there and replace the line:

PositionEntity(cam,Vec3(0,0,-1.5));

with

PositionEntity(cam,Vec3(0,0,-2.5));

In the last of the program examples in your 'Transparency and Refraction' tutorial.

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

It's pretty important that I get this working, I've been stuck now for 3 days.

Is there a way to get the refraction working properly? >.<

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

I can produce the problem. This is very strange. This code uses the up and down keys to move the camera forwards and backwards. There is a certain spot where the error appears and disappears almost instantly. Which GPU is this occuring on? I get an error with an ATI 3870, so it is unlikely to be a driver error if you are using NVidia cards:

#include "engine.h"

int main(int argc, char** argv)
{
Initialize();

//Create a graphics context
Graphics(800,600);

//Create a world
TWorld world=CreateWorld();
if (!world) {
	MessageBoxA(0,"Error","Failed to create world.",0);
	goto exitapp;
}

//Create a render buffer
TBuffer gbuffer=CreateBuffer(800,600,BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);

//Lighting buffer
TBuffer lightbuffer=CreateBuffer(800,600,BUFFER_COLOR);

//Create a camera
TEntity campiv=CreatePivot();
PositionEntity(campiv,Vec3(0,2,0));
TCamera cam=CreateCamera(campiv);
PositionEntity(cam,Vec3(0,0,-1.95));

//Load the scene
LoadMesh("scene.gmf");

//Create a light
TLight light=CreatePointLight();
PositionEntity(light,Vec3(0,1,0));

//Create the transparency world
TWorld foreground=CreateWorld();

//Create a camera for the transparency world
TCamera fgcam=CreateCamera();
CameraClearMode(fgcam,0);

//Create the transparent object
TMesh mesh=CreateSphere();
PositionEntity(mesh,Vec3(0,2,0));


//Load the transparent material
TMaterial material=LoadMaterial("glass_refraction.mat");
SetMaterialTexture(material,GetColorBuffer(lightbuffer),2);
SetMaterialTexture(material,GetDepthBuffer(gbuffer),3);
PaintEntity(mesh,material);
TShader refractionshader=GetMaterialShader(material);

SetWorld(world);

TMesh cube=CreateCube();
ScaleEntity(cube,Vec3(0.1));
PositionEntity(cube,Vec3(0,2,1));
TMaterial cubematerial=CreateMaterial();
SetMaterialColor(cubematerial,Vec4(1,0,0,1));
PaintEntity(cube,cubematerial);

float refractionstrength=0.01;

float mx=0.0,my=0.0;
TVec3 camrotation;

HideMouse();
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);

//Main loop
while(!KeyHit(KEY_ESCAPE)) {

	mx=Curve(MouseX()-GraphicsWidth()/2,mx,3);
	my=Curve(MouseY()-GraphicsHeight()/2,my,3);
	MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
	camrotation=EntityRotation(campiv);
	camrotation.X+=my;
	camrotation.Y-=mx;
	RotateEntity(campiv,camrotation);

	UpdateWorld();

	//Render the main world
	SetBuffer(gbuffer);
	SetWorld(world);
	RenderWorld();	

	//Render lighting
	SetBuffer(lightbuffer);
	RenderLights(gbuffer);

	CopyBuffer(lightbuffer,BackBuffer(),BUFFER_DEPTH|BUFFER_COLOR);
	CopyBuffer(gbuffer,BackBuffer(),BUFFER_DEPTH);

	//Set the refraction strength
	if (KeyDown(KEY_UP)) {MoveEntity(cam,Vec3(0,0,0.01)); }//{ refractionstrength+=0.001 ; }
	if (KeyDown(KEY_DOWN)) {MoveEntity(cam,Vec3(0,0,-0.01)); }//{ refractionstrength-=0.001 ; }
	refractionstrength=max(refractionstrength,0.0);
	SetShaderFloat(refractionshader,"refractionstrength",refractionstrength);

	//SetBuffer(gbuffer);
	//ClearBuffer();

	SetBuffer(BackBuffer());

	//Render transparency
	SetWorld(foreground);
	SetEntityMatrix(fgcam,GetEntityMatrix(cam));
	RenderWorld();
	SetWorld(world);

	Flip();
}

exitapp:
return Terminate();
}

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Find this line in mesh.frag around line 181:

		if (DepthToZPosition(gl_FragCoord.z)<DepthToZPosition(texture2DProj(LE_DEPTHBUFFER,refractionvector2).x)) {

 

Replace it with this:

		if (gl_FragCoord.z<texture2DProj(LE_DEPTHBUFFER,refractionvector2).x) {

 

The variable gl_FragCoord.z is a nonlinear depth value between 0.0 and 1.0. It's stored the same way a depth value in a depth texture is stored. We could convert it to a z position with the DepthToZPosition() function, or we can just compare it directly to the depth texture read:

http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=234519

 

I'm sure I knew this already, but I must have missed that when I wrote the shader.

 

This will fix your problem. :)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I get another artifact now though ^^

When I increase the refractionstrength, and I'm using a water bumpmap found in the leadwerks SDK dir (water02_00.dds) the refraction gets 'cut off' at the edge of an object in the way.

2mo5jkk.png

It's a bit hard to see without any motion, I can send you an example if you'd like.

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

I can't tell anything from that shot. It just looks like a lot of random colors.

 

I updated the lesson to include information on how to use global objects to automate the material texture settings.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Just go closer to the bridge using WASD, and you'll see how the refraction is cut-off near the bridge when it's in the way :)

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

You should add more objects in the air to test with. What is different about that bridge? The sides of the pit don't cause any error like that, and I haven't seen that happen in any of my examples.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I'll add more objects, but do this first:

Go to one of the pit walls, go straight up and look down in the pit.

At the edge of the pit it'll be glitched I think :)

Core2Duo, 3.33GHz, Radeon HD 5850 Black Edition, 4GB RAM, 4 TB HDD, Windows 7.

Core2Duo, 2.00GHz, Geforce 9500m, 4GB RAM, 320 GB HDD, Windows 7.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...