OpenGL + Triangulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freqzz
    New Member
    • Feb 2007
    • 11

    OpenGL + Triangulation

    Code:
    #include <windows.h> 
    #include <gl\gl.h>
    #include <gl\glut.h>
    #include <stdlib.h>
    #include <math.h>
    #include <vector>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    
    
    struct Point3D
    {
    	float x, y, z;
    };
    
    struct Triangle
    {
    	char name[10];
    	Point3D point[3];
    };
    
    istream & operator >> (istream & in , Point3D & pt)
    {
        char c;
    
        in >> c >> pt.x >> c >> pt.y >> c >> pt.z >> c;
    
        return in;
    }
    
    istream & operator >> (istream & in, Triangle & tri)
    {
        in >> tri.name >> tri.point[0] >> tri.point[1] >> tri.point[2];
    
        return in;
    }
    
    ostream & operator << (ostream & out , const Point3D & pt)
    {
        out << "(" << pt.x << "," << pt.y << "," << pt.z << ")";
    
        return out;
    }
    
    ostream & operator << (ostream & out , const Triangle & tri)
    {
        out << tri.name << " " << tri.point[0] << " " << tri.point[1] << " " << tri.point[2];
    
        return out;
    }
    
    float read () 
    {
        
        std::vector<Triangle> v;
    
        ifstream in ("sample.txt", ios::in);
    
        if (!in)
    
        {
            cout << "could not open" << endl;
            return 1;
        }
    
        return 1;
    }
    
    void draw (const Triangle)
    {
    	Triangle triangle;
    
        while (in >> triangle)
        {
            v.push_back(triangle);
        }
    
    	glBegin(GL_TRIANGLES);
    
    	for (int i = 0, i<v.size(), i++)
    	{
    		glVertex3f(v[i]);
    	}
    
    	glEnd;
    
    	glutSwapBuffers();
    
    	return 0;
    
    }
    
    int main (int argc, char **argv) 
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
    	glutInitWindowSize(640, 480);  
    	glutInitWindowPosition(10,10); 
    	glutCreateWindow("Triangle"); 
    	//init(); 
    	glMatrixMode(GL_PROJECTION);
    	glutDisplayFunc(draw);
    	//glutKeyboardFunc(keyboard);
    	
        glutMainLoop ();
    
    	return 0;
    
    }
    How do I pass the value from the read( ) into draw ( ) so I can retain the x,y,z value. So I can draw the point into triangle.

    This is ther first time I combine both graphic programming and OO. So, explaination will be helpful.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    not sure what you wish to do? I assume some form of animation - have you seen the spinning triangle example?
    http://www.lighthouse3 d.com/opengl/glut/index.php?4

    Comment

    • freqzz
      New Member
      • Feb 2007
      • 11

      #3
      This is part of code where I want to read a Triangle table from textfile and plot it to draw a triangle. From there, i would like to texture the triangle and build a camera movement.

      But now, i'm stuck at the point where a i couldnt pass the x,y,z valu for drawing.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by freqzz
        This is part of code where I want to read a Triangle table from textfile and plot it to draw a triangle. From there, i would like to texture the triangle and build a camera movement.

        But now, i'm stuck at the point where a i couldnt pass the x,y,z valu for drawing.
        some initial ideas - not sure if it is what you required - but it compiles and starts to run

        Code:
        //  ** read trianges into vector v
        std::vector<Triangle> v;  // moved - make global for now????
        
        float read () 
        {
            
            //std::vector<Triangle> v;   
        
            ifstream in ("sample.txt", ios::in);
        
            if (!in)
        
            {
                cout << "could not open" << endl;
                return 1;
            }
        	Triangle triangle;   // **** moved to here
        
            while (in >> triangle)
            {
                v.push_back(triangle);
            }
        
            return 1;
        }
        
        void draw ()  // ** removed const Triangle)
        {
        
        	glBegin(GL_TRIANGLES);
        
        	for (int i = 0; i<v.size(); i++)  // ** replaced , with ;
        	{
        		glVertex3f(v[i].point[0].x, v[i].point[0].y, v[i].point[0].z);   //  pass first vertex
                //  <<<  call glVertex3f() to pass other two vertexs here
        	}
        
        	glEnd();
        
        	glutSwapBuffers();
        
        	return;
        
        }
        
        int main (int argc, char **argv) 
        {
            read();  // ** read triangles
        	glutInit(&argc, argv);
        	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
        	glutInitWindowSize(640, 480);  
        	glutInitWindowPosition(10,10); 
        	glutCreateWindow("Triangle"); 
        	//init(); 
        	glMatrixMode(GL_PROJECTION);
        	glutDisplayFunc(draw);
        	//glutKeyboardFunc(keyboard);
        	
            glutMainLoop ();
        
        	return 0;
        
        }
        read() reads the triangle data into vector v - call it from main() make v global

        draw() can then read the vertex data from v

        does that help?

        Comment

        • freqzz
          New Member
          • Feb 2007
          • 11

          #5
          Thanks. It does help a bit. I'll modify 2-3 things before posting the code back. I'll keep this updated.

          Comment

          • freqzz
            New Member
            • Feb 2007
            • 11

            #6
            I have finish created a fully fuctioniol OpenGL program with some camera movement. Now how do I texture the triangle?

            Code:
            
            #include <windows.h> 
            #include <gl\gl.h>
            #include <gl\glut.h>
            #include <stdlib.h>
            #include <math.h>
            #include <vector>
            #include <iostream.h>
            #include <fstream.h>
            #include <stdio.h>
            
            float zoom = 15.0f;
            float rotx = 0;
            float roty = 0.001f;
            float tx = 0;
            float ty = 0;
            int lastx=0;
            int lasty=0;
            unsigned char Buttons[3] = {0};
            
            struct Point3D
            {
            	float x, y, z;
            };
            
            struct Triangle
            {
            	char name[10];
            	Point3D point[3];
            };
            
            istream & operator >> (istream & in , Point3D & pt)
            {
                char c;
            
                in >> c >> pt.x >> c >> pt.y >> c >> pt.z >> c;
            
                return in;
            }
            
            istream & operator >> (istream & in, Triangle & tri)
            {
                in >> tri.name >> tri.point[0] >> tri.point[1] >> tri.point[2];
            
                return in;
            }
            
            ostream & operator << (ostream & out , const Point3D & pt)
            {
                out << "(" << pt.x << "," << pt.y << "," << pt.z << ")";
            
                return out;
            }
            
            ostream & operator << (ostream & out , const Triangle & tri)
            {
                out << tri.name << " " << tri.point[0] << " " << tri.point[1] << " " << tri.point[2];
            
                return out;
            }
            
            std::vector<Triangle> v;
            
            float read () 
            {
                
                //std::vector<Triangle> v;
            
                ifstream in ("sample.txt", ios::in);
            
                if (!in)
            
                {
                    cout << "could not open" << endl;
                    return 1;
                }
            	
            	Triangle triangle;
            
            	while (in >> triangle)
            	{
            		v.push_back(triangle);
            	}
            
            	return 1;
               
            }
            
            void init()
            {
            	//glEnable(GL_DEPTH_TEST);
            	//glClearColor(0.0,0.0,0.0,0.0);
            	//glColor3f(1.0, 1.0, 1.0);
            	glLoadIdentity();
            	//glOrtho(-10,10,-10,10,-10,10);
            	
            }
            
            
            void draw ()
            {
            	
            	glClear(GL_COLOR_BUFFER_BIT);
            
            	glLoadIdentity();
            
            	glTranslatef(0,0,-zoom);
            	glTranslatef(tx,ty,0);
            	glRotatef(rotx,1,0,0);
            	glRotatef(roty,0,1,0);	
            	
            	
            	glBegin(GL_TRIANGLES); // I would like to texture this
                                                           // combination of multiple triangle
            
            	for ( int i = 0; i<v.size(); ++i)
            	{
            		glVertex3f(v[i].point[0].x, v[i].point[0].y, v[i].point[0].z);
            		glVertex3f(v[i].point[1].x, v[i].point[1].y, v[i].point[1].z);
            		glVertex3f(v[i].point[2].x, v[i].point[2].y, v[i].point[2].z);
            		
            	}	
            
            	glEnd();
            
            	glutSwapBuffers();
            
            	glBegin(GL_LINES);
            	for(int j=-10;j<=10;++j) 
            	{
            		glVertex3f(j,0,-10);
            		glVertex3f(j,0,10);
            
            		glVertex3f(10,0,j);
            		glVertex3f(-10,0,j);
            	}
            
            	glEnd();
            
            	glutSwapBuffers();
            }
            
            void reshape(int w, int h)
            {
            	
            	if(w==0) 
            		h = 1;
            
            	glViewport(0,0,w,h);
            	glMatrixMode(GL_PROJECTION);
            	glLoadIdentity();
            	gluPerspective(100,(float)w/h,1,100);
            	glMatrixMode(GL_MODELVIEW);
            	glLoadIdentity();
            }
            
            void Motion(int x,int y)
            {
            	int diffx=x-lastx;
            	int diffy=y-lasty;
            	lastx=x;
            	lasty=y;
            
            	if( Buttons[0] && Buttons[1] )
            	{
            		zoom -= (float) 0.05f * diffx;
            	}
            	else
            		if( Buttons[0] )
            		{
            			rotx += (float) 0.5f * diffy;
            			roty += (float) 0.5f * diffx;		
            		}
            		else
            			if( Buttons[1] )
            			{
            				tx += (float) 0.05f * diffx;
            				ty -= (float) 0.05f * diffy;
            			}
            			glutPostRedisplay();
            }
            
            void Mouse(int b,int s,int x,int y)
            {
            	lastx=x;
            	lasty=y;
            	switch(b)
            	{
            	case GLUT_LEFT_BUTTON:
            		Buttons[0] = ((GLUT_DOWN==s)?1:0);
            		break;
            	case GLUT_MIDDLE_BUTTON:
            		Buttons[1] = ((GLUT_DOWN==s)?1:0);
            		break;
            	case GLUT_RIGHT_BUTTON:
            		Buttons[2] = ((GLUT_DOWN==s)?1:0);
            		break;
            	default:
            		break;		
            	}
            	glutPostRedisplay();
            }
            
            int main (int argc, char **argv) 
            {
            	read();
            	glutInit(&argc, argv);
            	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
            	glutInitWindowSize(640, 480);  
            	glutInitWindowPosition(10,10); 
            	glutCreateWindow("Triangle"); 
            	
            	init(); 
            	glMatrixMode(GL_PROJECTION);
            	
            	glutDisplayFunc(draw);
            	glutReshapeFunc(reshape);
            	glutMouseFunc(Mouse);
            	glutMotionFunc(Motion);
            	
                glutMainLoop ();
            
            	return 0;
            
            }
            Any help will be highly appreciated.

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              have you seen this example in the codeproject?
              http://www.codeproject .com/opengl/openglstruct.as p

              Comment

              • freqzz
                New Member
                • Feb 2007
                • 11

                #8
                Originally posted by horace1
                have you seen this example in the codeproject?
                http://www.codeproject .com/opengl/openglstruct.as p
                That does not even come close to my requirement. But thanks. I just want to wrap a combination of Triangle.

                Comment

                • freqzz
                  New Member
                  • Feb 2007
                  • 11

                  #9
                  Any help? I'm practically stuck here..

                  Comment

                  Working...