Question About OpenGl

Hello,

I have a question that involves using OpenGl directly in C++, and although this is a jMonkeyEngine forum, the people here seem to know OpenGl quite well and therefore may be willing and able to help.

I am currently trying to implement a terrain system in OpenGl 3.3 core profile that is similar to JME’s (I am even using the heightmap and textures from JME’s testdata, for now), and it seems to be working quite well, except I am getting artifacts in the form of black patterns on the otherwise correctly rendered terrain:

I am also getting an issue where when I go towards the far corner of the terrain that the entire terrain disappears depending on what angle I look through:

Vertex shader:

#version 330 core

layout (location = 0) in vec2 inputPosition;

uniform float terrainSize;
uniform sampler2D heightmapTexture;
uniform mat4 worldToClip;

out vec3 position;

void main(){
    float y = texture(heightmapTexture, vec2(inputPosition.x/terrainSize, inputPosition.y/terrainSize)).r*512;
    position = vec3(inputPosition.x, y, inputPosition.y);
    gl_Position = worldToClip*vec4(position.xyz, 1);
}

Fragment Shader:

#version 330 core

uniform float terrainSize;
uniform float terrainDensity;

uniform sampler2D alphaChannelTexture;
uniform sampler2D redChannelTexture;
uniform sampler2D greenChannelTexture;
uniform sampler2D blueChannelTexture;

in vec3 position;

out vec4 outColor;

float toTexCoord(float f){
    return mod(f/terrainDensity, 1);
}

void main(){
    vec4 alphaTexture = texture(alphaChannelTexture, (position.xz)*(1/terrainSize));
    float alphaFilterTotal = alphaTexture.r + alphaTexture.g + alphaTexture.b;

    vec3 color = vec3(0.0, 0.0, 0.0);
    vec2 textureCoordinates = vec2(toTexCoord(position.x), toTexCoord(position.z));

    color += (texture(redChannelTexture, textureCoordinates).rgb)*(alphaTexture.r/alphaFilterTotal);
    color += (texture(greenChannelTexture, textureCoordinates).rgb)*(alphaTexture.g/alphaFilterTotal);
    color += (texture(blueChannelTexture, textureCoordinates).rgb)*(alphaTexture.b/alphaFilterTotal);

    outColor = vec4(color.rgb, 1.0);
}

Of course, there are other parts of the program than just a vertex and fragment shader, but those are written in C++ and there is a fairly large amount of them, so I don’t know which parts to upload. They are, all, however, very standard, and match reasonably well in most places to what is present at learnopengl.com. The main exception is that I am using sfml for window creation and management.

Misc information:
OS: Ubuntu Linux 16.04 LTS
GPU: Nvidia GTX 770
Driver: proprietary 384.111
Compiler: GNU GCC (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Terrain size: 4096x4096 world units

Without seeing the rest of the code, I can only guess… but:

First problem looks like Z-fighting with a black mesh… like you are rendering something black behind it or you have two copies of the terrain and don’t realize it… but it definitely behaves like Z-fighting.

Second problem sounds like clipping… like whatever code you use to decide if your terrain is in view or not breaks at certain positions/angles.

Approximately 14,689.161 things to get right in a custom engine… so if you can knock those two down then there will only be 14,689,159 left.

3 Likes