bobek784 Temat założony przez niniejszego użytkownika |
» 2014-09-25 13:25:34 To kod: Opengl.h pragma once
#include <windows.h> #include <gl\GL.h> #include <stdio.h> #include <gl\GLU.h> #include <string> #include <gl\glut.h> #include <gl\glaux.h>
using namespace System::Windows::Forms; using namespace std; using namespace System;
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; cp->X = 100; cp->Y = 100; cp->Height = iWidth; cp->Width = iHeight; cp->Parent = parentForm->Handle; cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; this->CreateHandle( cp ); m_hDC = GetDC(( HWND ) this->Handle.ToPointer() ); if( m_hDC ) MySetPixelFormat( m_hDC ); } System::Void Render( int c, int p, float w, int r ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glTranslatef( 0.0f, 0.0f, - 6.0f ); if( r == 1 ) { glRotatef( rtri, 0.0f, 1.0f, 0.0f ); } else if( r == 2 ) { glRotatef( rtri, 1.0f, 0.0f, 0.0f ); } else if( r == 3 ) { glRotatef( rtri, 1.0f, 1.0f, 0.0f ); } else { rtri = 0; }; switch( c ) { case 1: Punkty( p, w ); break; case 2: Linie( p ); break; case 3: Linie2( p ); break; case 4: Linie3( p ); break; case 5: Trojkaty( p ); break; case 6: Trojkaty2( p ); break; case 7: Trojkaty3( p ); break; case 8: Czworokaty( p ); break; case 9: Czworokaty2( p ); break; case 10: Poligony( p ); break; } rtri += 0.2f; glFlush(); } System::Void Render2( int a, int b, int r2, float c1a, float c1b, float c1c, float c2a, float c2b, float c2c, float c3a, float c3b, float c3c, float c4a, float c4b, float c4c ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glTranslatef( 0.0f, 0.0f, - 6.0f ); if( r2 == 1 ) { glRotatef( rtri, 0.0f, 1.0f, 0.0f ); } else if( r2 == 2 ) { glRotatef( rtri, 1.0f, 0.0f, 0.0f ); } else if( r2 == 3 ) { glRotatef( rtri, 1.0f, 1.0f, 0.0f ); } else { rtri = 0; }; switch( a ) { case 1: glColor3f( c1a, c1b, c1c ); if( b == 1 ) { glBegin( GL_TRIANGLES ); glVertex3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 1.0f, - 1.0f, 0.0f ); glVertex3f( - 1.0f, - 1.0f, 0.0f ); glEnd(); } else if( b == 2 ) { glBegin( GL_QUADS ); 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(); }; break; case 2: if( b == 1 ) { glBegin( GL_TRIANGLES ); glColor3f( c1a, c1b, c1c ); glVertex3f( 0.0f, 1.0f, 0.0f ); glColor3f( c2a, c2b, c2c ); glVertex3f( 1.0f, - 1.0f, 0.0f ); glColor3f( c3a, c3b, c3c ); glVertex3f( - 1.0f, - 1.0f, 0.0f ); glEnd(); } else if( b == 2 ) { glBegin( GL_QUADS ); glColor3f( c1a, c1b, c1c ); glVertex3f( - 1.0f, 1.0f, 0.0f ); glColor3f( c2a, c2b, c2c ); glVertex3f( 1.0f, 1.0f, 0.0f ); glColor3f( c3a, c3b, c3c ); glVertex3f( 1.0f, - 1.0f, 0.0f ); glColor3f( c4a, c4b, c4c ); glVertex3f( - 1.0f, - 1.0f, 0.0f ); glEnd(); }; break; } rtri += 0.2f; glFlush(); } System::Void SwapOpenGLBuffers( System::Void ) { SwapBuffers( m_hDC ); } private: HDC m_hDC; HGLRC m_hglrc; GLfloat rtri; protected: ~COpenGL( System::Void ) { this->DestroyHandle(); } GLint MySetPixelFormat( HDC hdc ) { PIXELFORMATDESCRIPTOR pfd = { sizeof( PIXELFORMATDESCRIPTOR ), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; GLint iPixelFormat; if(( iPixelFormat = ChoosePixelFormat( hdc, & pfd ) ) == 0 ) { MessageBox::Show( "ChoosePixelFormat Failed" ); return 0; } 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; } }; } Okno pierwsze: #include "opengl.h" #include <string> #include <msclr/marshal_cppstd.h> #include <iostream> #include <fstream> #pragma once
namespace ProjektOpenGL { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace OpenGLForm; using namespace std; using namespace System::Runtime::InteropServices; using namespace System::IO; public ref class My1okno : public System::Windows::Forms::Form { public: My1okno( void ) { InitializeComponent(); OpenGL = gcnew COpenGL( this, 500, 500 ); } protected: ~My1okno() { if( components ) { delete components; } } (...) private : OpenGLForm::COpenGL ^ OpenGL; int a; int rotacja2; int b2; int a2; float c2; (...) #pragma region Windows Form Designer generated code void InitializeComponent( void ) { (...) } #pragma endregion private: System::Void timer1_Tick( System::Object ^ sender, System::EventArgs ^ e ) { UNREFERENCED_PARAMETER( sender ); UNREFERENCED_PARAMETER( e ); int rotacja = 0; if( checkBox1->Checked && checkBox2->Checked ) { rotacja = 3; } else if( checkBox1->Checked ) { rotacja = 1; } else if( checkBox2->Checked ) { rotacja = 2; }; int b =( int ) liczba1->Value; float c =( float ) liczba2->Value; OpenGL->Render( a, b, c, rotacja ); OpenGL->SwapOpenGLBuffers(); rotacja = 0; }
Okno drugie tak samo tylko tam wywołuję funkcję: OpenGL->Render2( a, b, rotacja, c1a, c1b, c1c, c2a, c2b, c2c, c3a, c3b, c3c, c4a, c4b, c4c );
Z karty nie znika obciążenie nawet jak się wyłączy program, a obrazy po kilkakrotnym przełączeniu między oknami zaczynają na siebie zachodzić i migać. |