Converting OpenGL 1.1 Code to OpenGL 2.0

Want to convert OpenGL 1.1 to OpenGL 2.0.

I am using Cocos2d v2.0 (framework for building 2D games)

+3


source to share


1 answer


You must first get your shaders into your program. You can copy the shader code directly into a const char * as shown below, or you can load the shader at runtime from a file that depends on which platforms you are developing on, so I won't show you how to do that.

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

      

Create shaders for both the vertex shader and the fragment shader and store their identifiers:

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

      

This is where you populate the shaders you created with data that you either copied to a const char * file or loaded from a file:

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

      

Then you tell the program to compile the shaders:

glCompileShader(vertexShader);
glCompileShader(fragmentShader);

      

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

shaderProgram = glCreateProgram();

      



Attach shaders to your shader program:

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

      

Link the shader program to your program:

glLinkProgram(shaderProgram);

      

Tell OpenGL that you want to use the generated shader program:

glUseProgram(shaderProgram);

      

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);
}

      

+2


source







All Articles