Open Gl pépin

DarkNavius Messages postés 2 Date d'inscription samedi 8 mars 2008 Statut Membre Dernière intervention 22 mars 2008 - 22 mars 2008 à 14:40
luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 - 25 mars 2008 à 17:56
Chères personnes lisant mon message d'appel à l'aide.
J'ai besoin d'aide au sujet d'un programme en opengl que j'ai construit en copiant et collant quelques bouts de codes venant de droite à gauche. Malheureusement le code ne marche point: le compilateur compile mais la fenetre ouverte est noire.

J'aimerais l'aide de quelqu'un qui s'y connais un peu pour corriger le bug.
Voici mon code source: attention il est long
Merci d'avance à qui compte m'aidera.

#include <math.h>
#include <stdlib.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

void EnableOpenGL  (HWND hwnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hwnd, HDC *hDC, HGLRC* hRC);
void RePaint       (HWND hwnd);

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);
void EnableOpenGL        (HWND hwnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL       (HWND hwnd, HDC *hDC, HGLRC* hRC);
BOOL Resolution          ();

void RePaint (HWND hwnd)
{
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();

    gluLookAt (7,7,-10,0,0,0,0,1,0);

    glEnable(GL_DEPTH_TEST);

    glBegin (GL_QUADS);
        glColor3d (0,1,1);glVertex3i (-1,-1,1);
        glColor3d (1,0,0);glVertex3i (-1,1,1);
        glColor3d (0,1,0);glVertex3i (1,1,1);
        glColor3d (1,0,1);glVertex3i (1,-1,1);

        glColor3d (0,0,1);glVertex3i (-1,-1,-1);
        glColor3d (1,1,0);glVertex3i (-1,1,-1);
        glColor3d (1,1,1);glVertex3i (1,1,-1);
        glColor3d (.5,.5,.5);glVertex3i (1,-1,-1);

        glColor3d (1,1,0);glVertex3i (-1,1,-1);
        glColor3d (1,1,1);glVertex3i (1,1,-1);
        glColor3d (0,1,0);glVertex3i (1,1,1);
        glColor3d (1,0,0);glVertex3i (-1,1,1);

        glColor3d (0,0,1);glVertex3i (-1,-1,-1);
        glColor3d (.5,.5,.5);glVertex3i (1,-1,-1);
        glColor3d (1,0,1);glVertex3i (1,-1,1);
        glColor3d (0,1,1);glVertex3i (-1,-1,1);

        glColor3d (0,0,1);glVertex3i (-1,-1,-1);
        glColor3d (0,1,1);glVertex3i (-1,-1,1);
        glColor3d (1,0,0);glVertex3i (-1,1,1);
        glColor3d (1,1,0);glVertex3i (-1,1,-1);

        glColor3d (.5,.5,.5);glVertex3i (1,-1,-1);
        glColor3d (1,0,1);glVertex3i (1,-1,1);
        glColor3d (0,1,0);glVertex3i (1,1,1);
        glColor3d (1,1,1);glVertex3i (1,1,-1);
    glEnd();

    glBegin (GL_QUADS);
    glColor3d (1,0,0);
        glVertex3i (-6,-1,1);
        glVertex3i (-6,1,1);
        glVertex3i (-4,1,1);
        glVertex3i (-4,-1,1);
    glColor3d (0,1,0);
        glVertex3i (-6,-1,-1);
        glVertex3i (-6,1,-1);
        glVertex3i (-4,1,-1);
        glVertex3i (-4,-1,-1);
    glColor3d (1,1,0);
        glVertex3i (-6,1,-1);
        glVertex3i (-4,1,-1);
        glVertex3i (-4,1,1);
        glVertex3i (-6,1,1);
    glColor3d (0,0,0);
        glVertex3i (-6,-1,-1);
        glVertex3i (-4,-1,-1);
        glVertex3i (-4,-1,1);
        glVertex3i (-6,-1,1);
    glColor3d (0,1,1);
        glVertex3i (-6,-1,-1);
        glVertex3i (-6,-1,1);
        glVertex3i (-6,1,1);
        glVertex3i (-6,1,-1);
    glColor3d (1,0,1);
        glVertex3i (-4,-1,-1);
        glVertex3i (-4,-1,1);
        glVertex3i (-4,1,1);
        glVertex3i (-4,1,-1);
    glEnd();

    glDisable(GL_DEPTH_TEST);

    glBegin (GL_QUADS);
        glColor3d (0,1,1);glVertex3i (4,-1,1);
        glColor3d (1,0,0);glVertex3i (4,1,1);
        glColor3d (0,1,0);glVertex3i (6,1,1);
        glColor3d (1,0,1);glVertex3i (6,-1,1);

        glColor3d (0,0,1);glVertex3i (4,-1,-1);
        glColor3d (1,1,0);glVertex3i (4,1,-1);
        glColor3d (1,1,1);glVertex3i (6,1,-1);
        glColor3d (.5,.5,.5);glVertex3i (6,-1,-1);

        glColor3d (1,1,0);glVertex3i (4,1,-1);
        glColor3d (1,1,1);glVertex3i (6,1,-1);
        glColor3d (0,1,0);glVertex3i (6,1,1);
        glColor3d (1,0,0);glVertex3i (4,1,1);

        glColor3d (0,0,1);glVertex3i (4,-1,-1);
        glColor3d (.5,.5,.5);glVertex3i (6,-1,-1);
        glColor3d (1,0,1);glVertex3i (6,-1,1);
        glColor3d (0,1,1);glVertex3i (4,-1,1);

        glColor3d (0,0,1);glVertex3i (4,-1,-1);
        glColor3d (0,1,1);glVertex3i (4,-1,1);
        glColor3d (1,0,0);glVertex3i (4,1,1);
        glColor3d (1,1,0);glVertex3i (4,1,-1);

        glColor3d (.5,.5,.5);glVertex3i (6,-1,-1);
        glColor3d (1,0,1);glVertex3i (6,-1,1);
        glColor3d (0,1,0);glVertex3i (6,1,1);
        glColor3d (1,1,1);glVertex3i (6,1,-1);
    glEnd();

    //SwapBuffers (DC);
    SwapBuffers (GetDC (hwnd));
}

/////////////////////Lancement et destruction de OpenGl/////////////////////////
void EnableOpenGL (HWND hwnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    *hDC = GetDC (hwnd);

    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
   
    SetPixelFormat (*hDC, ChoosePixelFormat (*hDC, &pfd), &pfd);

    *hRC = wglCreateContext(*hDC);
    wglMakeCurrent(*hDC, *hRC);

}
void DisableOpenGL (HWND hwnd, HDC *hDC, HGLRC *hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (*hRC);
    ReleaseDC (hwnd, *hDC);
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
HINSTANCE hInst;
int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    if(Resolution()==FALSE)
    {
         return 1;                     
    }
////////////Définition des variables paramétrant la fenêtre Windows////////////
    WNDCLASS wc;
    HWND     hwnd;
    HDC      hDC;
    HGLRC    hRC;       
    MSG      msg;
    RECT     rc;
   
    BOOL bQuit = FALSE;
    hInst      = hInstance;
/////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////   

////////////////////Attribution des paramètres de base/////////////////////////
    wc.style         = CS_OWNDC;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInst;
    wc.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = NOMSYTEME;
    RegisterClass (&wc);
   
    hwnd = CreateWindow (NOMSYTEME, NOMSYTEME,
                         WS_BORDER      | WS_MINIMIZEBOX | WS_CAPTION |
                         WS_POPUPWINDOW | WS_VISIBLE,
                         POSITION_X, POSITION_Y,
                         TAILLE_X,   TAILLE_Y,
                         NULL, NULL, hInst, NULL);

   
   
    EnableOpenGL (hwnd, &hDC, &hRC);
/////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////   

///Cette fonction s'assure que l'on ait bien la fenêtre de Windows visible/////
    ShowWindow(hwnd, SW_NORMAL);
//////////////////////////////Boucle du processus//////////////////////////////
    while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
///////////La fonction servant à dessiner sur la fenêtre est appellé///////////
             RePaint(hwnd);
        }
    }
/////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////   

//////////////////Gestion de la fermeture du programme//////////////////////////  
    DisableOpenGL (hwnd, &hDC, &hRC);
    DestroyWindow (hwnd);
/////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////   
    return msg.wParam;
}

/////////////////Gestion des messages internes//////////////////////////////////
LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;
    case WM_PAINT:
        RePaint (hwnd);
    case WM_DESTROY:
        return 0;

//////////////////////Gestion du clavier de l'ordinateur////////////////////////
    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        case VK_SPACE:
        
            break;
        case VK_DOWN:
            break;
        case VK_UP:
            break;
        case VK_SUBTRACT:
            break;
        case VK_ADD:
            break;
        case VK_LEFT:
            break;
        case VK_RIGHT:
            break;
        case VK_F5:
            break;
        case VK_F6:
            break;
        case VK_F7:
            break;
        case VK_F8:
            break;
        }
        break;   
   
//////////////////////////////Gestion de la souris/////////////////////////////
    case WM_MOUSEMOVE:
           
        if(wParam & MK_LBUTTON)
        {
              
        }
        break;   
   
///////////////////////////////////////////////////////////////////////////////   
    case WM_SIZE:
        glViewport (0,0,LOWORD (lParam),HIWORD (lParam));
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        gluPerspective (45,(float)(LOWORD(lParam))/(float)(HIWORD(lParam)),1,100);
       
        break;
      
        return 0;
    default:
        return DefWindowProc (hwnd, message, wParam, lParam);
    }
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

//////Cette fonction s'assure que les paramètres de résolution sont corrects////
BOOL Resolution()
{    
     if(GetSystemMetrics(SM_CYSCREEN)!=RESOLUTION_Y)
     {
          MessageBox(NULL, TEXT("Impossible de lancer l'application.\n"
                                "Les paramètres de résolution de l'écran ou "
                                "de l'ordinateur ne sont pas compatibles\n\n"
                                "Résolution adéquate: 1280*800"),
          TEXT("Erreur de résolution"), MB_OK | MB_ICONERROR);
          return FALSE;
     }
     if(GetSystemMetrics(SM_CXSCREEN)!=RESOLUTION_X)
     {
          MessageBox(NULL, TEXT("Impossible de lancer l'application.\n"
                                "Les paramètres de résolution de l'écran ou "
                                "de l'ordinateur ne sont pas compatibles\n\n"
                                "Résolution adéquate: 1280*800"),
          TEXT("Erreur de résolution"), MB_OK | MB_ICONERROR);
          return FALSE;
     }
     return TRUE;
}

DarkNavius

3 réponses

luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
22 mars 2008 à 17:36
Et si tu mets ca, t'as une fenetre rouge ?

void RePaint (HWND hwnd)
{
    glClearColor(1.0f, 0.0f, 0.0f, 1.0);
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
...
0
DarkNavius Messages postés 2 Date d'inscription samedi 8 mars 2008 Statut Membre Dernière intervention 22 mars 2008
22 mars 2008 à 17:43
Oui [auteur/LUHTOR/15482.aspx luhtor]

DarkNavius
0
luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
25 mars 2008 à 17:56
dans ce cas, c'est simplement un problème de paramétrage de matrice (de ta caméra).
0
Rejoignez-nous