Jump to content
  • Advertisement
Sign in to follow this  
MrDarkKnight

OpenGL Trying to write a OBJ loader

This topic is 2515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OLD POST

Hello guys
I'm trying to write a OBJ loader using c++, OpenGL and SFML. The problem is when i run the software nothing shows up on the screen. Just a black screen. not model show up.

any ideas ?



NEW POST

nvm guys i fixed it thanks.
here is a OBJ loader that i write by a help of my frined.
if you guys can look at the code is there anything i can do to make it better?
BTW anyone can take the code and use.


OBJModel.h

#pragma once
#include <SFML\Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>

struct Coordinate
{
float X, Y, Z;
Coordinate(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
};

struct Face
{
int FacesNumber, Vertex[4];
bool ItsQuad;

Face(int NumberOfFaces, int V1, int V2, int V3)
{
FacesNumber = NumberOfFaces;
Vertex[0] = V1;
Vertex[1] = V2;
Vertex[2] = V3;
ItsQuad = false;
}

Face(int NumberOfFaces, int V1, int V2, int V3, int V4)
{
FacesNumber = NumberOfFaces;
Vertex[0] = V1;
Vertex[1] = V2;
Vertex[2] = V3;
Vertex[3] = V4;
ItsQuad = true;
}
};

class ObjModel
{
public:
ObjModel();
~ObjModel();
void LoadFromFile(const char* FileName);
int Draw();

private:
char Buffer[256];

float x, y, z;
int V1, V2, V3, V4, F; // V = Vertex, F = Face
int Num;

std::vector<std::string*> Coord;
std::vector<Coordinate*> Vertex;
std::vector<Face*> Faces;
std::vector<Coordinate*> Normals;
};



OBJModel.cpp

#include "ObjModel.h"

ObjModel::ObjModel()
{
}

ObjModel::~ObjModel()
{
}

void ObjModel::LoadFromFile(const char* FileName)
{
std::ifstream ReadFile(FileName);
if (ReadFile.is_open())
{
while (ReadFile.good())
{
ReadFile.getline(Buffer, 256);
Coord.push_back(new std::string(Buffer));
}
}
else
std::cout << "Unable to open file";

for(int i=0; i<Coord.size(); i++)
{
if((*Coord)[0] == '#')
continue;

else if((*Coord)[0] == 'v' && ((*Coord)[1] == ' '))
{
sscanf(Coord->c_str(), "v %f %f %f", &x, &y, &z);
Vertex.push_back(new Coordinate(x, y, z));
}

else if((*Coord)[0] == 'v' && ((*Coord)[1] == 'n'))
{
sscanf(Coord->c_str(), "vn %f %f %f", &x, &y, &z);
Normals.push_back(new Coordinate(x, y, z));
}

else if((*Coord)[0] == 'f')
{
if(std::count(Coord->begin(), Coord->end(), ' ') == 4)
{
sscanf(Coord->c_str(), "f %d//%d %d//%d %d//%d %d//%d", &V1,&F, &V2,&F, &V3,&F, &V4,&F);
Faces.push_back(new Face(F, V1, V2, V3, V4));
}

else
{
sscanf(Coord->c_str(), "f %d//%d %d//%d %d//%d %d//%d", &V1,&F, &V2,&F, &V3,&F);
Faces.push_back(new Face(F, V1, V2, V3));
}
}
}



}


int ObjModel::Draw()
{

Num=glGenLists(1); //generate a uniqe
glNewList(Num,GL_COMPILE);

for(int i=0; i<Faces.size(); i++)
{
if(Faces->ItsQuad)
{
glBegin(GL_QUADS);

glNormal3f(Normals[Faces->FacesNumber -1]->X, Normals[Faces->FacesNumber -1]->Y, Normals[Faces->FacesNumber -1]->Z);

glVertex3f(Vertex[Faces->Vertex[0] -1]->X, Vertex[Faces->Vertex[0] -1]->Y, Vertex[Faces->Vertex[0] -1]->Z);
glVertex3f(Vertex[Faces->Vertex[1] -1]->X, Vertex[Faces->Vertex[1] -1]->Y, Vertex[Faces->Vertex[1] -1]->Z);
glVertex3f(Vertex[Faces->Vertex[2] -1]->X, Vertex[Faces->Vertex[2] -1]->Y, Vertex[Faces->Vertex[2] -1]->Z);
glVertex3f(Vertex[Faces->Vertex[3] -1]->X, Vertex[Faces->Vertex[3] -1]->Y, Vertex[Faces->Vertex[3] -1]->Z);

glEnd();
}

else
{
glBegin(GL_TRIANGLES);

glNormal3f(Normals[Faces->FacesNumber -1]->X, Normals[Faces->FacesNumber -1]->Y, Normals[Faces->FacesNumber -1]->Z);

glVertex3f(Vertex[Faces->Vertex[0] -1]->X, Vertex[Faces->Vertex[0] -1]->Y, Vertex[Faces->Vertex[0] -1]->Z);
glVertex3f(Vertex[Faces->Vertex[1] -1]->X, Vertex[Faces->Vertex[1] -1]->Y, Vertex[Faces->Vertex[1] -1]->Z);
glVertex3f(Vertex[Faces->Vertex[2] -1]->X, Vertex[Faces->Vertex[2] -1]->Y, Vertex[Faces->Vertex[2] -1]->Z);

glEnd();
}
}
glEndList();

for(int i=0; i<Coord.size(); i++)
delete Coord;

for(int i=0; i<Vertex.size(); i++)
delete Vertex;

for(int i=0; i<Faces.size(); i++)
delete Faces;

for(int i=0; i<Normals.size(); i++)
delete Normals;

return Num;
}


Engine.cpp

#include "Engine.h"

Engine::Engine()
{
}

Engine::~Engine()
{
}

void Engine::Init()
{
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,640.0/480.0,1.0,500.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
ObjModel.LoadFromFile("untitled.obj");
cube = ObjModel.Draw();
glEnable(GL_LIGHTING); //we enable lighting, to make the 3D object to 3D
glEnable(GL_LIGHT0);
float col[]={1.0,1.0,1.0,1.0}; //light color is white
glLightfv(GL_LIGHT0,GL_DIFFUSE,col);

}

void Engine::Events(sf::Event &Event)
{
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}

void Engine::Render()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
float pos[]={-1.0,1.0,-2.0,1.0}; //set the position
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glTranslatef(0.0,0.0,-5.0);
glRotatef(90,0.0,0.0,1.0);
glCallList(cube); //draw the 3D mesh

}

Share this post


Link to post
Share on other sites
Advertisement

[font="Arial,"][color="#000000"]I suggest using VBOs instead of display lists, display lists are very old[/font]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!