3.2.3. Combining the position coordinates and color values into a single array

  1. Locate the tutorial project 06 - Drawing a Triangle the Hard Way and open the main.cpp file.

  2. Modify the triangle data from Example 3.9 to move the values from colorData[] to the corresponding row in vertexData[]. Delete the code that assigns values to colorData[].

    Example 3.11. Vertex shading of simple triangle with floating-point color values

    ...
            GLfloat vertexData[] = 
            { 
                -1.0, -1.0, 1.0, 0, 0, 1.0,
                 1.0, -1.0, 0, 1.0, 0, 1.0,
                 0.0,  1.0 0, 0, 1.0, 1.0
            };
    ...
    

  3. Modify the vertex information from Example 3.5 to create vertexBuffer based on the larger vertexData array as shown in Example 3.12:

    Example 3.12. Using floating-point values for the color buffers

    ...
            Managed<Buffer> vertexBuffer = context->createBuffer(GL_ARRAY_BUFFER, sizeof(vertexData), sizeof(GLfloat)*6);
            vertexBuffer->setData(0, sizeof(vertexData), vertexData);
    
            VertexElement elements[2];
            elements[0].components = 2;                    
            elements[0].offset = 0;                        
            elements[0].semantic = POSITION;            
            elements[0].stream = 0;                        
            elements[0].type = GL_FLOAT;                
            elements[0].normalize = false;                
    
            elements[1].components = 4;                    
            elements[1].offset = 2*sizeof(GLfloat);                        
            elements[1].semantic = COLOR;                
            elements[1].stream = 1;                        
            elements[1].type = GL_FLOAT;        
            elements[1].normalize = true;  
    ...              
    

    The colorBuffer array is not required because the color information is now part of the vertexData array.

    The offset for elements[1] must be specified as 2*sizeof(GLfloat) to jump over the position values.

  4. The triangle has been specified, so it can be drawn as shown in Example 3.6.

    Example 3.13. Using the vertex information to draw the triangle

    ...
            do 
            {
    
                context->setVertexBuffer(0, vertexBuffer);
                context->setVertexBuffer(1, vertexBuffer);
    
                /* Draw indexed triangles using the first 3 vertices of the buffers */
                context->drawArrays(GL_TRIANGLES, 0, 1);
    
                if(keyboard->getSpecialKeyState(Keyboard::KEY_ESCAPE)) break;
            } while( context->update() );
            
        }
        catch(Exception& e)
        {
            printf("MDE \n%s\n\n", e.getMessage().getCharString());
        }
    }
    

    Unlike in Example 3.6, the context uses vertexBuffer for both stream 0 and stream 1 because both stream values are contained in one array.

  5. Rebuild the project and run it. Figure 3.3 shows the triangle appearance has not changed:

    Figure 3.4. The 2D triangle with combined position and color values in one vertexBuffer array

    The 2D triangle with combined position and color values in one vertexBuffer array

Copyright © 2010 ARM. All rights reserved.ARM DUI 0527A‑02a
Non-Confidential - Draft - BetaID070710