PDA

View Full Version : I want to draw a mesh from a file .I have problems.



spworld
10-30-2003, 10:14 AM
The mesh I want t draw is simply a cube(as a start).Its data are kept in a file and is shown below.The code is this :
//code==============================
#include <GL/glut.h>
#include <GL/GL.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
//----------------------
class GLintpoint{
public:
GLint x,y;
};//end class
//---------------------
class Point3{
public:
GLint x,y,z;
};//end class
//---------------------
class Vector3{
public:
GLint x,y,z;
};//end class
//---------------------
GLintpoint CP;
void moveto(GLint x,GLint y) {
CP.x=x;CP.y=y;
}//end moveto

class VertexID{
public :
int vertIndex;
int normIndex;
};
//------------------------------
class Face{
public :
int nVerts;
VertexID * vert;
Face(){nVerts = 0;vert=NULL;}
~Face(){delete[] vert;nVerts = 0;}
};
//------------------------------
class Mesh{
private:
int numVerts;
Point3 *pt;
int numNormals;
Vector3 *norm;
int numFaces;
Face *face;

public :
Mesh(){
numVerts=numNormals=numFaces=0;
pt=NULL;norm=NULL;face=NULL;
};
~Mesh(){};
int readFile(char * filename){
fstream infile;
infile.open(filename,ios::in);
if(infile.fail()) return -1;
if(infile.eof()) return -1;
infile >> numVerts >>numNormals >>numFaces;
pt =new Point3[numVerts];
norm = new Vector3[numNormals];
face = new Face[numFaces];
if (!pt &#0124; &#0124; !norm &#0124; &#0124; !face) return -1;
for (int p=0;p<numVerts;p++)
infile >> pt[p].x>>pt[p].y>>pt[p].z;
for (int n=0;n<numNormals;n++)
infile >>norm[n].x>>norm[n].y>>norm[n].z;
for(int f=0;f<numFaces;f++)
{
infile >> face[f].nVerts;
face[f].vert = new VertexID[face[f].nVerts];
for(int i=0;i<face[f].nVerts;i++)
infile >> face[f].vert[i].vertIndex
>> face[f].vert[i].normIndex;
}

return 0;

}//end readFile()
//---------------------------
void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
for (int f=0;f<numFaces;f++)
{
glBegin(GL_POLYGON);
for (int v=0;v< face[f].nVerts;v++)
{
int in = face[f].vert[v].normIndex;
int iv = face[f].vert[v].vertIndex;
glNormal3f(norm[in].x,norm[in].y,norm[in].z);
glVertex3f(pt[iv].x,pt[iv].y,pt[iv].z);
}
glEnd();

}//end for
}//end draw()
};//end class Mesh()
//-------------------------------
Mesh m;

//-------------------------------
void setWindow(float left, float right, float bottom, float top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left,right,bottom, top);
}
//---------------- setViewport ------------------
void setViewport(int left, int right, int bottom, int top)
{
glViewport(left,bottom,right-left,top-bottom);
}
//--------------------------------------------------------
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4.0);
// glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluOrtho2D(0.0,640.0,0.0,480.0);
}
//------------------------------------------------------
void mydisplay9(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,1.0f,1.0f);
// setWindow(0.0,6.4,0.0,4.8);
//setViewport(0,640,0,480);
// glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.2,3.2,-2.4,2.4,1,50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-4.0,4.0,4.0,0.0,0.0,0.0,0.0,1.0,0.0);
m.draw();
glFlush();
}//end mydisplay9
//------------------------------------------------------
int main(int argc, char **argv)
{
m.readFile("data.txt");
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640,480);
glutInitWindowPosition(0,10);
glutCreateWindow ("Just Exercises");
glutDisplayFunc(mydisplay9);
/*glutAddMenuEntry("scaled squares",1);
glutAddMenuEntry("maize like a cube",2);
glutAddMenuEntry("hook collections",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);*/
myInit();
glutMainLoop();
return 0;
}

//-----------------------------------
//------data of the cube in 3D
10 7 7
0 0 0 1 0 0 1 1 0 .5 1.5 0 0 1 0
0 0 1 1 0 1 1 1 1 .5 1.5 1 0 1 1
-1 0 0 -0.707 0.707 0 0.707 0.707 0
1 0 0 0 -1 0 0 0 1 0 0 -1
4 0 5 9 4 0 0 0 0
4 3 4 9 8 1 1 1 1
4 2 3 8 7 2 2 2 2
4 1 2 7 6 3 3 3 3
4 0 1 6 5 4 4 4 4
5 5 6 7 8 9 5 5 5 5 5
5 0 4 3 2 1 6 6 6 6 6

mikael_aronsson
10-31-2003, 01:00 AM
Hi !

So you have problems with it ? it would be nice if you could explain what you have problems with, what result do you get, does it draw anything ?

Mikael

spworld
11-02-2003, 02:01 AM
Actually ,nothing is drawn.Well,it something might have been drawn but it is not shown.I guess that I couldn't set the camera correctly.Would you help .