Issue with translucent Terrain (Probably Depth Buffer issues)
up vote
0
down vote
favorite
I have issues with rendering. The Blocks im rendering are somewhat translucent. Everything is fine as long as im using Nvidia Graphics Cards. On Intel (integrated) or AMD Graphics Cards it looks like this:

As you can see there are blocks being rendered, that should be behind others.
This are my prepare methods.
public void prepare()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(skyColour.X, skyColour.Y, skyColour.Z, 0f);
}
public void enableGLCaps()
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.DepthClamp);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
}
This is my vertex shader:
#version 440
in vec3 position;
in vec2 textureCoords;
out vec2 pass_textureCoords;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float density;
uniform float gradient;
void main() {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
And here my fragment shader:
#version 440
uniform sampler2D textureSampler;
uniform vec3 skyColour;
in vec2 pass_textureCoords;
in float visibility;
out vec4 color;
void main() {
vec4 textureColour = texture(textureSampler, pass_textureCoords);
if (textureColour.a < 0.5)
discard;
color = mix(vec4(skyColour, 1.0), textureColour, visibility);
color.a = 1;
}
Changing the near plane doesnt help.
EDIT 1:
I have rendered the z value and have noticed, that some faces definitely dont have the correct value.
I am rendering every texture in a region (16*16*16 chunks) as a single VAO (will use texture atlases later, but im running into flickering/overlapping with mipmapping using it). The Faces, that are rendered as one VAO are not overlapping each other so the z buffer seems to be ok there. It looks like its being retested after every render call.
Here is my render call:
GL.DrawElements(BeginMode.Quads, vertices, DrawElementsType.UnsignedInt, 0);
SOLUTION:
The depth function I was using was wrong. You should be using Lequal. I also had to set the depth range to (0, 1).
c# opengl rendering opentk depth-buffer
add a comment |
up vote
0
down vote
favorite
I have issues with rendering. The Blocks im rendering are somewhat translucent. Everything is fine as long as im using Nvidia Graphics Cards. On Intel (integrated) or AMD Graphics Cards it looks like this:

As you can see there are blocks being rendered, that should be behind others.
This are my prepare methods.
public void prepare()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(skyColour.X, skyColour.Y, skyColour.Z, 0f);
}
public void enableGLCaps()
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.DepthClamp);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
}
This is my vertex shader:
#version 440
in vec3 position;
in vec2 textureCoords;
out vec2 pass_textureCoords;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float density;
uniform float gradient;
void main() {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
And here my fragment shader:
#version 440
uniform sampler2D textureSampler;
uniform vec3 skyColour;
in vec2 pass_textureCoords;
in float visibility;
out vec4 color;
void main() {
vec4 textureColour = texture(textureSampler, pass_textureCoords);
if (textureColour.a < 0.5)
discard;
color = mix(vec4(skyColour, 1.0), textureColour, visibility);
color.a = 1;
}
Changing the near plane doesnt help.
EDIT 1:
I have rendered the z value and have noticed, that some faces definitely dont have the correct value.
I am rendering every texture in a region (16*16*16 chunks) as a single VAO (will use texture atlases later, but im running into flickering/overlapping with mipmapping using it). The Faces, that are rendered as one VAO are not overlapping each other so the z buffer seems to be ok there. It looks like its being retested after every render call.
Here is my render call:
GL.DrawElements(BeginMode.Quads, vertices, DrawElementsType.UnsignedInt, 0);
SOLUTION:
The depth function I was using was wrong. You should be using Lequal. I also had to set the depth range to (0, 1).
c# opengl rendering opentk depth-buffer
I'd check the value ofpow(x,y). It may be undefined ifx<0orx==0 && y<=0In other words, some imprecision for different drivers ondistance*densitycalculation.
– Ripi2
Nov 12 at 0:08
That isnt the issue unfortunately. Ive removed everything but the three matrix multiplications and the texture function in the fragment shader. Still the same result. Ive rendered the Z value and there are many Faces not rendering in the color they are supposed to (many are rendered white = near, even tho they are far away). So i think its definitely an issue with the depth buffer.
– N. J. Funk
Nov 12 at 2:12
See the edit for more information.
– N. J. Funk
Nov 12 at 2:36
it might be also driver problem ... I got similar problems on AMD and Intel while rendering into texture and use result on semitransparent object. Intel is not capable of rendering to texture (other than ugly slow CPU/GPU side transfers) and AMD has problems with rendering correctly. The only work around I found is to not use rendering to texture at all on both vendors. But it does not look like you are rendering to texture so it might be entirely different issue (however I suspect its related)
– Spektre
Nov 12 at 7:44
what z value have you rendered? Depth or space camera? Also your atmospheric fog computation should only take into accountpositionRelativeToCam.z, which contains the depth or distance from the camera to the vertex
– Nadir
Nov 12 at 14:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have issues with rendering. The Blocks im rendering are somewhat translucent. Everything is fine as long as im using Nvidia Graphics Cards. On Intel (integrated) or AMD Graphics Cards it looks like this:

As you can see there are blocks being rendered, that should be behind others.
This are my prepare methods.
public void prepare()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(skyColour.X, skyColour.Y, skyColour.Z, 0f);
}
public void enableGLCaps()
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.DepthClamp);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
}
This is my vertex shader:
#version 440
in vec3 position;
in vec2 textureCoords;
out vec2 pass_textureCoords;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float density;
uniform float gradient;
void main() {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
And here my fragment shader:
#version 440
uniform sampler2D textureSampler;
uniform vec3 skyColour;
in vec2 pass_textureCoords;
in float visibility;
out vec4 color;
void main() {
vec4 textureColour = texture(textureSampler, pass_textureCoords);
if (textureColour.a < 0.5)
discard;
color = mix(vec4(skyColour, 1.0), textureColour, visibility);
color.a = 1;
}
Changing the near plane doesnt help.
EDIT 1:
I have rendered the z value and have noticed, that some faces definitely dont have the correct value.
I am rendering every texture in a region (16*16*16 chunks) as a single VAO (will use texture atlases later, but im running into flickering/overlapping with mipmapping using it). The Faces, that are rendered as one VAO are not overlapping each other so the z buffer seems to be ok there. It looks like its being retested after every render call.
Here is my render call:
GL.DrawElements(BeginMode.Quads, vertices, DrawElementsType.UnsignedInt, 0);
SOLUTION:
The depth function I was using was wrong. You should be using Lequal. I also had to set the depth range to (0, 1).
c# opengl rendering opentk depth-buffer
I have issues with rendering. The Blocks im rendering are somewhat translucent. Everything is fine as long as im using Nvidia Graphics Cards. On Intel (integrated) or AMD Graphics Cards it looks like this:

As you can see there are blocks being rendered, that should be behind others.
This are my prepare methods.
public void prepare()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(skyColour.X, skyColour.Y, skyColour.Z, 0f);
}
public void enableGLCaps()
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.DepthClamp);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
}
This is my vertex shader:
#version 440
in vec3 position;
in vec2 textureCoords;
out vec2 pass_textureCoords;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float density;
uniform float gradient;
void main() {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
And here my fragment shader:
#version 440
uniform sampler2D textureSampler;
uniform vec3 skyColour;
in vec2 pass_textureCoords;
in float visibility;
out vec4 color;
void main() {
vec4 textureColour = texture(textureSampler, pass_textureCoords);
if (textureColour.a < 0.5)
discard;
color = mix(vec4(skyColour, 1.0), textureColour, visibility);
color.a = 1;
}
Changing the near plane doesnt help.
EDIT 1:
I have rendered the z value and have noticed, that some faces definitely dont have the correct value.
I am rendering every texture in a region (16*16*16 chunks) as a single VAO (will use texture atlases later, but im running into flickering/overlapping with mipmapping using it). The Faces, that are rendered as one VAO are not overlapping each other so the z buffer seems to be ok there. It looks like its being retested after every render call.
Here is my render call:
GL.DrawElements(BeginMode.Quads, vertices, DrawElementsType.UnsignedInt, 0);
SOLUTION:
The depth function I was using was wrong. You should be using Lequal. I also had to set the depth range to (0, 1).
c# opengl rendering opentk depth-buffer
c# opengl rendering opentk depth-buffer
edited Dec 5 at 12:55
asked Nov 11 at 22:56
N. J. Funk
838
838
I'd check the value ofpow(x,y). It may be undefined ifx<0orx==0 && y<=0In other words, some imprecision for different drivers ondistance*densitycalculation.
– Ripi2
Nov 12 at 0:08
That isnt the issue unfortunately. Ive removed everything but the three matrix multiplications and the texture function in the fragment shader. Still the same result. Ive rendered the Z value and there are many Faces not rendering in the color they are supposed to (many are rendered white = near, even tho they are far away). So i think its definitely an issue with the depth buffer.
– N. J. Funk
Nov 12 at 2:12
See the edit for more information.
– N. J. Funk
Nov 12 at 2:36
it might be also driver problem ... I got similar problems on AMD and Intel while rendering into texture and use result on semitransparent object. Intel is not capable of rendering to texture (other than ugly slow CPU/GPU side transfers) and AMD has problems with rendering correctly. The only work around I found is to not use rendering to texture at all on both vendors. But it does not look like you are rendering to texture so it might be entirely different issue (however I suspect its related)
– Spektre
Nov 12 at 7:44
what z value have you rendered? Depth or space camera? Also your atmospheric fog computation should only take into accountpositionRelativeToCam.z, which contains the depth or distance from the camera to the vertex
– Nadir
Nov 12 at 14:04
add a comment |
I'd check the value ofpow(x,y). It may be undefined ifx<0orx==0 && y<=0In other words, some imprecision for different drivers ondistance*densitycalculation.
– Ripi2
Nov 12 at 0:08
That isnt the issue unfortunately. Ive removed everything but the three matrix multiplications and the texture function in the fragment shader. Still the same result. Ive rendered the Z value and there are many Faces not rendering in the color they are supposed to (many are rendered white = near, even tho they are far away). So i think its definitely an issue with the depth buffer.
– N. J. Funk
Nov 12 at 2:12
See the edit for more information.
– N. J. Funk
Nov 12 at 2:36
it might be also driver problem ... I got similar problems on AMD and Intel while rendering into texture and use result on semitransparent object. Intel is not capable of rendering to texture (other than ugly slow CPU/GPU side transfers) and AMD has problems with rendering correctly. The only work around I found is to not use rendering to texture at all on both vendors. But it does not look like you are rendering to texture so it might be entirely different issue (however I suspect its related)
– Spektre
Nov 12 at 7:44
what z value have you rendered? Depth or space camera? Also your atmospheric fog computation should only take into accountpositionRelativeToCam.z, which contains the depth or distance from the camera to the vertex
– Nadir
Nov 12 at 14:04
I'd check the value of
pow(x,y). It may be undefined if x<0 or x==0 && y<=0 In other words, some imprecision for different drivers on distance*density calculation.– Ripi2
Nov 12 at 0:08
I'd check the value of
pow(x,y). It may be undefined if x<0 or x==0 && y<=0 In other words, some imprecision for different drivers on distance*density calculation.– Ripi2
Nov 12 at 0:08
That isnt the issue unfortunately. Ive removed everything but the three matrix multiplications and the texture function in the fragment shader. Still the same result. Ive rendered the Z value and there are many Faces not rendering in the color they are supposed to (many are rendered white = near, even tho they are far away). So i think its definitely an issue with the depth buffer.
– N. J. Funk
Nov 12 at 2:12
That isnt the issue unfortunately. Ive removed everything but the three matrix multiplications and the texture function in the fragment shader. Still the same result. Ive rendered the Z value and there are many Faces not rendering in the color they are supposed to (many are rendered white = near, even tho they are far away). So i think its definitely an issue with the depth buffer.
– N. J. Funk
Nov 12 at 2:12
See the edit for more information.
– N. J. Funk
Nov 12 at 2:36
See the edit for more information.
– N. J. Funk
Nov 12 at 2:36
it might be also driver problem ... I got similar problems on AMD and Intel while rendering into texture and use result on semitransparent object. Intel is not capable of rendering to texture (other than ugly slow CPU/GPU side transfers) and AMD has problems with rendering correctly. The only work around I found is to not use rendering to texture at all on both vendors. But it does not look like you are rendering to texture so it might be entirely different issue (however I suspect its related)
– Spektre
Nov 12 at 7:44
it might be also driver problem ... I got similar problems on AMD and Intel while rendering into texture and use result on semitransparent object. Intel is not capable of rendering to texture (other than ugly slow CPU/GPU side transfers) and AMD has problems with rendering correctly. The only work around I found is to not use rendering to texture at all on both vendors. But it does not look like you are rendering to texture so it might be entirely different issue (however I suspect its related)
– Spektre
Nov 12 at 7:44
what z value have you rendered? Depth or space camera? Also your atmospheric fog computation should only take into account
positionRelativeToCam.z, which contains the depth or distance from the camera to the vertex– Nadir
Nov 12 at 14:04
what z value have you rendered? Depth or space camera? Also your atmospheric fog computation should only take into account
positionRelativeToCam.z, which contains the depth or distance from the camera to the vertex– Nadir
Nov 12 at 14:04
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254073%2fissue-with-translucent-terrain-probably-depth-buffer-issues%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254073%2fissue-with-translucent-terrain-probably-depth-buffer-issues%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'd check the value of
pow(x,y). It may be undefined ifx<0orx==0 && y<=0In other words, some imprecision for different drivers ondistance*densitycalculation.– Ripi2
Nov 12 at 0:08
That isnt the issue unfortunately. Ive removed everything but the three matrix multiplications and the texture function in the fragment shader. Still the same result. Ive rendered the Z value and there are many Faces not rendering in the color they are supposed to (many are rendered white = near, even tho they are far away). So i think its definitely an issue with the depth buffer.
– N. J. Funk
Nov 12 at 2:12
See the edit for more information.
– N. J. Funk
Nov 12 at 2:36
it might be also driver problem ... I got similar problems on AMD and Intel while rendering into texture and use result on semitransparent object. Intel is not capable of rendering to texture (other than ugly slow CPU/GPU side transfers) and AMD has problems with rendering correctly. The only work around I found is to not use rendering to texture at all on both vendors. But it does not look like you are rendering to texture so it might be entirely different issue (however I suspect its related)
– Spektre
Nov 12 at 7:44
what z value have you rendered? Depth or space camera? Also your atmospheric fog computation should only take into account
positionRelativeToCam.z, which contains the depth or distance from the camera to the vertex– Nadir
Nov 12 at 14:04