One of new methods in OpenGl 1.1 is that it can allow the
developer to set the most important data , such as texture, colors,
normals, vertexs, in an interleaved data structure body and build one vertex
array. Besides that, an indices array is necessary for rendering. The two key
functions in this realization are “glInterleavedArrays” and
“glDrawArrays”. The definition of vertex structure for “glInterleavedArrays”
function depends on what format will be used in “glInterleavedArrays”. The
definition of the indices array also bases on what type of rendering index in
“glDrawArrays” The following is the vertex structure and indices (with C++
template) in this demo.
template <class Typeclass vertex
{
public:
Type m_texture[2];
Type m_normal[3];
Type m_vertex[3];
public:
vertex(){};
~vertex(){};
void SetTexture(Type texture1, Type texture2)
{
m_texture[0] = texture1;
m_texture[1] = texture2;
};
void GetTexture(Type* texture1, Type* texture2)
{
*texture1 = m_texture[0];
*texture2 = m_texture[1];
};
void SetNormal(Type normal1, Type normal2, Type normal3)
{
m_normal[0] = normal1;
m_normal[1] = normal2;
m_normal[2] = normal3;
};
void GetNormal(Type* normal1, Type* normal2, Type* normal3)
{
*normal1 = m_normal[0];
*normal2 = m_normal[1];
*normal3 = m_normal[2];
};
void SetVertex(Type vertex1, Type vertex2, Type vertex3)
{
m_vertex[0] = vertex1;
m_vertex[1] = vertex2;
m_vertex[2] = vertex3;
};
void GetVertex(Type* vertex1, Type* vertex2, Type* vertex3)
{
*vertex1 = m_vertex[0];
*vertex2 = m_vertex[1];
*vertex3 = m_vertex[2];
};
};
typedef vertex<float fVertex;
template <class Typeclass indices
{
public:
Type m_indice[2];
public:
indices(){};
~indices(){};
void SetIndices(Type indice1, Type indice2)
{
m_indice[0] = indice1;
m_indice[1] = indice2;
};
void GetIndices(Type* indice1, Type* indice2)
{
*indice1 = m_indice[0];
*indice2 = m_indice[1];
};
};
typedef indices<unsigned int uiIndice;
The rendering implementation in code is
class CSGObject : public CObject
{
private:
…………
fVertex* m_vertex;
uiIndice* m_indice;
…………
float m_x;
float m_y;
float m_z;
float m_sx;
float m_sy;
float m_sz;
float m_rx;
float m_ry;
float m_rz;
BOOL m_bWire;
public:
…………
void DrawObject(void);
…………
};
void CSGObject::DrawObject(void)
{
int k;
int i;
if(m_bWire)
{
m_dvalue.SetValue(m_dMemvalue.m_dX*4, m_dMemvalue.m_dY*4, m_dMemvalue.m_dZ*4);
m_size.SetValue((int)(m_Memsize.m_row*2.5), (int)(m_Memsize.m_col*2.5));
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else
{
m_dvalue.SetValue(m_dMemvalue.m_dX, m_dMemvalue.m_dY, m_dMemvalue.m_dZ);
m_size.SetValue(m_Memsize.m_row*10, m_Memsize.m_col*10);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if(m_Oldsize.m_row != m_size.m_row || m_Oldsize.m_col != m_size.m_col)
{
if(m_vertex)
delete []m_vertex;
if(m_indice)
delete []m_indice;
m_vertex = new fVertex[(m_size.m_row+1) * (m_size.m_col+1)];
m_indice = new uiIndice[m_size.m_row * (m_size.m_col+1)];
(*FuncIndice)(m_indice, m_size);
m_Oldsize.m_row = m_size.m_row;
m_Oldsize.m_col = m_size.m_col;
}
(*FuncVertex)(m_vertex, m_size, m_factor, m_dvalue);
SetTexture();
glInterleavedArrays(GL_T2F_N3F_V3F, 0, m_vertex);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glMatrixMode(GL_MODELVIEW);
glTranslatef(m_x, m_y, m_z);
glScalef(m_sx, m_sy, m_sz);
glRotatef(m_rx, 1.0, 0.0, 0.0);
glRotatef(m_ry, 0.0, 1.0, 0.0);
glRotatef(m_rz, 0.0, 0.0, 1.0);
for(i=0; i< m_size.m_row; i++)
{
k = i*(m_size.m_col+1);
glDrawElements(GL_TRIANGLE_STRIP, 2*(m_size.m_col+1), GL_UNSIGNED_INT, &m_indice[k]);
}
glRotatef((float)(-1.0 * m_rz), 0.0, 0.0, 1.0);
glRotatef((float)(-1.0 * m_ry), 0.0, 1.0, 0.0);
glRotatef((float)(-1.0 * m_rx), 1.0, 0.0, 0.0);
glScalef((float)(1.0f/m_sx), (float)(1.0f/m_sy), (float)(1.0f/m_sz));
glTranslatef((float)(-1.0 * m_x), (float)(-1.0 * m_y), (float)(-1.0 * m_z));
}