Use Triangle strips, and/or Triangle fans, 3D cards are optimized for strips and fans…
Here’s a start on one that i have been doing… it draws a cube, but two surfaces, there texture Coords are weird…
Im using my OpenGL windows toolkit, which should be online in a few days… Heres the full source code…
#pragma hdrstop
#include <GL/GLWin.h>
#ifdef BORLANDC
#include <condefs.h>
#endif /* End BROLANDC */
#include
//---------------------------------------------------------------------------
USERC(“resource.rc”);
//---------------------------------------------------------------------------
#define WinMain /* C++Builder */
//---------------------------------------------------------------------------
#define xyz 30.0
#define uv 1.0
#define u0 0.0
//---------------------------------------------------------------------------
static LPCTSTR AppName = “Hello World!”;
static GLfloat Cube = {
-xyz, xyz, -xyz,
xyz, xyz, -xyz,
xyz, -xyz, -xyz,
-xyz, -xyz, -xyz,
-xyz, xyz, xyz,
xyz, xyz, xyz,
xyz, -xyz, xyz,
-xyz, -xyz, xyz
};
//---------------------------------------------------------------------------
static GLfloat texCoords = {
uv, uv, u0, uv, u0, u0, uv, u0,
uv, u0, u0, u0, u0, uv, uv, uv,
u0, uv, uv, uv, uv, u0, u0, u0,
uv, u0, u0, u0, u0, uv, uv, uv,
uv, uv, u0, u0, u0, uv, uv, u0,
u0, uv, uv, uv, uv, u0, u0, u0
};
//---------------------------------------------------------------------------
static GLuint DrawCube = {
4, 5, 6, 7,
1, 2, 6, 5,
0, 1, 5, 4,
1, 0, 3, 2,
0, 4, 7, 3,
7, 6, 2, 3
};
//---------------------------------------------------------------------------
enum Coord{ X, Y, Z, W };
static GLfloat Rot = { 0.0, 0.0, 0.0 };
static float animspeed = 20;
GLuint Textures[ 6 ];
char* bitmaps[ 6 ] = {
“xray.bmp”,
“lightning.bmp”,
“fall.bmp”,
“coins.bmp”,
“sand.bmp”,
“eye.bmp”
};
//---------------------------------------------------------------------------
void Init( void )
{
for( int t = 0; t < 6; t++ )
Textures[ t ] = glWin_LoadBMPTexture( bitmaps[ t ] );
glEnable( GL_TEXTURE_2D );
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glFrontFace( GL_CW );
glClearColor( 0.2f, 0.2f, 0.7f, 0.0f );
glInterleavedArrays( GL_V3F, 0, Cube );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer( 2, GL_FLOAT, 0, texCoords );
}
//---------------------------------------------------------------------------
#pragma argsused /* Stop the x and y not used warnings */
void Keydown( UINT key, int x, int y )
{
switch( key )
{
case VK_ESCAPE:
glWin_Close();
break;
}
}
//---------------------------------------------------------------------------
void Resize( GLsizei w, GLsizei h )
{
GLfloat aspect;
if( h == 0 ) h = 1;
aspect = ( GLfloat )w / ( GLfloat )h;
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0f, aspect, 60, 300 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0f, 20.0f, -200.0f );
}
//---------------------------------------------------------------------------
void Draw( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef( Rot[ X ], 1.0f, 0.0f, 0.0f );
glRotatef( Rot[ Y ], 0.0f, 1.0f, 0.0f );
glRotatef( Rot[ Z ], 0.0f, 0.0f, 1.0f );
for( int t = 0, offset = 0; t < 6; t++, offset += 4 )
{
glBindTexture( GL_TEXTURE_2D, Textures[ t ] );
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_INT, &DrawCube[ offset ] );
}
glPopMatrix();
glFlush();
}
//---------------------------------------------------------------------------
#define CLAMP_TO( x, y ) if( x > y ){ x -= y; }
void Animate( float milliseconds, BOOL& reset )
{
if( milliseconds > animspeed )
{
Rot[ X ] += 2.0f;
Rot[ Y ] += 8.0f;
Rot[ Z ] += 1.0f;
CLAMP_TO( Rot[ X ], 360.0f );
CLAMP_TO( Rot[ Y ], 360.0f );
CLAMP_TO( Rot[ Z ], 360.0f );
reset = TRUE;
}
}
//---------------------------------------------------------------------------
void Destroy( void )
{
glDeleteTextures( 6, Textures );
}
//---------------------------------------------------------------------------
GLWINENTRY GLWINDCLASS RegisterGLWindowClass( LPSTR params )
{
string commands = string( params );
GLWIN_MODE mode = GLWIN_WINDOW;
if( commands.find( "-fullscreen", 0 ) != string::npos )
mode = GLWIN_FULLSCREEN;
if( commands.find( "-fast", 0 ) != string::npos )
animspeed = 1.0;
GLWINDCLASS glwc = { AppName,
mode,
640, 480,
0, 0,
Init,
Keydown,
NULL,
NULL,
NULL,
Resize,
Draw,
Animate,
Destroy };
return glwc;
}
//---------------------------------------------------------------------------
I am bamboozled with the Vertex Array texture coords… No matter what i do, they still look weird…