How do I sort the texture positions based on the texture indices given in a Wavefront (.obj) file?

If there are different indexes for vertex coordinates and texture coordinates, then the vertex positions must be “duplicated”. The vertex coordinate and its attributes (like texture coordinate) form a tuple. Each vertex coordinate must have its own texture coordinates and attributes. You can think of a 3D vertex coordinate and a 2D texture coordinate as … Read more

In a fragment shader, why can’t I use a flat input integer to index a uniform array of sampler2D?

[…] but can’t use it to index an array of samplers as expected because compiler sees it as “non-constant” […] In GLSL up to version 3.30 respectively GLSL ES up to version 3.00, the index of an array of texture samplers has to be a constant expression: GLSL 3.30 Specification – 4.1.7 Samplers (page 21) … Read more

OpenGL define vertex position in pixels

This is rather basic knowledge that your favourite OpenGL learning resource should teach you as one of the first things. But anyway the standard OpenGL pipeline is as follows: The vertex position is transformed from object-space (local to some object) into world-space (in respect to some global coordinate system). This transformation specifies where your object … Read more

OpenGL – mask with multiple textures

This should work: glEnable(GL_BLEND); // Use a simple blendfunc for drawing the background glBlendFunc(GL_ONE, GL_ZERO); // Draw entire background without masking drawQuad(backgroundTexture); // Next, we want a blendfunc that doesn’t change the color of any pixels, // but rather replaces the framebuffer alpha values with values based // on the whiteness of the mask. In … Read more

How exactly does OpenGL do perspectively correct linear interpolation?

The output of a vertex shader is a four component vector, vec4 gl_Position. From Section 13.6 Coordinate Transformations of core GL 4.4 spec: Clip coordinates for a vertex result from shader execution, which yields a vertex coordinate gl_Position. Perspective division on clip coordinates yields normalized device coordinates, followed by a viewport transformation (see section 13.6.1) … Read more

Getting the true z value from the depth buffer

From http://web.archive.org/web/20130416194336/http://olivers.posterous.com/linear-depth-in-glsl-for-real // == Post-process frag shader =========================================== uniform sampler2D depthBuffTex; uniform float zNear; uniform float zFar; varying vec2 vTexCoord; void main(void) { float z_b = texture2D(depthBuffTex, vTexCoord).x; float z_n = 2.0 * z_b – 1.0; float z_e = 2.0 * zNear * zFar / (zFar + zNear – z_n * (zFar – zNear)); } … Read more