PDA

View Full Version : Opengl import 3d model problem



Kieran
04-13-2012, 03:33 AM
Hey im ive been working on importing 3d models from 3ds max and blender as a obj file as they are simple and texted based. Im wanting to be able to read all the vertices and faces into my prgram and display my models using opengl. I am able to read in all my data into my program from one of my model txt files but then when i come to draw them in 3D space the triangles do not draw correctly. triangular model class ive been working on can anyone give us a hand?? or point me in a better direction.

#pragma once
#include "Model.h"
#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <iostream>
using namespace std;
class Triangular
{
public:
struct point3D {
float x;
float y;
float z;
};
struct colour{
float r;
float g;
float b;
};

struct polygon {
int a;
int b;
int c;
int d;
};
int NUM_VERTS;
int NUM_COL;
int NUM_POLY;
point3D * vertices;
colour * colours;
polygon * indices;
Triangular(string fName)
{
Triangular::readFile(fName);
}
virtual ~Triangular(void)
{
delete vertices;
delete colours;
delete indices;
}

int readFile(string fileName)
{
ifstream inFile;
inFile.open(fileName.c_str());
if (!inFile.good())
{
cerr << "Can't open file" << endl;
NUM_POLY = 0;
return 1;
}
inFile >> NUM_VERTS;
vertices = new point3D[NUM_VERTS];
for (int i=0; i < NUM_VERTS; i++)
{
inFile >> vertices[i].x;
inFile >> vertices[i].y;
inFile >> vertices[i].z;
string temp;
inFile >> temp;
cout << vertices[i].x << " " << vertices[i].y << " " << vertices[i].z << "\n";
}

/*inFile >> NUM_COL;
colours = new colour[NUM_COL];

for (int i=0; i < NUM_COL; i++)
{
inFile >> colours[i].r;
inFile >> colours[i].g;
inFile >> colours[i].b;
}*/

inFile >> NUM_POLY;
indices = new polygon[NUM_POLY];
for(int i=0; i < NUM_POLY; i++)
{
inFile >> indices[i].a;
inFile >> indices[i].b;
inFile >> indices[i].c;
string temp;
inFile >> temp;
cout << indices[i].a << " " << indices[i].b << " " << indices[i].c << "\n";
}

/*Reading 3ds max polys
for (int i=0; i < NUM_POLY; i++)
{
string temp;
inFile >> temp;
string ind;
if (temp != "f")
{
bool first = true;
ind = "";
for(int j=0; j < temp.length(); j++)
{
if(temp.at(j) != '/')
{
ind += temp.at(j);
}
else if(temp.at(j) = '/')
{
if(first)
{
indices[i].a = atoi(ind.c_str());
first = false;
}
else
{
indices[i].b = atoi(ind.c_str());
}
ind = "";
}
}
indices[i].c = atoi(ind.c_str());
cout << indices[i].a << " " << indices[i].b << " " << indices[i].c << "\n";
}
else{i--;}
}*/
inFile.close();
return 0;
}
void drawPolygon(int a,int b,int c){
glVertex3fv(&amp;vertices[a].x);
glVertex3fv(&amp;vertices[B].x);
glVertex3fv(&amp;vertices[c].x);

}
void draw(){glBegin(GL_TRIANGLES);
for (int i = 0; i < NUM_POLY; i++){
glColor3f(6.0, 0.0, 0.0);
drawPolygon(indices[i].a, indices[i].b, indices[i].c);
}glEnd();
}
};

tonyo_au
04-14-2012, 12:24 AM
Are you exporting your .OBJ as triangles only? .OBJ faces can be polygons