PDA

View Full Version : Reading 3D Model



Ameya
02-18-2012, 08:24 PM
Hi Experts,

I am completely new at Computer Graphics and has been exploring a lot in OpenGL, i am able to render basic primitives, translate and rotate them.

I am having trouble reading an input model file.

Object name = SQUARE
# triangles = 2
Material count = 1
ambient color 0.694 0.580 0.459
diffuse color 0.992 0.941 0.863
specular color 1.000 1.000 1.000
material shine 0.250
-- 3*[pos(x,y,z) normal(x,y,z) color_index] face_normal(x,y,z)
v0 -1.0 -1.0 -2.0 0.0 0.0 1.0 0
v1 1.0 -1.0 -2.0 0.0 0.0 1.0 0
v2 1.0 1.0 -2.0 0.0 0.0 1.0 0
face normal 0.0 0.0 1.0
v0 1.0 1.0 -2.0 0.0 0.0 1.0 0
v1 -1.0 1.0 -2.0 0.0 0.0 1.0 0
v2 -1.0 -1.0 -2.0 0.0 0.0 1.0 0
face normal 0.0 0.0 1.0

I tried this normal way using file operations

#include<GL/freeglut.h>
#include<GL/gl.h>
#include<math.h>
#include<fstream>
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
char line[256];
int tri,mat;
vector<double> ambient,diffuse,specular;

float material_shine;

FILE* infile;

infile=fopen("/home/ameya/Documents/CG/square.in","r");

series of fscanf operations to read float and integer values.

return 0;
}

1)Am getting garbage values.
2)How should i store the vertices of the sqaure.
3)Should i define a vector class to store each vector something like that.
4) or just store vertices in a normal array.

Actually i am completely clueless, in need of some direction.

Kindly help.

BionicBytes
02-19-2012, 12:27 PM
What you are trying to load is a OBJ model file.
Personally I think this may be a little beyond you at the moment unless you are very familiar with vertex arrays.
The data you get from this OBJ model can not be directly used without first restructuring it into multiple arrays (position, normal, texture coordinate, etc). This process is complicated enough without the additional complication of not knowing what the end result is expected to look like.

Ameya
02-21-2012, 12:37 AM
Hi ,

Even i guessed , can you please provide me with any reference tutorial for this

it will be of great help.

Regards,
Ameya

Alfonse Reinheart
02-21-2012, 12:58 AM
There's a reference tutorial called "Go use the Open Asset Import library (http://assimp.sourceforge.net/)."

Ameya
02-22-2012, 11:33 PM
Hi,

I wrote a program to read the square model

i defined structs for the vertices , color etc

struct color{
GLfloat x,y,z;
};
struct vertex{
GLfloat x,y,z;
};

struct normal{
GLfloat x,y,z;
};

#include<GL/freeglut.h>
#include<GL/gl.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include "read.h"

using namespace std;

vector<vertex> v;
vector<normal> n;
vector<facenormal> face;

void fileread(FILE *file){


vertex vec;
normal nor;
facenormal f;

char line[256];

int tri,mat;

color c;

vector<color> ambient,diffuse,specular;


float material_shine;




if(file == NULL)
cout << "Cant open file";

while(1){

int r = fscanf(file,"%s",line);//read first line
if(r==EOF)
break;

//cout << line;
if(line[0]=='#'){

fscanf(file," triangles = %d",&amp;tri);

}
if(line[0] == 'M'){
//cout << line;
fscanf(file," count = %d",&amp;mat);
}

if(line[0]=='a')
{cout << "yay";
cout << fscanf(file," color %f %f %f",&amp;c.x,&amp;c.y,&amp;c.z);
ambient.push_back(c);
}
if(line[0]=='d'){
cout << fscanf(file," color %f %f %f",&amp;c.x,&amp;c.y,&amp;c.z);
diffuse.push_back(c);
}

if(line[0]=='s'){
cout << fscanf(file," color %f %f %f",&amp;c.x,&amp;c.y,&amp;c.z);
specular.push_back(c);
}
if(line[0] == 'm')
cout<<fscanf(file," shine %f",&amp;material_shine);

if(line[0] == 'v')
{ cout << fscanf(file," %f %f %f %f %f %f",&amp;vec.x,&amp;vec.y,&amp;vec.z,&amp;nor.x,&amp;nor.y,&amp;nor.z);
v.push_back(vec);
n.push_back(nor);

}

if(line[0] == 'f')
{ cout<<fscanf(file," normal %f %f %f",&amp;f.x,&amp;f.y,&amp;f.z);
face.push_back(f);
}

}

//cout << endl<<"debug = "<<tri<<" "<<mat<< " "<<material_shine;

return;
}

i am getting error while passing the vector vertex V to glVertex3fv , cant convert const *float to const GLfloat

Kindly help