How to create objects in code with SIO2Interactive

In the following example a material is created with a texture and a flat square with texture coordinates is created Inflatables Christmas For Sale.

Initialize A New Material

SIO2material * material;
material = sio2MaterialInit("material_name");
 
// Material Texture Flags
 
material->tflags[SIO2_MATERIAL_CHANNEL0] = SIO2_IMAGE_MIPMAP|SIO2_IMAGE_CLAMP;
 
// Material Diffuse Color
material->diffuse->x = 1.0f;
material->diffuse->y = 1.0f;
material->diffuse->z = 1.0f;
material->diffuse->w = 1.0f;
 
// Material Specular Color
material->specular->x = 1.0f;
material->specular->y = 1.0f;
material->specular->z = 1.0f;
 
// Material Alpha
material->alpha = 1;
material->alvl = 0;
material->diffuse->w = material->alpha;
material->specular->w = material->alpha;
material->shininess = 12;
material->blend = 0;
 
// Material Texture Name
strcpy(material->tname[ SIO2_MATERIAL_CHANNEL0 ], "image_name.png");
 
// Material Texure Image in Channel 0
material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = loadImage("image_name.png", SIO2_IMAGE_MIPMAP|SIO2_IMAGE_CLAMP);

Initialize A New Object

SIO2object * square = sio2ObjectInit("square");
 
float square_width = 32.0f;
float square_height = 32.0f;
 
square->dim->x = square_width;
square->dim->y = square_height;
square->dim->z = 0;
 
// VBO ( See sio2ObjectBindVBO to see how these are used )
square->vbo_offset[ SIO2_OBJECT_SIZE    ] = 80;
// This is the size of all the vertex buffers ( sizeof(v_coords) + sizeof(uv_coords) ) ...
 
square->vbo_offset[ SIO2_OBJECT_NORMALS ] = 0;
square->vbo_offset[ SIO2_OBJECT_VCOLOR  ] = 0;
square->vbo_offset[ SIO2_OBJECT_TEXUV0  ] = 48; // 0 + sizeof(v_coords) = 3*sizeof(float)*4 = 3*4*4 = 48
// The position in byte offset from the beginning of the buffer for this data.
 
square->vbo_offset[ SIO2_OBJECT_TEXUV1  ] = 0;
 
// Vertex Buffer ( All data(vertices,colors,uv0,uv1,normals) except for triangle indexes is copied into this buffer )
square->buf = (unsigned char*)malloc(square->vbo_offset[ SIO2_OBJECT_SIZE ]);
 
// Vertices
GLfloat v_coords[] = {
        square_width, square_height, 0,
        0, square_height, 0,
        0, 0, 0,
        square_width, 0, 0
};
 
// Vertex Array Goes First in the buffer.
memcpy(square->buf, v_coords, sizeof(v_coords));
 
// UV Coords
float uvwidth = 1.0f;
float uvheight = 1.0f;
 
GLfloat uv_coords[] = {
        uvwidth, 0,
        0, 0,
        0, uvheight,
        uvwidth, uvheight,
};
 
// The position depends on where you place the offsets it could be different.
// In this case it is directly after the vertex array.
memcpy(square->buf + 48, uv_coords, sizeof(uv_coords));
 
// Vertex Groups ( Only Creating One Vertex Group )
 
// Allocate Array of Pointers 1 For Each Vertex Group
square->n_vgroup = 1;
square->_SIO2vertexgroup = ( SIO2vertexgroup ** ) malloc( square->n_vgroup * sizeof( SIO2vertexgroup * ) );
 
// Init Each Vertex Group
square->_SIO2vertexgroup[0] = sio2VertexGroupInit("null");
square->_SIO2vertexgroup[0]->mode = GL_TRIANGLES;
 
// Set Material
strcpy(square->_SIO2vertexgroup[0]->mname, material->name);
square->_SIO2vertexgroup[0]->_SIO2material = material;
 
// Setup Triangle Indices(Faces)
square->_SIO2vertexgroup[0]->n_ind = 6;
square->_SIO2vertexgroup[0]->ind = ( unsigned short * ) malloc( square->_SIO2vertexgroup[0]->n_ind << 1 );
// The malloc here is sizeof(unsigned short)*n_ind = 6*2
 
square->_SIO2vertexgroup[0]->ind[0] = 0;
square->_SIO2vertexgroup[0]->ind[1] = 1;
square->_SIO2vertexgroup[0]->ind[2] = 2;
 
square->_SIO2vertexgroup[0]->ind[3] = 0;
square->_SIO2vertexgroup[0]->ind[4] = 2;
square->_SIO2vertexgroup[0]->ind[5] = 3;
 
// Store The Buffers(VBO) .. Send to openGL buffers
sio2ObjectGenId(square);
 
// Store the indices .. Send to openGL buffers (GL_ELEMENT_ARRAY_BUFFER)
sio2VertexGroupGenId(square->_SIO2vertexgroup[0]);
// Do this more if you have more vertex groups
 
// Set Location ( Centered in Landscape )
square->_SIO2transform->loc->x = 240 - square_width/2;
square->_SIO2transform->loc->y = 160 - square_height/2;
square->_SIO2transform->loc->z = 0;
 
// Bind Location as usual
sio2TransformBindMatrix(square->_SIO2transform);