vertex loading and rendering functions:
Code:
void fFormat::LoadVerticies()
{
// Create a file pointer and load the model from a file of vertices
FILE *fp = fopen(FILE_NAME, "r");
// Make sure we opened the file correctly and it was found
if(!fp)
MessageBox(NULL, "Can't Open File", "Error", MB_OK);
// Create a temporary variable to hold what we read in from the file
CVector3 vTemp;
// Read in the number of vertices that are stored in the file
while(1)
{
// Read in a vertice and get the return value. If it's EOF we are done reading
int result = fscanf(fp, "%f %f %f\n", &vTemp.x, &vTemp.y, &vTemp.z);
// If we hit End Of File then we are done reading in the vertices, break
if(result == EOF)
break;
// Increase the vertice count
g_NumberOfVerts++;
}
// Allocate the needed memory to hold the vertices
pp_3DF = new CVector3 [ g_NumberOfVerts ];
// Go back to the beginning of the file so we can store the vertices now
rewind(fp);
// Create a counter for the index of the g_vWorld[] array
int index = 0;
// Read in the vertices that are stored in the file
for(int i = 0; i < g_NumberOfVerts; i++)
{
// Read in the current vertice and at the end, then add 1 to the index
fscanf(fp, "%f %f %f\n", &pp_3DF[ index ].x,
&pp_3DF[ index ].y,
&pp_3DF[ index ].z);
index++; // Increase our index for the vertex list
}
// Close our file because we are done
fclose(fp);
}
void fFormat::RenderFile()
{
glBindTexture(GL_TEXTURE_2D, g_Texture[0]);
glBegin(GL_TRIANGLES); // This is our BEGIN to draw the polys
// Go through all the vertices and draw them
for(int i = 0; i < g_NumberOfVerts; i += 3)
{
glTexCoord2f(pp_3DF[i].x, pp_3DF[i].y); glVertex3f(pp_3DF[i].x, pp_3DF[i].y, pp_3DF[i].z);
glTexCoord2f(pp_3DF[i+1].x, pp_3DF[i+1].y); glVertex3f(pp_3DF[i+1].x, pp_3DF[i+1].y, pp_3DF[i+1].z);
glTexCoord2f(pp_3DF[i+2].x, pp_3DF[i+2].y); glVertex3f(pp_3DF[i+2].x, pp_3DF[i+2].y, pp_3DF[i+2].z);
}
glEnd();
}
rendering function
Code:
void OGLrender::Render()
{
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gCam.Look();
_glDynaLight->DefaultLight();
//_glGeometry->DrawGrid(300);
//glRotatef(rotation, 1, 1, 1);
//_glGeometry->DrawSolidCube(0, 0, 0, 1.0, 1.0, 1.0); //Draw solid cube with default position and size. Will become dependant on user input.
_glskyBox->RenderSkyBox(0, 0, 0, 750, 750, 370);
_glTerrain->DrawTerrain();
glScalef(0.04, 0.04, 0.04);
glRotatef(225, 0, 1, 0);
glTranslatef(0, 0, -10);
_gl3DF->RenderFile();
_gl2D->DrawCrosshair(gCam.m_vView.x, gCam.m_vView.y, gCam.m_vView.z);
//rotation += 0.1;
}