Win32 combiné avec glut...

Soyez le premier à donner votre avis sur cette source.

Vue 3 984 fois - Téléchargée 201 fois

Description

Bin, j'ai combiné glut avec l'api Win32 pour voir ce que ça donne... Plus un truc nouveau pour moi, un changement de caméra.

Dsl sur le code, je n'ai pas fait de commentaire, j'en mettrai quand j'aurai le temps.

Source / Exemple :


#include <stdio.h> 
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include <gl/glu.h>

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);
GLfloat	rtri;

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;
    
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    hWnd = CreateWindow (
      "GLSample", "Glut et l'API Win32.", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 800, 600,
      NULL, NULL, hInstance, NULL);

    EnableOpenGL (hWnd, &hDC, &hRC);

    while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glMatrixMode(GL_MODELVIEW);
            gluLookAt (0,0,-1,0,0,0,0.5,1,0);
            glRotatef(0.2,0.0f,1.0f,0.0f);

            glPushMatrix ();
            glColor3f(1.0,1.0,1.0);
            glutWireSphere(0.5,5,5);
            glPopMatrix ();

            glPushMatrix ();
            glBegin (GL_LINE_LOOP);
            glColor3f(1.0,0.0,0.0);
            glVertex3f(-0.5,-0.5,-0.5);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(0.5,0.01,0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(0.5,0.5,0.5);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(0.2,0.05,0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(-0.5,-0.5,-0.5);
            
            glColor3f(1.0,0.0,0.0);
            glVertex3f(0.1,0.1,0.1);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(-0.1,-0.01,-0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(-0.1,-0.1,-0.1);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(-0.2,-0.05,-0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(0.1,0.1,0.1);
            glEnd ();
            
            glBegin (GL_LINE_LOOP);
            glColor3f(1.0,0.0,0.0);
            glVertex3f(0.5,0.5,0.5);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(-0.5,-0.01,-0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(-0.5,-0.5,-0.5);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(-0.2,-0.05,-0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(0.5,0.5,0.5);
            
            glColor3f(1.0,0.0,0.0);
            glVertex3f(-0.1,-0.1,-0.1);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(0.1,0.01,0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(0.1,0.1,0.1);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(0.2,0.05,0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(-0.1,-0.1,-0.1);
            glEnd ();
            glPopMatrix ();
            
            
            
            glPushMatrix ();
            gluLookAt (0,0,-1,0,0,0,-0.5,1,0);
            glColor3f(1.0,1.0,1.0);
            glutWireSphere(0.5,5,5);
            glPopMatrix ();

            glPushMatrix ();
            gluLookAt (0,0,-1,0,0,0,-0.5,1,0);
            glBegin (GL_LINE_LOOP);
            glColor3f(1.0,0.0,0.0);
            glVertex3f(-0.5,-0.5,-0.5);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(0.5,0.01,0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(0.5,0.5,0.5);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(0.2,0.05,0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(-0.5,-0.5,-0.5);
            
            glColor3f(1.0,0.0,0.0);
            glVertex3f(0.1,0.1,0.1);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(-0.1,-0.01,-0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(-0.1,-0.1,-0.1);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(-0.2,-0.05,-0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(0.1,0.1,0.1);
            glEnd ();
            
            glBegin (GL_LINE_LOOP);
            glColor3f(1.0,0.0,0.0);
            glVertex3f(0.5,0.5,0.5);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(-0.5,-0.01,-0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(-0.5,-0.5,-0.5);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(-0.2,-0.05,-0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(0.5,0.5,0.5);
            
            glColor3f(1.0,0.0,0.0);
            glVertex3f(-0.1,-0.1,-0.1);
            glColor3f(0.0,1.0,0.0);
            glVertex3f(0.1,0.01,0.2);
            glColor3f(0.0,0.0,1.0);
            glVertex3f(0.1,0.1,0.1);
            glColor3f(1.0,0.0,1.0);
            glVertex3f(0.2,0.05,0.03);
            glColor3f(0.0,1.0,1.0);
            glVertex3f(-0.1,-0.1,-0.1);
            glEnd ();
            glPopMatrix ();
                        
            SwapBuffers (hDC);
        }
    }
    DisableOpenGL (hWnd, hDC, hRC);

    DestroyWindow (hWnd);

    return msg.wParam;
}

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_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}

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; iFormat = ChoosePixelFormat (*hDC, &pfd); SetPixelFormat (*hDC, iFormat, &pfd);
  • hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC ); } void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) { wglMakeCurrent (NULL, NULL); wglDeleteContext (hRC); ReleaseDC (hWnd, hDC); }

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

cs_beg0
Messages postés
52
Date d'inscription
lundi 17 janvier 2005
Statut
Membre
Dernière intervention
13 mars 2011
1 -
j'avoue que je vois pas trop l'utilitée de glut, a part glutWireSphere() tu utilise pas grand chose de glut, donc je trouve le titre un peu trompeur, mais sinon c'est un bon code d'exemple pour commencer a se familiariser avec ogl sous windows ;)
Arkain
Messages postés
31
Date d'inscription
jeudi 28 août 2003
Statut
Membre
Dernière intervention
13 décembre 2003
-
lol^^
cs_Kirua
Messages postés
3006
Date d'inscription
dimanche 14 avril 2002
Statut
Membre
Dernière intervention
31 décembre 2008
-
ah ok, un esprit expérimental, j'aime :-D
Arkain
Messages postés
31
Date d'inscription
jeudi 28 août 2003
Statut
Membre
Dernière intervention
13 décembre 2003
-
arf kirua, j'ai fait ça juste pour voir si c'était compatible, rien d'autre...
cs_Kirua
Messages postés
3006
Date d'inscription
dimanche 14 avril 2002
Statut
Membre
Dernière intervention
31 décembre 2008
-
je ne comprends pas très bien quel avantage tu en tires.
glut portable, win32 pas portable. le seul avantage de glut étant d'ailleurs sa portabilité, puisque désormais plus suivi (depuis 1997 quand même)
comprends pas, tu m'expliques? parce que par exemple, la gestion clavier sous glut est pu*nte! mais sous windows elle rocks :-)

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.