Immediate mode

From DmWiki

Immediate mode is way of interacting with graphics APIs. It is a simple way of sending data to the card piece by piece, i.e. sending vertices one by one. The alternative way of working is to submit large chunks of memory in one go for rendering.


Immediate mode is quite useful in some situations:

  • Prototyping applications, and creating proof-of-concepts
  • Writing testing code
  • For one off settings like vertex color


However, when submiting anywhere near a reasonable load to the video card the cpu time wasted making a large amount of function calls becomes significant. Using a call that submits a big chunk of memory in one go is a lot more efficient and preferred in nearly all circumstances. Also, when submiting a block of vertices the processor can quite efficiently process them in a big stream using modern instructions.

Submitting one vertex in OpenGL's immediate mode

// Send the vertex normal
glNormal3f (0.5, 0.1, 0.3);

// Send the vertex color
glColor4f (0.3, 0.3, 0.8, 1.0);

// Send the vertex position
glVertex3i (2, 6, 8);

/* Repeat ad-infinitum till the triangles are all sent */

Submitting an entire model in OpenGL

struct vec3
{
     float x, y, z;
};

struct vec2
{
     float x, y;
};

struct OpenGLVert
{
     vec3 Position, Normal;
     vec2 TexCoord;
};
 
OpenGLVert * Vertices;

//create an array of OpenGLVertices, NumVertices long
Vertices = new OpenGLVert[NumVertices];
gl[something]Pointer(NumComponents, ComponentTypes, Stride, Pointer)

NumComponents: number of components in the vector ComponentTypes: type of the components in the vector EX: for a 3 component float vector as the vertex position, you'd do glVertexPointer(3, GL_FLOAT, [Some Stride], [Some Pointer]); stride: sizeof your vertex structure (or 0 if you store each array of vertices separately) pointer: points to the first occurence of your vector in the array

//Let openGL know about these vertices
glVertexPointer(3, GL_FLOAT, sizeof(OpenGLVert), &Vertices[0].Position);

Now openGL knows that every sizeof(OpenGLVert) bytes it will find 3 floats that represent the vertex's position

glNormalPointer(GL_FLOAT, sizeof(OpenGLVert), &Vertices[0].Normal);

Now openGL knows that every sizeof(OpenGLVert) bytes it will find 3 floats representing the vertex's normal. NOTE:I didn't specify 3 for the normal pointer, thats because normals can ONLY be 3 components

glTexCoordPointer(2, GL_FLOAT, sizeof(OpenGLVert), &Vertices[0].TexCoord);

now openGL knows that every sizeof(OpenGLVert) bytes it will find 2 floats that are the texture coordinate

// Draw a stream of vertices that form triangles
glDrawArrays (GL_TRIANGLES, 0, NumVertices);

// Alternative: Draw vertices (as triangles) according to a stream of indicies
glDrawElements (GL_TRIANGLES, numberOfIndicies, GL_UNSIGNED_SHORT, indexPointer );	


DevMaster navigation