Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <windows.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <math.h>
- using namespace System::Windows::Forms;
- namespace OpenGLForm
- {
- public ref class COpenGL: public System::Windows::Forms::NativeWindow
- {
- public:
- COpenGL(System::Windows::Forms::Form ^ parentForm, GLsizei iWidth, GLsizei iHeight)
- {
- CreateParams^ cp = gcnew CreateParams;
- // Set the position on the form
- cp->X = 235;
- cp->Y = 80;
- cp->Height = iWidth;
- cp->Width = iHeight;
- // Specify the form as the parent.
- cp->Parent = parentForm->Handle;
- // Create as a child of the specified parent and make OpenGL compliant (no clipping)
- cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
- // Create the actual window
- this->CreateHandle(cp);
- m_hDC = GetDC((HWND)this->Handle.ToPointer());
- if(m_hDC)
- {
- MySetPixelFormat(m_hDC);
- ReSizeGLScene(iWidth, iHeight);
- InitGL();
- }
- rot = 0.0f;
- }
- void Render (int kleur, int index) {
- const double PI = 3.141592654;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0.0f,0.6f,-6.0f);
- glRotatef(rot,0.0f,1.0f,0.0f);
- glColor3f(1.0f,0.0f,0.0f);
- if (kleur == 1 ) {
- rot+=0.2f;
- }
- //(X, Y, Z)
- /*glBegin(GL_LINES);
- glColor3f(0.0f,0.0f,0.0f);
- glVertex3f(-1.0f, -1.0f, 0.0f);
- glVertex3f(-1.0f, 1.0f, 0.0f);
- glVertex3f(1.0f, 1.0f, 0.0f);
- glVertex3f(1.0f, -1.0f, 0.0f);
- glEnd();*/
- if ( index == 2 ) {
- glBegin(GL_POLYGON);
- glVertex2d(-1.0,1.0);
- glVertex2d(1.0,1.0);
- glVertex2d(1.0,-1.0);
- glVertex2d(-1.0,-1.0);
- glEnd();
- }
- //Vierkant
- if ( index == 3 ) {
- glBegin(GL_POLYGON);
- /*Rechthoekige driehoek 0
- Gelijkbenige driehoek 1
- Vierkant 2
- Rechthoek 3
- Cirkel 4
- Pentagoon 5
- Hexagoon 6
- Octagoon 7
- Kubus 8
- Balk 9
- Bol 10
- Cilinder 11
- Piramide 12*/
- if ( index == 6 ) {
- //Hexagoon
- glBegin(GL_POLYGON);
- for(int i = 0; i < 6; ++i) {
- glVertex2d(sin(i/6.0*2*PI),
- cos(i/6.0*2*PI));
- }
- glEnd();}
- if ( index == 5 ) {
- glBegin(GL_POLYGON);
- for(int i = 0; i < 5; ++i) {
- glVertex2d(sin(i/5.0*2*PI),
- cos(i/5.0*2*PI));
- }
- glEnd();}
- //Pentagoon
- if ( index == 7 ) {
- glBegin(GL_POLYGON);
- for(int i = 0; i < 8; ++i) {
- glVertex2d(sin(i/8.0*2*PI),
- cos(i/8.0*2*PI));
- }
- glEnd();}
- //Octagoon
- if ( index == 12 ) {
- glBegin(GL_TRIANGLES);
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f(-1.0f,-1.0f, 1.0f);
- glVertex3f( 1.0f,-1.0f, 1.0f);
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f( 1.0f,-1.0f, 1.0f);
- glVertex3f( 1.0f,-1.0f, -1.0f);
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f( 1.0f,-1.0f, -1.0f);
- glVertex3f(-1.0f,-1.0f, -1.0f);
- glVertex3f( 0.0f, 1.0f, 0.0f);
- glVertex3f(-1.0f,-1.0f,-1.0f);
- glVertex3f(-1.0f,-1.0f, 1.0f);
- glEnd();
- }
- //Cirkel
- /*kubus
- glBegin(GL_QUADS);
- glColor3f(2.0f,5.0f,8.0f);
- glVertex3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0.0f, -1.0f, 0.0f);
- glVertex3f(-2.0f, -1.0f, 0.0f);
- glVertex3f(-2.0f, 1.0f, 0.0f);
- glVertex3f(0.0f, 1.0f, 2.0f);
- glVertex3f(0.0f, -1.0f, 2.0f);
- glVertex3f(-2.0f, -1.0f, 2.0f);
- glVertex3f(-2.0f, 1.0f, 2.0f);
- glVertex3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0.0f, -1.0f, 0.0f);
- glVertex3f(-2.0f, -1.0f, 0.0f);
- glVertex3f(-2.0f, 1.0f, 0.0f);
- glVertex3f(0.0f, 1.0f, 2.0f);
- glVertex3f(0.0f, -1.0f, 2.0f);
- glVertex3f(-2.0f, -1.0f, 2.0f);
- glVertex3f(-2.0f, 1.0f, 2.0f);
- glEnd();*/
- }
- System::Void SwapOpenGLBuffers(System::Void)
- {
- SwapBuffers(m_hDC) ;
- }
- private:
- HDC m_hDC;
- HGLRC m_hglrc;
- GLfloat rot;
- protected:
- ~COpenGL(System::Void)
- {
- this->DestroyHandle();
- }
- GLint MySetPixelFormat(HDC hdc)
- {
- static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
- {
- sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
- 1, // Version Number
- PFD_DRAW_TO_WINDOW | // Format Must Support Window
- PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
- PFD_DOUBLEBUFFER, // Must Support Double Buffering
- PFD_TYPE_RGBA, // Request An RGBA Format
- 16, // Select Our Color Depth
- 0, 0, 0, 0, 0, 0, // Color Bits Ignored
- 0, // No Alpha Buffer
- 0, // Shift Bit Ignored
- 0, // No Accumulation Buffer
- 0, 0, 0, 0, // Accumulation Bits Ignored
- 16, // 16Bit Z-Buffer (Depth Buffer)
- 0, // No Stencil Buffer
- 0, // No Auxiliary Buffer
- PFD_MAIN_PLANE, // Main Drawing Layer
- 0, // Reserved
- 0, 0, 0 // Layer Masks Ignored
- };
- GLint iPixelFormat;
- // get the device context's best, available pixel format match
- if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
- {
- MessageBox::Show("ChoosePixelFormat Failed");
- return 0;
- }
- // make that match the device context's current pixel format
- if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
- {
- MessageBox::Show("SetPixelFormat Failed");
- return 0;
- }
- if((m_hglrc = wglCreateContext(m_hDC)) == NULL)
- {
- MessageBox::Show("wglCreateContext Failed");
- return 0;
- }
- if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
- {
- MessageBox::Show("wglMakeCurrent Failed");
- return 0;
- }
- return 1;
- }
- bool InitGL(GLvoid) // All setup for opengl goes here
- {
- glShadeModel(GL_SMOOTH); // Enable smooth shading
- glClearColor(0.90980392156862745098039215686275f, 0.90980392156862745098039215686275f, 0.90980392156862745098039215686275f, 0.5f); // Black background
- glClearDepth(1.0f); // Depth buffer setup
- glEnable(GL_DEPTH_TEST); // Enables depth testing
- glDepthFunc(GL_LEQUAL); // The type of depth testing to do
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really nice perspective calculations
- return TRUE; // Initialisation went ok
- }
- GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize and initialise the gl window
- {
- if (height==0) // Prevent A Divide By Zero By
- {
- height=1; // Making Height Equal One
- }
- glViewport(0,0,width,height); // Reset The Current Viewport
- glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
- glLoadIdentity(); // Reset The Projection Matrix
- // Calculate The Aspect Ratio Of The Window
- gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
- glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
- glLoadIdentity(); // Reset The Modelview Matrix
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment