Jump to content

Semi transparent rendering


brandon3055

Recommended Posts

This is a problem i have run into every time i try to render a semi transparent object and i have never been able to find a way to fix it. Im hoping someone here who knows more about rendering can help me.

 

The problem i am having involves semi transparent objects hiding objects behind them (See example bellow)

ydCqBum.png

Also semi transparent objects hiding other semi transparent objects as seen where the beams converge.

 

For some reason the order in which the tiles are placed has an effect (The front left beam was placed last and things behind it are rendering just fine) But only until the world is reloaded.

 

Im really hoping someone can help me fix this problem because i have tryed everything i can think of and im starting to wonder if this problem can be fixed at all.

 

Code:

 

 

@Override
public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float partialTick) {
//--- Pre Render ---//
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glTranslated(x + 0.5, y + 0.5, z + 0.5);
GL11.glScalef(0.5f, 0.5f, 0.5f);
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);

//--- Render Bottom Layer ---//
bindTexture(crystalBlue);

model.renderAll();


//--- Render Alpha Overlay ---//
GL11.glEnable(GL11.GL_ALPHA_TEST);
bindTexture(crystalBlueAlpha);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.5f + ClientEventHandler.energyCrystalAlphaValue * 0.5f);

model.renderAll();

GL11.glDisable(GL11.GL_ALPHA_TEST);


//--- Render Overlay ---//
bindTexture(texrure);

model.renderAll();


//--- Post Render ---//
GL11.glPopAttrib();
GL11.glPopMatrix();


//--- Render Beam ---//
//Pre
GL11.glPushMatrix();
GL11.glTranslated(x + 0.5, y + 0.5, z + 0.5);
GL11.glEnable(GL11.GL_BLEND);
//OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
GL11.glDisable(GL11.GL_LIGHTING);
Tessellator tessellator = Tessellator.instance;//todo pass in
//GL11.glDisable(GL11.GL_CULL_FACE);
bindTexture(beamTexture);

//Render
renderBeam(tessellator, tileEntity.xCoord + 0.5, tileEntity.yCoord + 1.5, tileEntity.zCoord + 0.5, 0.5, 3.5, 0.5, partialTick);


//Post
//GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopMatrix();
}

private void renderBeam(Tessellator tessellator, double tileX, double tileY, double tileZ, double tX, double tY, double tZ, float partialTick)
{

	float xDist = (float)(tileX - tX);
	float yDist = (float)(tileY - tY);
	float zDist = (float)(tileZ - tZ);


	float IID_Dist = MathHelper.sqrt_float(xDist * xDist + zDist * zDist);
	float IIID_Dist = MathHelper.sqrt_float(xDist * xDist + yDist * yDist + zDist * zDist);


	GL11.glRotatef((float) (-Math.atan2((double) zDist, (double) xDist)) * 180.0F / (float) Math.PI - 90.0F, 0.0F, 1.0F, 0.0F);
	GL11.glRotatef((float)(-Math.atan2((double)IID_Dist, (double)yDist)) * 180.0F / (float)Math.PI + 90.0F, 1.0F, 0.0F, 0.0F);


	//float motion = ((float)ClientEventHandler.elapsedTicks + partialTick) * 0.05F;

	tessellator.startDrawingQuads();


	tessellator.addVertexWithUV(0, 0, 0, 0, 0);
	tessellator.addVertexWithUV(0, 0, IIID_Dist, 0, 1);
	tessellator.addVertexWithUV(0, 1, IIID_Dist, 1, 1);
	tessellator.addVertexWithUV(0, 1, 0, 1, 0);


	tessellator.draw();
}

 

 

I am the author of Draconic Evolution

Link to comment
Share on other sites

Well it can be fixed, the problem is that the beam is being rendered before the glowstone edged cube is rendered, if you see the chest and terrain are rendered perfectly.

 

You have to somehow draw them in the order of how they are displayed. Block first, beam second.

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

Or more accurately:

You need to render the beams after all other blocks.

 

Sometimes vanilla has this problem, rendering snow behind water, etc. and it all has to do with the fact that the rendering engine being used doesn't know how to sort transparent objects.  Then again, its kind of a holy grail.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So what i got from that is there is no quick fix for this problem. Instead i would have to build my own render engine capable of sorting transparent objects.

 

Before i came here for help i looked at another mod that dose something similar to what im trying to do and that mod was using particles for its beams which at the time i thought was a bit odd at the time. Well now i know why and i may have to do something similar.

I am the author of Draconic Evolution

Link to comment
Share on other sites

So what i got from that is there is no quick fix for this problem. Instead i would have to build my own render engine capable of sorting transparent objects.

 

Before i came here for help i looked at another mod that dose something similar to what im trying to do and that mod was using particles for its beams which at the time i thought was a bit odd at the time. Well now i know why and i may have to do something similar.

 

To be honest, I would first finish my mod and then if I have time I would try and make it look better.

 

MoSCoW Method :) (Must, Should, Could, Would) split all tasks in those criteria and move from there. Must are the ones that have to be completed no matter what, Should are tasks that are needed but not too urgent, Could are tasks that will help you in the long run but aren't too important and Would are the features that with or without the finished product would still work.

 

This method really allows me not to waste time while working on my projects. :)

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

Hmm thats not a bad method i will be using that a lot when i finally update to 1.8 (or later if i skip 1.8) But i my mod is already "Technically" finished in much the same way minecraft is "Technically" finished I just have a lot of new content planned so there is no real rush to get things like this done. I would also prefer do do it properly the first time so i dont ever have to worry about it again. Especially in this case where switching from a tesr  to a particle would require a lot of work in the tile entity. I would prefer to get my renderer working first and then design my tile bassed on that renderer.

I am the author of Draconic Evolution

Link to comment
Share on other sites

On the original question,

 

A trick often used in games is to use additive blending rather than src (1-dest).

 

Use GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);

rather than GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

 

Standard src (1-dest) isn't commutative meaning it has to be done in the correct order whereas additive can be done in any order - it also has the nice side effect of saturating the colours quite quickly the more that are overlaid.

 

Though from looking at the first image it looks like z writes are enabled on the beams - which is another issue. Particles or anything transparent should be rendered with z tests on and z writes off unless you sort them. Sorting large transparent objects is quite complex and needs some kind of separating axis test - hence the prevalent use of additive alpha and no z writes.

Link to comment
Share on other sites

  • 1 month later...

Let me elaborate for future readers why this is happening:

 

Computers don't know the principle of transparency. All they do is taking the color for every single point and merging it with the color of the transparent object by a rate of transparency (from 0%(invisible) to 100%(not transparent)). That way transparent objects have to be sortedfrom back to front line. When rendering a object and another behind it it already set the color of the first one so the second does not show up. Fiddling with GL_DEPTH_TEST is dangerous. Better find out the object positions then sort them.

 

I hope that helped ;)

Link to comment
Share on other sites

You shouldn't resurrect such old threads. But since you did, there is also the concept of rendering passes that helps sort through some of this, plus some other settings about side visibility and opaqueness.  TheGreyGhost has a detailed explanation here: http://greyminecraftcoder.blogspot.com.au/2013/07/rendering-transparent-blocks.html

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.