cs_Tibabou
Messages postés
129
Date d'inscription
mercredi 2 janvier 2002
Statut
Membre
Dernière intervention
7 juillet 2012
2 juil. 2005 à 17:13
Je tiens à préciser que c'est Xavious qui m'a donné le code permettant de dessiner dans un controle, j'ai essayé, à partir de sa source, d'en créer un deuxième.
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASS wcls;
ZeroMemory(&wcls,sizeof(wcls));
wcls.style=CS_HREDRAW|CS_VREDRAW;
wcls.lpfnWndProc=WinProc;
wcls.hInstance=hInst;
wcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wcls.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
wcls.lpszClassName="MyWndClassName";
if (!RegisterClass(&wcls))
{
MessageBox(NULL,"Impossible d'enregistrer la classe de fenêtre.\nL'application va se terminer.","Erreur",MB_ICONERROR);
return 0;
}
hWndMain=CreateWindow("MyWndClassName","OpenGl dans un contrôle by X@v...",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,0,840,630,HWND_DESKTOP,NULL,hInst,0);
if (hWndMain==NULL)
{
MessageBox(NULL,"Impossible de créer la fenêtre.\nL'application va se terminer.","Erreur",MB_ICONERROR);
return 0;
}
hOGl=CreateWindow("STATIC","",WS_VISIBLE|WS_CHILD|WS_BORDER,10,10,400,300,hWndMain,NULL,hInst,NULL);
hOGl2=CreateWindow("STATIC","",WS_VISIBLE|WS_CHILD|WS_BORDER,420,10,400,300,hWndMain,NULL,hInst,NULL);
UpdateWindow(hWndMain);
ShowWindow(hWndMain,SW_SHOW);
if (InitOpenGl()==0)
{
MessageBox(NULL,"Impossible dinitialiser OpenGl.\nL'application va se terminer.","Erreur",MB_ICONERROR);
}
SizeOpenGL(400,300);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ReleaseOpenGl();
return 0;
}
LRESULT CALLBACK WinProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam )
{
switch(uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_SIZE:
SizeOpenGL(400,300);
break;
case WM_PAINT:
PAINTSTRUCT ps,ps1,ps2;
BeginPaint(hWndMain,&ps);
BeginPaint(hOGl,&ps1);
EndPaint(hOGl,&ps1);
BeginPaint(hOGl2,&ps2);
EndPaint(hOGl2,&ps2);
EndPaint(hWndMain,&ps);
Draw();
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return FALSE;
}
int InitOpenGl()
{
OGlDC=GetDC(hOGl);
OGlDC2=GetDC(hOGl2);
PIXELFORMATDESCRIPTOR pfd,pfd2;
int pixelformat,pixelformat2;
pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion=1;
pfd.dwFlags= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.dwLayerMask= PFD_MAIN_PLANE;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.cColorBits=16;
pfd.cDepthBits=16;
pfd.cAccumBits=0;
pfd.cStencilBits=0;
//2
pfd2.nSize=sizeof(PIXELFORMATDESCRIPTOR);
pfd2.nVersion=1;
pfd2.dwFlags= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd2.dwLayerMask= PFD_MAIN_PLANE;
pfd2.iPixelType=PFD_TYPE_RGBA;
pfd2.cColorBits=16;
pfd2.cDepthBits=16;
pfd2.cAccumBits=0;
pfd2.cStencilBits=0;
if ((pixelformat=ChoosePixelFormat(OGlDC,&pfd))==FALSE)
{
MessageBox(hWndMain,"ChoosePixelFormat : L'opération a échoué.","Erreur",MB_ICONERROR);
return 0;
}
if (SetPixelFormat(OGlDC,pixelformat,&pfd)==FALSE)
{
MessageBox(hWndMain,"SetPixelFormat : L'opération a échoué.","Erreur",MB_ICONERROR);
return 0;
}
//2
if ((pixelformat2=ChoosePixelFormat(OGlDC2,&pfd2))==FALSE)
{
MessageBox(hWndMain,"ChoosePixelFormat : L'opération a échoué.","Erreur",MB_ICONERROR);
return 0;
}
if (SetPixelFormat(OGlDC2,pixelformat2,&pfd2)==FALSE)
{
MessageBox(hWndMain,"SetPixelFormat : L'opération a échoué.","Erreur",MB_ICONERROR);
return 0;
}
MainHRC = wglCreateContext(OGlDC);
MainHRC2 = wglCreateContext(OGlDC2);
return 1;
}
void SizeOpenGL(int width,int height)
{
if (hOGl==NULL) return;
if (hOGl2==NULL) return;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,float(width)/float(height),0.1,100);
}
void dessin()
{
glBegin(GL_QUADS);
glColor3f(1.f,0.f,0.f);
glVertex2i(-1,-1);
glColor3f(0.f,1.f,0.f);
glVertex2i(1,-1);
glColor3f(0.f,0.f,1.f);
glVertex2i(1,1);
glColor3f(1.f,1.f,1.f);
glVertex2i(-1,1);
glEnd();
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
wglMakeCurrent(OGlDC,MainHRC);
gluLookAt(0,0,-10,0,0,0,0,1,0);
dessin();
SwapBuffers(OGlDC);
wglMakeCurrent(OGlDC2,MainHRC2);
gluLookAt(0,0,-10,0,0,0,0,0,1);
dessin();
SwapBuffers(OGlDC2);
}
void ReleaseOpenGl()
{
wglMakeCurrent(NULL, NULL);
if (MainHRC) wglDeleteContext(MainHRC);
ReleaseDC(hOGl,OGlDC);
wglMakeCurrent(NULL, NULL);
if (MainHRC2) wglDeleteContext(MainHRC2);
ReleaseDC(hOGl2,OGlDC2);
}
Désolé pour la longueur...
Tibabou