Guest User

Untitled

a guest
Mar 5th, 2011
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <windows.h>
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6. #include <math.h>
  7.  
  8. using namespace System::Windows::Forms;
  9.  
  10. namespace OpenGLForm
  11. {
  12.     public ref class COpenGL: public System::Windows::Forms::NativeWindow
  13.     {
  14.     public:
  15.         COpenGL(System::Windows::Forms::Form ^ parentForm, GLsizei iWidth, GLsizei iHeight)
  16.         {
  17.             CreateParams^ cp = gcnew CreateParams;
  18.  
  19.             // Set the position on the form
  20.             cp->X = 235;
  21.             cp->Y = 80;
  22.             cp->Height = iWidth;
  23.             cp->Width = iHeight;
  24.  
  25.             // Specify the form as the parent.
  26.             cp->Parent = parentForm->Handle;
  27.  
  28.             // Create as a child of the specified parent and make OpenGL compliant (no clipping)
  29.             cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  30.  
  31.             // Create the actual window
  32.             this->CreateHandle(cp);
  33.  
  34.             m_hDC = GetDC((HWND)this->Handle.ToPointer());
  35.  
  36.             if(m_hDC)
  37.             {
  38.                 MySetPixelFormat(m_hDC);
  39.                 ReSizeGLScene(iWidth, iHeight);
  40.                 InitGL();
  41.             }
  42.  
  43.             rot = 0.0f;
  44.         }
  45.            
  46.  
  47.         void Render (int kleur, int index) {
  48.             const double PI = 3.141592654;
  49.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  50.             glLoadIdentity();
  51.  
  52.             glTranslatef(0.0f,0.6f,-6.0f);
  53.             glRotatef(rot,0.0f,1.0f,0.0f);
  54.             glColor3f(1.0f,0.0f,0.0f);
  55.             if (kleur == 1 ) {
  56.             rot+=0.2f;
  57.             }
  58.             //(X, Y, Z)
  59.             /*glBegin(GL_LINES);
  60.            
  61.                 glColor3f(0.0f,0.0f,0.0f);
  62.                 glVertex3f(-1.0f, -1.0f, 0.0f);
  63.                 glVertex3f(-1.0f, 1.0f, 0.0f);
  64.                 glVertex3f(1.0f, 1.0f, 0.0f);
  65.                 glVertex3f(1.0f, -1.0f, 0.0f);
  66.  
  67.  
  68.                
  69.                 glEnd();*/
  70.  
  71.            
  72.             if ( index == 2 ) {
  73.                 glBegin(GL_POLYGON);
  74.                 glVertex2d(-1.0,1.0);
  75.                 glVertex2d(1.0,1.0);
  76.                 glVertex2d(1.0,-1.0);
  77.                 glVertex2d(-1.0,-1.0);
  78.                 glEnd();
  79.             }
  80.  
  81.             //Vierkant
  82.  
  83.             if ( index == 3 ) {
  84.                 glBegin(GL_POLYGON);
  85.                            
  86.             /*Rechthoekige driehoek 0
  87. Gelijkbenige driehoek 1
  88. Vierkant 2
  89. Rechthoek 3
  90. Cirkel 4
  91. Pentagoon 5
  92. Hexagoon 6
  93. Octagoon 7
  94. Kubus 8
  95. Balk 9
  96. Bol 10
  97. Cilinder 11
  98. Piramide 12*/
  99.  
  100.            
  101.             if ( index == 6 ) {
  102.             //Hexagoon
  103.             glBegin(GL_POLYGON);
  104. for(int i = 0; i < 6; ++i) {
  105.     glVertex2d(sin(i/6.0*2*PI),
  106.                cos(i/6.0*2*PI));
  107. }
  108. glEnd();}
  109.  
  110.  
  111.  
  112.             if ( index == 5 ) {
  113.             glBegin(GL_POLYGON);
  114. for(int i = 0; i < 5; ++i) {
  115.     glVertex2d(sin(i/5.0*2*PI),
  116.                cos(i/5.0*2*PI));
  117. }
  118. glEnd();}
  119.             //Pentagoon
  120.  
  121.  
  122.  
  123.             if ( index == 7 ) {
  124.             glBegin(GL_POLYGON);
  125. for(int i = 0; i < 8; ++i) {
  126.     glVertex2d(sin(i/8.0*2*PI),
  127.                cos(i/8.0*2*PI));
  128. }
  129. glEnd();}
  130.             //Octagoon
  131.  
  132.            
  133.  
  134.  
  135.            
  136.  
  137.            
  138.             if ( index == 12 ) {
  139.             glBegin(GL_TRIANGLES); 
  140.        
  141.                 glVertex3f( 0.0f, 1.0f, 0.0f); 
  142.                 glVertex3f(-1.0f,-1.0f, 1.0f);
  143.                 glVertex3f( 1.0f,-1.0f, 1.0f); 
  144.                 glVertex3f( 0.0f, 1.0f, 0.0f); 
  145.                 glVertex3f( 1.0f,-1.0f, 1.0f); 
  146.                 glVertex3f( 1.0f,-1.0f, -1.0f);
  147.                 glVertex3f( 0.0f, 1.0f, 0.0f); 
  148.                 glVertex3f( 1.0f,-1.0f, -1.0f);
  149.                 glVertex3f(-1.0f,-1.0f, -1.0f);    
  150.                 glVertex3f( 0.0f, 1.0f, 0.0f);         
  151.                 glVertex3f(-1.0f,-1.0f,-1.0f);             
  152.                 glVertex3f(-1.0f,-1.0f, 1.0f);
  153.             glEnd();                                           
  154.             }
  155.             //Cirkel
  156.  
  157.  
  158.             /*kubus
  159.             glBegin(GL_QUADS);
  160.  
  161.                 glColor3f(2.0f,5.0f,8.0f);
  162.                 glVertex3f(0.0f, 1.0f, 0.0f);
  163.                 glVertex3f(0.0f, -1.0f, 0.0f);
  164.                 glVertex3f(-2.0f, -1.0f, 0.0f);
  165.                 glVertex3f(-2.0f, 1.0f, 0.0f);
  166.                 glVertex3f(0.0f, 1.0f, 2.0f);
  167.                 glVertex3f(0.0f, -1.0f, 2.0f);
  168.                 glVertex3f(-2.0f, -1.0f, 2.0f);
  169.                 glVertex3f(-2.0f, 1.0f, 2.0f);
  170.  
  171.                 glVertex3f(0.0f, 1.0f, 0.0f);
  172.                 glVertex3f(0.0f, -1.0f, 0.0f);
  173.                 glVertex3f(-2.0f, -1.0f, 0.0f);
  174.                 glVertex3f(-2.0f, 1.0f, 0.0f);
  175.                 glVertex3f(0.0f, 1.0f, 2.0f);
  176.                 glVertex3f(0.0f, -1.0f, 2.0f);
  177.                 glVertex3f(-2.0f, -1.0f, 2.0f);
  178.                 glVertex3f(-2.0f, 1.0f, 2.0f);
  179.             glEnd();*/
  180.  
  181.            
  182.         }
  183.  
  184.  
  185.  
  186.         System::Void SwapOpenGLBuffers(System::Void)
  187.         {
  188.             SwapBuffers(m_hDC) ;
  189.         }
  190.  
  191.     private:
  192.         HDC m_hDC;
  193.         HGLRC m_hglrc;
  194.         GLfloat rot;
  195.  
  196.     protected:
  197.         ~COpenGL(System::Void)
  198.         {
  199.             this->DestroyHandle();
  200.         }
  201.  
  202.         GLint MySetPixelFormat(HDC hdc)
  203.         {
  204.             static  PIXELFORMATDESCRIPTOR pfd=              // pfd Tells Windows How We Want Things To Be
  205.                 {
  206.                     sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
  207.                     1,                                          // Version Number
  208.                     PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
  209.                     PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
  210.                     PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
  211.                     PFD_TYPE_RGBA,                              // Request An RGBA Format
  212.                     16,                                     // Select Our Color Depth
  213.                     0, 0, 0, 0, 0, 0,                           // Color Bits Ignored
  214.                     0,                                          // No Alpha Buffer
  215.                     0,                                          // Shift Bit Ignored
  216.                     0,                                          // No Accumulation Buffer
  217.                     0, 0, 0, 0,                                 // Accumulation Bits Ignored
  218.                     16,                                         // 16Bit Z-Buffer (Depth Buffer)  
  219.                     0,                                          // No Stencil Buffer
  220.                     0,                                          // No Auxiliary Buffer
  221.                     PFD_MAIN_PLANE,                             // Main Drawing Layer
  222.                     0,                                          // Reserved
  223.                     0, 0, 0                                     // Layer Masks Ignored
  224.                 };
  225.            
  226.             GLint  iPixelFormat;
  227.          
  228.             // get the device context's best, available pixel format match
  229.             if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
  230.             {
  231.                 MessageBox::Show("ChoosePixelFormat Failed");
  232.                 return 0;
  233.             }
  234.              
  235.             // make that match the device context's current pixel format
  236.             if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
  237.             {
  238.                 MessageBox::Show("SetPixelFormat Failed");
  239.                 return 0;
  240.             }
  241.  
  242.             if((m_hglrc = wglCreateContext(m_hDC)) == NULL)
  243.             {
  244.                 MessageBox::Show("wglCreateContext Failed");
  245.                 return 0;
  246.             }
  247.  
  248.             if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
  249.             {
  250.                 MessageBox::Show("wglMakeCurrent Failed");
  251.                 return 0;
  252.             }
  253.  
  254.  
  255.             return 1;
  256.         }
  257.  
  258.         bool InitGL(GLvoid)                                     // All setup for opengl goes here
  259.         {
  260.             glShadeModel(GL_SMOOTH);                            // Enable smooth shading
  261.             glClearColor(0.90980392156862745098039215686275f, 0.90980392156862745098039215686275f, 0.90980392156862745098039215686275f, 0.5f);              // Black background
  262.             glClearDepth(1.0f);                                 // Depth buffer setup
  263.             glEnable(GL_DEPTH_TEST);                            // Enables depth testing
  264.             glDepthFunc(GL_LEQUAL);                             // The type of depth testing to do
  265.             glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really nice perspective calculations
  266.             return TRUE;                                        // Initialisation went ok
  267.         }
  268.  
  269.         GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize and initialise the gl window
  270.         {
  271.             if (height==0)                                      // Prevent A Divide By Zero By
  272.             {
  273.                 height=1;                                       // Making Height Equal One
  274.             }
  275.  
  276.             glViewport(0,0,width,height);                       // Reset The Current Viewport
  277.  
  278.             glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  279.             glLoadIdentity();                                   // Reset The Projection Matrix
  280.  
  281.             // Calculate The Aspect Ratio Of The Window
  282.             gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  283.  
  284.             glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
  285.             glLoadIdentity();                                   // Reset The Modelview Matrix
  286.         }
  287.     };
  288. }
Advertisement
Add Comment
Please, Sign In to add comment