Today, we will implement a flatten shader.

What this shader does is compressing 3D model into a x-z plane, simply by setting y of all vertices to zero.


As you know, all compressing is implemented in vertex shader, fragment shader doesn’t matter.


Below is the original 3D model



Here comes the effects

flattenShader2 flattenShader1


Source Code

flattenShader.vs, vertex shader

// Flatten shader

// Created by Zhiyong Yang,


// The shader will compress y axis to one pixel in length.

// In that case, all models are projected to x-z plane, result in a flatten model.


// This shader need no complex fragment shader in pair.

void main()


vec4 position = gl_Vertex;


gl_Position = gl_ModelViewProjectionMatrix * position;

gl_FrontColor = gl_Color;

gl_TexCoord[0] = gl_MultiTexCoord0;


Fragment shader

// simple fragment shader

// 'time' contains seconds since the program was linked.

uniform float time;

void main()


gl_FragColor = gl_Color;



Next, we will implement a more complex compressing .

Last edited Mar 13, 2013 at 2:56 PM by younthu, version 2


No comments yet.