PDA

View Full Version : Loading OBJ and MTL files



Rodrigo
08-26-2010, 07:12 AM
Hi guys.
I was searching for a way of loading an imagen with textures from an obj file and its mtl.
I found the library GLM written in C that seems to do those things.
I actually made it work to load the model (after changing some parts of the code), but I can't make it work for the texture.
GLM: http://devernay.free.fr/hacks/glm/

Does anybody know how to work with this library?
Or would it be better do write my own code for loading model and textures?

marshats
08-26-2010, 06:50 PM
I download and compiled your code. It seemed to work with texturing on my Ubuntu 10.02 box. What problem are you having exactly?

Rodrigo
08-26-2010, 07:33 PM
My only problem is to load the texture.
It's only drawing the polygons.

Did it work at first time? I didn't work for me, I had to change parts of the code.
But i'm running on windows. Would it be the problem?

I downloaded the code, created a project in devcpp to build glm.a library and then linked that library to my opengl project.

marshats
08-26-2010, 08:45 PM
The example code worked fine without any changes on Ubuntu. Note that some of the files did not have texture coords in them but the OBJ files that did showed the textures fine. The ones that didn't probably were saved to disk without specifying the texture cooridinates -- GLM will not auto generate texture coord, they have to already exist in the OBJ file.

Rodrigo
08-27-2010, 05:52 AM
Damn it, I tried the example and it worked.

But it still doesn't work in my project. My problem is that I started a little project to learn opengl and then, when I thought I had more knowledge to go on, I started looking for something to load those obj files exported from blender and I found glm.
The point is that I tried to make it work directly in my project and things didn't go well =[.

Here is my code. Maybe someone can point where the problem is:




#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <windows.h>
#include <gl/glut.h>
#include <glm-0.3.1/glm/glm.h>
#include <glm-0.3.1/glm/glmint.h>

#define PTS 10
#define LIMITES 200
#define LIMITEV 100
#define LIMITEA 20
#define LIMITEM 2000
#define RELOGIO 0.001
#define FORCAG 100

//CONST_GRAV 6,673 * exp10(-11) m^3 kg^-1 s^-2

struct ponto{
double x,y,z;
double xtmp,ytmp,ztmp;
double vx,vy,vz;
double vxtmp,vytmp,vztmp;
double ax,ay,az;
double fx,fy,fz;
double massa;
};

struct meshPts{
float *x,*y,*z;
int numPts;
};

struct ponto p[PTS];
struct meshPts mp;
float cor1[PTS],cor2[PTS],cor3[PTS];
FILE *fp;
GLMmodel *model;

GLfloat angle, fAspect;
float angle=0;

// Função callback chamada para fazer o desenho
void Desenha(void)
{
int i;

glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();

// Limpa a janela de visualização com a cor de fundo especificada
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


for(i=0;i<PTS;i++)
{
glColor3f(cor1[i], cor2[i], cor3[i]);

glPushMatrix(); // salvar as coordenadas correntes

glTranslatef(p[i].x,p[i].y,p[i].z);

glutSolidSphere(10.0,20,20);

glPopMatrix(); // volta para o sistema de coordenadas
}

//Drawing the model

glPushMatrix(); // salvar as coordenadas correntes

angle += 1.0;
glTranslatef(0.0,0.0,500.0);
glRotatef(angle,1.0,0.0,0.0);
//HERE IS WHERE I DRAW MY OBJ
glmDraw(model,GLM_SMOOTH|GLM_TEXTURE|GLM_MATERIAL) ;

glPopMatrix();

glutSwapBuffers();
}



// Inicializa parâmetros de rendering
void Inicializa (void)
{
GLfloat luzAmbiente[4]={0.2,0.2,0.2,1.0};
GLfloat luzDifusa[4]={0.7,0.7,0.7,1.0}; // "cor"
GLfloat luzEspecular[4]={1.0, 1.0, 1.0, 1.0};// "brilho"
GLfloat posicaoLuz[4]={0.0, 400.0, 0.0, 1.0};

// Capacidade de brilho do material
GLfloat especularidade[4]={0.7,0.5,0.5,0.0};
GLint especMaterial = 60;

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

// Habilita o modelo de colorização de Gouraud
glShadeModel(GL_SMOOTH);

// Define a refletância do material
glMaterialfv(GL_FRONT,GL_SPECULAR, especularidade);
// Define a concentração do brilho
glMateriali(GL_FRONT,GL_SHININESS,especMaterial);

// Ativa o uso da luz ambiente
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, luzAmbiente);

// Define os parâmetros da luz de número 0
glLightfv(GL_LIGHT0, GL_AMBIENT, luzAmbiente);
glLightfv(GL_LIGHT0, GL_DIFFUSE, luzDifusa );
glLightfv(GL_LIGHT0, GL_SPECULAR, luzEspecular );
glLightfv(GL_LIGHT0, GL_POSITION, posicaoLuz );

// Habilita a definição da cor do material a partir da cor corrente
glEnable(GL_COLOR_MATERIAL);
//Habilita o uso de iluminação
glEnable(GL_LIGHTING);
// Habilita a luz de número 0
glEnable(GL_LIGHT0);
// Habilita o depth-buffering
glEnable(GL_DEPTH_TEST);


angle=60;
}

// Função usada para especificar o volume de visualização
void EspecificaParametrosVisualizacao(void)
{
// Especifica sistema de coordenadas de projeção
glMatrixMode(GL_PROJECTION);
// Inicializa sistema de coordenadas de projeção
glLoadIdentity();

// Especifica a projeção perspectiva
gluPerspective(angle,fAspect,0.1,1000.0);

// Especifica sistema de coordenadas do modelo
glMatrixMode(GL_MODELVIEW);
// Inicializa sistema de coordenadas do modelo
glLoadIdentity();

// Especifica posição do observador e do alvo
gluLookAt(0,0,600, 0,0,0, 0,1,0);
}

// Função callback chamada pela GLUT a cada intervalo de tempo
// (a window não está sendo redimensionada ou movida)
void Timer(int value)
{
int i,j;
double ftmp;

for(i=0;i<PTS;i++){

p[i].fx = p[i].fy = p[i].fz = 0;

for(j=0;j<PTS;j++){
double dij;
dij = sqrt((p[j].x - p[i].x)*(p[j].x - p[i].x) +
(p[j].y - p[i].y)*(p[j].y - p[i].y) +
(p[j].z - p[i].z)*(p[j].z - p[i].z));
if(i!=j){
//ftmp = 6,673 * exp10(-11) * p[i].massa * p[j].massa/dij;
ftmp = FORCAG * p[i].massa * p[j].massa/dij;
p[i].fx += (p[j].x - p[i].x)*ftmp/dij;
p[i].fy += (p[j].y - p[i].y)*ftmp/dij;
p[i].fz += (p[j].z - p[i].z)*ftmp/dij;
}
}
p[i].ax = p[i].fx/p[i].massa;
p[i].ay = p[i].fy/p[i].massa;
p[i].az = p[i].fz/p[i].massa;
}

for(i=0;i<PTS;i++){
p[i].vxtmp = p[i].vx + p[i].ax*RELOGIO;
p[i].vytmp = p[i].vy + p[i].ay*RELOGIO;
p[i].vztmp = p[i].vz + p[i].az*RELOGIO;

p[i].xtmp = p[i].x + p[i].vx*RELOGIO + p[i].ax*RELOGIO*RELOGIO/2;
p[i].ytmp = p[i].y + p[i].vy*RELOGIO + p[i].ay*RELOGIO*RELOGIO/2;
p[i].ztmp = p[i].z + p[i].vz*RELOGIO + p[i].az*RELOGIO*RELOGIO/2;
}

for(i=0;i<PTS;i++){
p[i].vx = p[i].vxtmp;
p[i].vy = p[i].vytmp;
p[i].vz = p[i].vztmp;

p[i].x = p[i].xtmp;
p[i].y = p[i].ytmp;
p[i].z = p[i].ztmp;
}

// Redesenha as esferas com as novas coordenadas
glutPostRedisplay();
glutTimerFunc(1,Timer, 1);
}

// Função callback chamada quando o tamanho da janela é alterado
void AlteraTamanhoJanela(GLsizei w, GLsizei h)
{
// Para previnir uma divisão por zero
if ( h == 0 ) h = 1;

// Especifica o tamanho da viewport
glViewport(0, 0, w, h);

// Calcula a correção de aspecto
fAspect = (GLfloat)w/(GLfloat)h;

EspecificaParametrosVisualizacao();
}

// Programa Principal
int main(void)
{

int i;

model=glmReadOBJ("cone.obj");
glmVertexNormals(model,180.0,0);

for(i=0;i<PTS;i++){
p[i].x = (rand()%(2*LIMITES+1)) - LIMITES;
p[i].y = (rand()%(2*LIMITES+1)) - LIMITES;
p[i].z = (rand()%(2*LIMITES+1)) - LIMITES;

p[i].vx = (rand()%(2*LIMITEV+1)) - LIMITEV;
p[i].vy = (rand()%(2*LIMITEV+1)) - LIMITEV;
p[i].vz = (rand()%(2*LIMITEV+1)) - LIMITEV;

p[i].massa = (rand()%(LIMITEM)) + 200;

cor1[i] = rand() % 50;
cor2[i] = rand() % 50;
cor3[i] = rand() % 50;

cor1[i] = cor1[i]/50;
cor2[i] = cor2[i]/50;
cor3[i] = cor3[i]/50;

}

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000,800);
//glutInitWindowPosition(10,10);
glutCreateWindow("Interação Gravitacional");
glutDisplayFunc(Desenha);
glutReshapeFunc(AlteraTamanhoJanela);
glutTimerFunc(1, Timer, 1);
Inicializa();
glutMainLoop();

return 0;
}

Rodrigo
08-27-2010, 09:58 AM
Problem Solved!
I was reading the obj too soon... =S
Reading it a few lines deeper made it work.
Thanks!!

vinucho
10-09-2012, 04:07 AM
Rodrigo,

Você poderia me passar seu e-mail?

Estou precisando de ajuda no carregamento de OBJ + MTL.

Grato,
Vinicius.

thokra
10-09-2012, 04:52 AM
vinucho: Even if you should not be inclined to post anymore, the language of the OpenGL discussion board is English.