Reading 3D Model

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.

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.

Hi ,

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

it will be of great help.

Regards,
Ameya

There’s a reference tutorial called “Go use the Open Asset Import library.”

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&lt;color&gt; ambient,diffuse,specular;


float material_shine;




if(file == NULL)
	cout &lt;&lt; "Cant open file";

while(1){

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

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

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

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

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

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

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

	  		 }

        if(line[0] == 'f')
        {	cout&lt;&lt;fscanf(file," normal %f %f %f",&f.x,&f.y,&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