Hello, I have a problem, I can’t properly view my model that I have loaded, I dont know if it is the lighting or what the problem may be. I have attached the .OBJ file that im using
#include <allegro.h>
#include <alleggl.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
/***************************************************************************
OBJ Loading
***************************************************************************/
class Model_OBJ
{
public:
Model_OBJ();
float* Model_OBJ::calculateNormal(float* coord1,float* coord2,float* coord3 );
int Model_OBJ::Load(char *filename); // Loads the model
void Model_OBJ::Draw(); // Draws the model on the screen
void Model_OBJ::Release(); // Release the model
float* normals; // Stores the normals
float* Faces_Triangles; // Stores the triangles
float* vertexBuffer; // Stores the points which make the object
long TotalConnectedPoints; // Stores the total number of connected verteces
long TotalConnectedTriangles; // Stores the total number of connected triangles
};
#define POINTS_PER_VERTEX 3
#define TOTAL_FLOATS_IN_TRIANGLE 9
using namespace std;
Model_OBJ::Model_OBJ()
{
this->TotalConnectedTriangles = 0;
this->TotalConnectedPoints = 0;
}
float* Model_OBJ::calculateNormal( float *coord1, float *coord2, float *coord3 )
{
/* calculate Vector1 and Vector2 */
float va[3], vb[3], vr[3], val;
va[0] = coord1[0] - coord2[0];
va[1] = coord1[1] - coord2[1];
va[2] = coord1[2] - coord2[2];
vb[0] = coord1[0] - coord3[0];
vb[1] = coord1[1] - coord3[1];
vb[2] = coord1[2] - coord3[2];
/* cross product */
vr[0] = va[1] * vb[2] - vb[1] * va[2];
vr[1] = vb[0] * va[2] - va[0] * vb[2];
vr[2] = va[0] * vb[1] - vb[0] * va[1];
/* normalization factor */
val = sqrt( vr[0]*vr[0] + vr[1]*vr[1] + vr[2]*vr[2] );
float norm[3];
norm[0] = vr[0]/val;
norm[1] = vr[1]/val;
norm[2] = vr[2]/val;
return norm;
}
int Model_OBJ::Load(char* filename)
{
string line;
ifstream objFile (filename);
if (objFile.is_open()) // If obj file is open, continue
{
objFile.seekg (0, ios::end); // Go to end of the file,
long fileSize = objFile.tellg(); // get file size
objFile.seekg (0, ios::beg); // we'll use this to register memory for our 3d model
vertexBuffer = (float*) malloc (fileSize); // Allocate memory for the verteces
Faces_Triangles = (float*) malloc(fileSize*sizeof(float)); // Allocate memory for the triangles
normals = (float*) malloc(fileSize*sizeof(float)); // Allocate memory for the normals
int triangle_index = 0; // Set triangle index to zero
int normal_index = 0; // Set normal index to zero
while (! objFile.eof() ) // Start reading file data
{
getline (objFile,line); // Get line from file
if (line.c_str()[0] == 'v') // The first character is a v: on this line is a vertex stored.
{
line[0] = ' '; // Set first character to 0. This will allow us to use sscanf
sscanf(line.c_str(),"%f %f %f ", // Read floats from the line: v X Y Z
&vertexBuffer[TotalConnectedPoints],
&vertexBuffer[TotalConnectedPoints+1],
&vertexBuffer[TotalConnectedPoints+2]);
TotalConnectedPoints += POINTS_PER_VERTEX; // Add 3 to the total connected points
}
if (line.c_str()[0] == 'f') // The first character is an 'f': on this line is a point stored
{
line[0] = ' '; // Set first character to 0. This will allow us to use sscanf
int vertexNumber[4] = { 0, 0, 0 };
sscanf(line.c_str(),"%i%i%i", // Read integers from the line: f 1 2 3
&vertexNumber[0], // First point of our triangle. This is an
&vertexNumber[1], // pointer to our vertexBuffer list
&vertexNumber[2] ); // each point represents an X,Y,Z.
vertexNumber[0] -= 1; // OBJ file starts counting from 1
vertexNumber[1] -= 1; // OBJ file starts counting from 1
vertexNumber[2] -= 1; // OBJ file starts counting from 1
/********************************************************************
* Create triangles (f 1 2 3) from points: (v X Y Z) (v X Y Z) (v X Y Z).
* The vertexBuffer contains all verteces
* The triangles will be created using the verteces we read previously
*/
int tCounter = 0;
for (int i = 0; i < POINTS_PER_VERTEX; i++)
{
Faces_Triangles[triangle_index + tCounter ] = vertexBuffer[3*vertexNumber[i] ];
Faces_Triangles[triangle_index + tCounter +1 ] = vertexBuffer[3*vertexNumber[i]+1 ];
Faces_Triangles[triangle_index + tCounter +2 ] = vertexBuffer[3*vertexNumber[i]+2 ];
tCounter += POINTS_PER_VERTEX;
}
/*********************************************************************
* Calculate all normals, used for lighting
*/
float coord1[3] = { Faces_Triangles[triangle_index], Faces_Triangles[triangle_index+1],Faces_Triangles[triangle_index+2]};
float coord2[3] = {Faces_Triangles[triangle_index+3],Faces_Triangles[triangle_index+4],Faces_Triangles[triangle_index+5]};
float coord3[3] = {Faces_Triangles[triangle_index+6],Faces_Triangles[triangle_index+7],Faces_Triangles[triangle_index+8]};
float *norm = this->calculateNormal( coord1, coord2, coord3 );
tCounter = 0;
for (int i = 0; i < POINTS_PER_VERTEX; i++)
{
normals[normal_index + tCounter ] = norm[0];
normals[normal_index + tCounter +1] = norm[1];
normals[normal_index + tCounter +2] = norm[2];
tCounter += POINTS_PER_VERTEX;
}
triangle_index += TOTAL_FLOATS_IN_TRIANGLE;
normal_index += TOTAL_FLOATS_IN_TRIANGLE;
TotalConnectedTriangles += TOTAL_FLOATS_IN_TRIANGLE;
}
}
objFile.close(); // Close OBJ file
}
else
{
cout << "Unable to open file";
}
return 0;
}
void Model_OBJ::Release()
{
free(this->Faces_Triangles);
free(this->normals);
free(this->vertexBuffer);
}
void Model_OBJ::Draw()
{
glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex arrays
glEnableClientState(GL_NORMAL_ARRAY); // Enable normal arrays
glVertexPointer(3,GL_FLOAT, 0,Faces_Triangles); // Vertex Pointer to triangle array
glNormalPointer(GL_FLOAT, 0, normals); // Normal pointer to normal array
glDrawArrays(GL_TRIANGLES, 0, TotalConnectedTriangles); // Draw the triangles
glDisableClientState(GL_VERTEX_ARRAY); // Disable vertex arrays
glDisableClientState(GL_NORMAL_ARRAY); // Disable normal arrays
}
/***************************************************************************
* Program code
***************************************************************************/
Model_OBJ obj;
float g_rotation;
using namespace std;
GLfloat rtri; // Angle For The Triangle ( NEW )
void Init()
{
// Start up Allegro and AllegroGL systems
allegro_init();
install_allegro_gl();
install_keyboard();
install_mouse();
allegro_gl_clear_settings();
// Set some AllegroGL options
allegro_gl_set( AGL_COLOR_DEPTH, 32 );
allegro_gl_set( AGL_Z_DEPTH, 8 );
allegro_gl_set( AGL_SUGGEST, AGL_COLOR_DEPTH | AGL_Z_DEPTH );
// Set up a suitable viewing window
set_color_depth(32);
allegro_message("Passed Initial Color Depth");
set_gfx_mode(GFX_OPENGL_WINDOWED, 800, 600, 0, 0); //GFX_OPENGL_WINDOWED
allegro_message("Passed Initial Window");
// Set the background color to black (R,G,B,A)
glClearColor(0, 0, 0, 0);
}
int main()
{
Init();
obj.Load("untitled.obj");
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, 800, 600);
GLfloat aspect = (GLfloat) 800 / 600;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glShadeModel( GL_SMOOTH );
glClearColor( 0.0f, 0.1f, 0.0f, 0.5f );
glClearDepth( 1.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glDepthFunc( GL_LEQUAL );
glEnable( GL_DEPTH_TEST );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
while(!key[KEY_ESC])
{
// Clear the screen
// Clear the screen and depth buffer:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glRotatef(g_rotation,0,1,0);
glRotatef(90,0,1,0);
g_rotation++;
obj.Draw();
glPopMatrix();
// Flush the rendering pipeline:
glFlush();
// Flip the backbuffer to the Allegro screen:
allegro_gl_flip();
}
return 0;
}
END_OF_MAIN();