Convert OpenGL 1.1 code to OpenGL 2.0

Go To


Want convert OpenGL 1.1 code to OpenGL 2.0.

I use Cocos2d v2.0 (a framework for building 2D games)

2012-04-04 05:52
by Sinba
You need to bind shader to drow something. Without shader nothing wil be drawn - Mārtiņš Možeiko 2012-04-04 06:12


First you have to get your shaders into your program. You can either copy the shader code straight into a const char * like I show below or you can load the shader at runtime from a file, which varies depending on what platforms you are developing for so I won't show how to do that.

const char *vertexShader = "... Vertex Shader source ...";
const char *fragmentShader = "... Fragment Shader source ...";

Create shaders for both a vertex shader and a fragment shader and store their id's:

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

This is where you fill the shaders you just created with the data you either copy pasted into a const char * or loaded from file:

glShaderSource(vertexShader, 1, &vertexShader, NULL);
glShaderSource(fragmentShader, 1, &fragmentShader, NULL);

Then you tell the program to compile you shaders:


Create a shader program, which is the interface to your shaders from OpenGL:

shaderProgram = glCreateProgram();

Attach your shaders to the shader program:

glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);

Link the shader program to your program:


Tell OpenGL you want to use the shader program you created:


Vertex Shader:

attribute vec4 a_Position; // Attribute is data send with each vertex. Position 
attribute vec2 a_TexCoord; // and texture coordinates is the example here

uniform mat4 u_ModelViewProjMatrix; // This is sent from within your program

varying mediump vec2 v_TexCoord; // varying means it is passed to next shader

void main()
    // Multiply the model view projection matrix with the vertex position
    gl_Position = u_ModelViewProjMatrix* a_Position;
    v_TexCoord = a_TexCoord; // Send texture coordinate data to fragment shader.

Fragment Shader:

precision mediump float; // Mandatory OpenGL ES float precision

varying vec2 v_TexCoord; // Sent in from Vertex Shader

uniform sampler2D u_Texture; // The texture to use sent from within your program.

void main()
    // The pixel colors are set to the texture according to texture coordinates.
    gl_FragColor =  texture2D(u_Texture, v_TexCoord);
2012-04-04 07:42
by Nyatra