# drawing a horizon / sky in OpenGL ES 2.0

I want to render a sky: My camera is at the origin, and I want there to be a white background for all world coordinates with z < 0 (would normally be considered y > 0, but I have a different coordinate system via glLookAt()).

I thought I may be able to do this in the fragment shader by multiplying the inverse MVP matrix with the corresponding clip space (?) coordinate (I compute that matrix on the CPU each frame by calling Matrix.invertM()). To get my vertices into the fragment shader, I use a varying variable (I wasn't sure if I could access gl_Position in the fragment shader) and passed the position right through; not sure if my syntax is right (see below):

attribute vec4 position;
varying vec4 vp;
void main() {
gl_Position = position;
vp = position;
}


precision mediump float;
uniform mat4 inverseMatrix;
varying vec4 vp;
void main() {
vec4 inversePosition = inverseMatrix * vp;
if (inversePosition.z > 0.0) {
gl_FragColor = vec4(0,0,0,1);
} else {
gl_FragColor = vec4(1,1,1,1);
}
}


Question:

Does this approach seem sane? Should I use some other approach that doesn't involve multiplying each fragment coordinate with the inverse MVP matrix?

• You could multiply your varying with the matrix in the vertex shader, the results will be the same as they'll be interpolated, that way you at least do the calculation per vertex instead of per fragment. Also you can use gl_FragCoord in the fragment shader to get the fragments position, it's a vec4 holding the interpolated data from gl_Position. Jan 1 '16 at 9:09
• Did you considered using a cubemap for the background and a plane for the soil ? Jan 1 '16 at 9:46
• I was considering a cubemap, but I have never used one before and wasn't sure if that would actually be simpler? It seems like drawing the top (as in above the horizon) half of the screen in one solid color should be simple to do without texturing? cozmic - hm, would that actually be correct? Perspective is not a linear operation in 3D, which is why I wanted to interpolate the clipspace points. But perhaps it works as long as I do perspective divide for the interpolated 4d values. Jan 1 '16 at 10:12
• PS. I implemented both of these suggestions. The vertex shader doing the inverse matrix multiplication works great. The cubemap doesn't work because the colors that are above and below the horizon on four sides of the cubemap get blended by OpenGL, even though I have set glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); Perhaps it's a bug in OpenGL? Jan 1 '16 at 17:35