473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

OpenGL + Triangulation

11
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <windows.h> 
  3. #include <gl\gl.h>
  4. #include <gl\glut.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <vector>
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <stdio.h>
  11.  
  12.  
  13.  
  14. struct Point3D
  15. {
  16.     float x, y, z;
  17. };
  18.  
  19. struct Triangle
  20. {
  21.     char name[10];
  22.     Point3D point[3];
  23. };
  24.  
  25. istream & operator >> (istream & in , Point3D & pt)
  26. {
  27.     char c;
  28.  
  29.     in >> c >> pt.x >> c >> pt.y >> c >> pt.z >> c;
  30.  
  31.     return in;
  32. }
  33.  
  34. istream & operator >> (istream & in, Triangle & tri)
  35. {
  36.     in >> tri.name >> tri.point[0] >> tri.point[1] >> tri.point[2];
  37.  
  38.     return in;
  39. }
  40.  
  41. ostream & operator << (ostream & out , const Point3D & pt)
  42. {
  43.     out << "(" << pt.x << "," << pt.y << "," << pt.z << ")";
  44.  
  45.     return out;
  46. }
  47.  
  48. ostream & operator << (ostream & out , const Triangle & tri)
  49. {
  50.     out << tri.name << " " << tri.point[0] << " " << tri.point[1] << " " << tri.point[2];
  51.  
  52.     return out;
  53. }
  54.  
  55. float read () 
  56. {
  57.  
  58.     std::vector<Triangle> v;
  59.  
  60.     ifstream in ("sample.txt", ios::in);
  61.  
  62.     if (!in)
  63.  
  64.     {
  65.         cout << "could not open" << endl;
  66.         return 1;
  67.     }
  68.  
  69.     return 1;
  70. }
  71.  
  72. void draw (const Triangle)
  73. {
  74.     Triangle triangle;
  75.  
  76.     while (in >> triangle)
  77.     {
  78.         v.push_back(triangle);
  79.     }
  80.  
  81.     glBegin(GL_TRIANGLES);
  82.  
  83.     for (int i = 0, i<v.size(), i++)
  84.     {
  85.         glVertex3f(v[i]);
  86.     }
  87.  
  88.     glEnd;
  89.  
  90.     glutSwapBuffers();
  91.  
  92.     return 0;
  93.  
  94. }
  95.  
  96. int main (int argc, char **argv) 
  97. {
  98.     glutInit(&argc, argv);
  99.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
  100.     glutInitWindowSize(640, 480);  
  101.     glutInitWindowPosition(10,10); 
  102.     glutCreateWindow("Triangle"); 
  103.     //init(); 
  104.     glMatrixMode(GL_PROJECTION);
  105.     glutDisplayFunc(draw);
  106.     //glutKeyboardFunc(keyboard);
  107.  
  108.     glutMainLoop ();
  109.  
  110.     return 0;
  111.  
  112. }
  113.  
  114.  
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.
Feb 28 '07 #1
8 4989
horace1
1,510 Expert 1GB
not sure what you wish to do? I assume some form of animation - have you seen the spinning triangle example?
http://www.lighthouse3d.com/opengl/glut/index.php?4
Feb 28 '07 #2
freqzz
11
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.
Feb 28 '07 #3
horace1
1,510 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. //  ** read trianges into vector v
  3. std::vector<Triangle> v;  // moved - make global for now????
  4.  
  5. float read () 
  6. {
  7.  
  8.     //std::vector<Triangle> v;   
  9.  
  10.     ifstream in ("sample.txt", ios::in);
  11.  
  12.     if (!in)
  13.  
  14.     {
  15.         cout << "could not open" << endl;
  16.         return 1;
  17.     }
  18.     Triangle triangle;   // **** moved to here
  19.  
  20.     while (in >> triangle)
  21.     {
  22.         v.push_back(triangle);
  23.     }
  24.  
  25.     return 1;
  26. }
  27.  
  28. void draw ()  // ** removed const Triangle)
  29. {
  30.  
  31.     glBegin(GL_TRIANGLES);
  32.  
  33.     for (int i = 0; i<v.size(); i++)  // ** replaced , with ;
  34.     {
  35.         glVertex3f(v[i].point[0].x, v[i].point[0].y, v[i].point[0].z);   //  pass first vertex
  36.         //  <<<  call glVertex3f() to pass other two vertexs here
  37.     }
  38.  
  39.     glEnd();
  40.  
  41.     glutSwapBuffers();
  42.  
  43.     return;
  44.  
  45. }
  46.  
  47. int main (int argc, char **argv) 
  48. {
  49.     read();  // ** read triangles
  50.     glutInit(&argc, argv);
  51.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
  52.     glutInitWindowSize(640, 480);  
  53.     glutInitWindowPosition(10,10); 
  54.     glutCreateWindow("Triangle"); 
  55.     //init(); 
  56.     glMatrixMode(GL_PROJECTION);
  57.     glutDisplayFunc(draw);
  58.     //glutKeyboardFunc(keyboard);
  59.  
  60.     glutMainLoop ();
  61.  
  62.     return 0;
  63.  
  64. }
  65.  
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?
Feb 28 '07 #4
freqzz
11
Thanks. It does help a bit. I'll modify 2-3 things before posting the code back. I'll keep this updated.
Mar 1 '07 #5
freqzz
11
I have finish created a fully fuctioniol OpenGL program with some camera movement. Now how do I texture the triangle?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <windows.h> 
  4. #include <gl\gl.h>
  5. #include <gl\glut.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <vector>
  9. #include <iostream.h>
  10. #include <fstream.h>
  11. #include <stdio.h>
  12.  
  13. float zoom = 15.0f;
  14. float rotx = 0;
  15. float roty = 0.001f;
  16. float tx = 0;
  17. float ty = 0;
  18. int lastx=0;
  19. int lasty=0;
  20. unsigned char Buttons[3] = {0};
  21.  
  22. struct Point3D
  23. {
  24.     float x, y, z;
  25. };
  26.  
  27. struct Triangle
  28. {
  29.     char name[10];
  30.     Point3D point[3];
  31. };
  32.  
  33. istream & operator >> (istream & in , Point3D & pt)
  34. {
  35.     char c;
  36.  
  37.     in >> c >> pt.x >> c >> pt.y >> c >> pt.z >> c;
  38.  
  39.     return in;
  40. }
  41.  
  42. istream & operator >> (istream & in, Triangle & tri)
  43. {
  44.     in >> tri.name >> tri.point[0] >> tri.point[1] >> tri.point[2];
  45.  
  46.     return in;
  47. }
  48.  
  49. ostream & operator << (ostream & out , const Point3D & pt)
  50. {
  51.     out << "(" << pt.x << "," << pt.y << "," << pt.z << ")";
  52.  
  53.     return out;
  54. }
  55.  
  56. ostream & operator << (ostream & out , const Triangle & tri)
  57. {
  58.     out << tri.name << " " << tri.point[0] << " " << tri.point[1] << " " << tri.point[2];
  59.  
  60.     return out;
  61. }
  62.  
  63. std::vector<Triangle> v;
  64.  
  65. float read () 
  66. {
  67.  
  68.     //std::vector<Triangle> v;
  69.  
  70.     ifstream in ("sample.txt", ios::in);
  71.  
  72.     if (!in)
  73.  
  74.     {
  75.         cout << "could not open" << endl;
  76.         return 1;
  77.     }
  78.  
  79.     Triangle triangle;
  80.  
  81.     while (in >> triangle)
  82.     {
  83.         v.push_back(triangle);
  84.     }
  85.  
  86.     return 1;
  87.  
  88. }
  89.  
  90. void init()
  91. {
  92.     //glEnable(GL_DEPTH_TEST);
  93.     //glClearColor(0.0,0.0,0.0,0.0);
  94.     //glColor3f(1.0, 1.0, 1.0);
  95.     glLoadIdentity();
  96.     //glOrtho(-10,10,-10,10,-10,10);
  97.  
  98. }
  99.  
  100.  
  101. void draw ()
  102. {
  103.  
  104.     glClear(GL_COLOR_BUFFER_BIT);
  105.  
  106.     glLoadIdentity();
  107.  
  108.     glTranslatef(0,0,-zoom);
  109.     glTranslatef(tx,ty,0);
  110.     glRotatef(rotx,1,0,0);
  111.     glRotatef(roty,0,1,0);    
  112.  
  113.  
  114.     glBegin(GL_TRIANGLES); // I would like to texture this
  115.                                                // combination of multiple triangle
  116.  
  117.     for ( int i = 0; i<v.size(); ++i)
  118.     {
  119.         glVertex3f(v[i].point[0].x, v[i].point[0].y, v[i].point[0].z);
  120.         glVertex3f(v[i].point[1].x, v[i].point[1].y, v[i].point[1].z);
  121.         glVertex3f(v[i].point[2].x, v[i].point[2].y, v[i].point[2].z);
  122.  
  123.     }    
  124.  
  125.     glEnd();
  126.  
  127.     glutSwapBuffers();
  128.  
  129.     glBegin(GL_LINES);
  130.     for(int j=-10;j<=10;++j) 
  131.     {
  132.         glVertex3f(j,0,-10);
  133.         glVertex3f(j,0,10);
  134.  
  135.         glVertex3f(10,0,j);
  136.         glVertex3f(-10,0,j);
  137.     }
  138.  
  139.     glEnd();
  140.  
  141.     glutSwapBuffers();
  142. }
  143.  
  144. void reshape(int w, int h)
  145. {
  146.  
  147.     if(w==0) 
  148.         h = 1;
  149.  
  150.     glViewport(0,0,w,h);
  151.     glMatrixMode(GL_PROJECTION);
  152.     glLoadIdentity();
  153.     gluPerspective(100,(float)w/h,1,100);
  154.     glMatrixMode(GL_MODELVIEW);
  155.     glLoadIdentity();
  156. }
  157.  
  158. void Motion(int x,int y)
  159. {
  160.     int diffx=x-lastx;
  161.     int diffy=y-lasty;
  162.     lastx=x;
  163.     lasty=y;
  164.  
  165.     if( Buttons[0] && Buttons[1] )
  166.     {
  167.         zoom -= (float) 0.05f * diffx;
  168.     }
  169.     else
  170.         if( Buttons[0] )
  171.         {
  172.             rotx += (float) 0.5f * diffy;
  173.             roty += (float) 0.5f * diffx;        
  174.         }
  175.         else
  176.             if( Buttons[1] )
  177.             {
  178.                 tx += (float) 0.05f * diffx;
  179.                 ty -= (float) 0.05f * diffy;
  180.             }
  181.             glutPostRedisplay();
  182. }
  183.  
  184. void Mouse(int b,int s,int x,int y)
  185. {
  186.     lastx=x;
  187.     lasty=y;
  188.     switch(b)
  189.     {
  190.     case GLUT_LEFT_BUTTON:
  191.         Buttons[0] = ((GLUT_DOWN==s)?1:0);
  192.         break;
  193.     case GLUT_MIDDLE_BUTTON:
  194.         Buttons[1] = ((GLUT_DOWN==s)?1:0);
  195.         break;
  196.     case GLUT_RIGHT_BUTTON:
  197.         Buttons[2] = ((GLUT_DOWN==s)?1:0);
  198.         break;
  199.     default:
  200.         break;        
  201.     }
  202.     glutPostRedisplay();
  203. }
  204.  
  205. int main (int argc, char **argv) 
  206. {
  207.     read();
  208.     glutInit(&argc, argv);
  209.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
  210.     glutInitWindowSize(640, 480);  
  211.     glutInitWindowPosition(10,10); 
  212.     glutCreateWindow("Triangle"); 
  213.  
  214.     init(); 
  215.     glMatrixMode(GL_PROJECTION);
  216.  
  217.     glutDisplayFunc(draw);
  218.     glutReshapeFunc(reshape);
  219.     glutMouseFunc(Mouse);
  220.     glutMotionFunc(Motion);
  221.  
  222.     glutMainLoop ();
  223.  
  224.     return 0;
  225.  
  226. }
  227.  
  228.  
Any help will be highly appreciated.
Mar 7 '07 #6
horace1
1,510 Expert 1GB
have you seen this example in the codeproject?
http://www.codeproject.com/opengl/openglstruct.asp
Mar 7 '07 #7
freqzz
11
have you seen this example in the codeproject?
http://www.codeproject.com/opengl/openglstruct.asp
That does not even come close to my requirement. But thanks. I just want to wrap a combination of Triangle.
Mar 10 '07 #8
freqzz
11
Any help? I'm practically stuck here..
Mar 12 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
15
by: oracle411 | last post by:
Hi I'm new to OpenGL, but have an OpenGL application written in CPP that I would like to display on a web browser. Was wondering how this could be done? Thank you very much
3
by: john townsley | last post by:
Hi I want to do some OpenGl stuff with c++ on a winXP (home) machine. I have Borland c++ 5, MVS c++ 5 What C++ do you recommend (any others?)and where do you get the OpenGL stuff. thanks
1
by: Shi Mu | last post by:
is there any sample code to triangulation? many thanks!
10
by: Bad_Kid | last post by:
which is better for what?
4
by: tobfon | last post by:
I'm creating a scientific visualization application with rather high demands on performance. I've created a nice rendering engine for it in C++/OpenGL and a python interface to the rendering...
3
by: jg.campbell.ng | last post by:
I'm beginning learning Python and OpenGL in Python. Python fine. But difficulties with OpenGL; presumably with the installation of OpenGL. OS = Linux FC5. Python program gl_test.py: from...
9
by: Achim Domma | last post by:
Hi, I'm developing a GUI app in Python/C++ to visualize numerical results. Currently I'm using Python 2.4 with wx and PyOpenGLContext, but there are no windows binaries for Python 2.5 for quite...
3
by: McGruber | last post by:
Hi, guys. Currently I am researching the optimised algorithms of building triangulations of 3D surfaces. And I faces the problem with visualization of the 3D triangulation. I tried to do it with...
8
by: Grant Edwards | last post by:
Can anybody point me to a Delaunay triangulation module (for Win32)? I'm currently using http://flub.stuffwillmade.org/delny/ under Linux, but I have been unable to find a build for Windows. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.