IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

Programmation OpenGL sous Dev-C++


Sujet :

OpenGL

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 14
    Points : 8
    Points
    8
    Par défaut Programmation OpenGL sous Dev-C++
    Bonsoir,

    Je suis nouveau sur le forum,je suis en fin de DUT informatiques et je souhaiterai obtenir de l'aide pour un projet en OpenGL

    Programmant en C++ ien que ce soit avec OpenGL,je met ma question ici , n'hésiter pas à déplacer si je me suis trompé

    Je dois réaliser un mini projet 3D en OpenGL qui doit comporte : mouvement de caméra 3D,lumiere,rotation,texturing,translation,objets 3D

    J'ai décidé de modéliser une rue avec des objets divers tel que des billes

    J'avais fais une préversion mais elle ne comportait pas de déplacement 3D,j'ai pris donc une grosse partie d'un code venant du site GLinFrench du chapitre 10 pour des déplacements 3D

    Or quand j'ai intégré mon code contenant la modélisation des objets,ils se sont retrouvé a l'envers, et j'ai un probleme de profondeur de caméra, mes objets disparaissent au bout d'une certaine profondeur je ne comprend pas !

    voici le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    #include <windows.h>
    #include <math.h>
    #include <gl\gl.h>
    #include <gl\glu.h>
    #include <gl\glaux.h>
    #include <gl\glext.h>
     
     WNDCLASS wc;
        MSG msg;
        HWND hWnd;
        HDC	DC;
        HGLRC RC;
     
    HINSTANCE hInstance;
     
    bool touche[256];
     
    GLsizei width,height;
     
    bool fullscreen=TRUE;
    bool touchef;
     
    float pi = 3.141592654f;
    float xpos;
    float zpos;
     
    float yrot;
    float rotationx;
    float z;
     
    unsigned filter;
    unsigned texture[5];
     
    //Definit un entier qui contiendra le numéro de la texture   
        GLuint sol;
        GLuint caisse;
        GLuint mur;
        GLuint murdefond;
        GLuint fenetre;
        GLuint fond;
     
    void LoadTexture()
    {
     
    	AUX_RGBImageRec *texture1;
        texture1 = auxDIBImageLoad("machin.bmp");
     
        glGenTextures (5, &texture[0]);
     
    	glBindTexture(GL_TEXTURE_2D, texture[0]);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0,
    	GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
     
        glBindTexture(GL_TEXTURE_2D, texture[1]);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0,
    	GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
     
    	glBindTexture(GL_TEXTURE_2D, texture[2]);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture1->sizeX, texture1->sizeY,
    	GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
     
    	glBindTexture(GL_TEXTURE_2D, texture[3]);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture1->sizeX, texture1->sizeY,
    	GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
     
    	glBindTexture(GL_TEXTURE_2D, texture[4]);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.0f);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture1->sizeX, texture1->sizeY,
    	GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
     
        free(texture1->data);
        free(texture1);
     
        glGenTextures (1, &sol);
            AUX_RGBImageRec* image;
            image = auxDIBImageLoad("bit.bmp" );
            glBindTexture (GL_TEXTURE_2D, sol);
           	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        	glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data);
     
        	 glGenTextures (1, &mur);
            AUX_RGBImageRec* image2;
            image2 = auxDIBImageLoad("neHe.bmp" );
            glBindTexture (GL_TEXTURE_2D, mur);
           	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        	glTexImage2D(GL_TEXTURE_2D, 0, 3, image2->sizeX, image2->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image2->data);
     
        	glGenTextures (1,&caisse);
            AUX_RGBImageRec* image3;
            image3 = auxDIBImageLoad("Crate.bmp" );
            glBindTexture (GL_TEXTURE_2D, caisse);
           	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        	glTexImage2D(GL_TEXTURE_2D, 0, 3, image3->sizeX, image3->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image3->data);
     
        	glGenTextures (1,&murdefond);
     	    AUX_RGBImageRec* image4;
            image4 = auxDIBImageLoad("brique2.bmp" );
            glBindTexture (GL_TEXTURE_2D, murdefond);
           	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        	glTexImage2D(GL_TEXTURE_2D, 0, 3, image4->sizeX, image4->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image4->data);
     
        	glGenTextures (1,&fenetre);
     	    AUX_RGBImageRec* image5;
            image5 = auxDIBImageLoad("fenetre.bmp" );
            glBindTexture (GL_TEXTURE_2D, fenetre);
           	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        	glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        	glTexImage2D(GL_TEXTURE_2D, 0, 3, image5->sizeX, image5->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image5->data);
     
    }
     
    void ReSize(GLsizei width, GLsizei height)
    {
    	glViewport(0,0,width,height);
     
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
     
    	gluPerspective (45,(float)width/(float)height,1,100);
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
     
    void InitGL()
    {
    	LoadTexture();
     
    	glEnable(GL_TEXTURE_2D);
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	glEnable(GL_DEPTH_TEST);
    	glShadeModel(GL_SMOOTH);
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    }
     
    int RePaint()
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
     
    	float rotationy = 360.0f - yrot;
     
    	glRotatef(rotationx,1.0f,0,0);
    	glRotatef(rotationy,0,1.0f,0);
     
    	glTranslatef(-xpos, -0.25f, -zpos);
    	glBindTexture(GL_TEXTURE_2D, texture[filter]);
     
    	   //Creation du mur du fond
             glBindTexture(GL_TEXTURE_2D,murdefond);
     
        glBegin (GL_QUADS);
    		glTexCoord2i(1,0);glVertex3i (-50,-50,50);
    		glTexCoord2i(1,1);glVertex3i (-50,50,50);
    		glTexCoord2i(0,1);glVertex3i (50,50,50);
    		glTexCoord2i(0,0);glVertex3i (50,-50,50);
     
     
     
    	glEnd();
     
     
     
     
     
     
    	return TRUE;
    }
     
    void Shutdown()
    {
    	if (fullscreen)
    	{
    		ChangeDisplaySettings(NULL,0);
    		ShowCursor(TRUE);
    	}
     
    	wglMakeCurrent(NULL,NULL);
     
    	wglDeleteContext(RC);
    	RC=NULL;
     
    	ReleaseDC(hWnd,DC);
    	DC=NULL;
     
    	DestroyWindow(hWnd);
    	hWnd=NULL;
     
    	UnregisterClass("OpenGL", hInstance);
    	hInstance=NULL;
    }
     
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam,LPARAM	lParam)
    {
    	switch (uMsg)
    	{
    		case WM_SYSCOMMAND:
    		{
    			switch (wParam)
    			{
    				case SC_SCREENSAVE:
    				case SC_MONITORPOWER:
    				return 0;
    			}
    			break;
    		}
     
    		case WM_CLOSE:
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
     
    		case WM_KEYDOWN:
    		{
    			touche[wParam] = TRUE;
    			return 0;
    		}
     
    		case WM_KEYUP:
    		{
    			touche[wParam] = FALSE;
    			return 0;
    		}
     
    		case WM_SIZE:
    		{
    			ReSize(LOWORD(lParam),HIWORD(lParam));
    			return 0;
    		}
    	}
     
    	return DefWindowProc(hWnd,uMsg,wParam,lParam);
    }
     
    BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
    {
    	WNDCLASS	wc;
    	DWORD		dwExStyle;
    	DWORD		dwStyle;
    	RECT		WindowRect;
    	WindowRect.left=(long)0;
    	WindowRect.right=(long)width;
    	WindowRect.top=(long)0;
    	WindowRect.bottom=(long)height;
    	fullscreen=fullscreenflag;
     
    	hInstance			= GetModuleHandle(NULL);
    	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    	wc.lpfnWndProc		= (WNDPROC) WndProc;
    	wc.cbClsExtra		= 0;
    	wc.cbWndExtra		= 0;
    	wc.hInstance		= hInstance;
    	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);
    	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground	= NULL;
    	wc.lpszMenuName		= NULL;
    	wc.lpszClassName	= "OpenGL";
     
    	RegisterClass(&wc);
     
    	if (fullscreen)
    	{
    		DEVMODE dmScreenSettings;
    		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
    		dmScreenSettings.dmSize=sizeof(dmScreenSettings);
    		dmScreenSettings.dmPelsWidth	= width;
    		dmScreenSettings.dmPelsHeight	= height;
    		dmScreenSettings.dmBitsPerPel	= bits;
    		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
     
    		ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN);
    	}
     
    	if (fullscreen)
    	{
    		dwExStyle=WS_EX_APPWINDOW;
    		dwStyle=WS_POPUP;
    	}
    	else
    	{
    		dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    		dwStyle=WS_OVERLAPPEDWINDOW;
    	}
     
    	ShowCursor(TRUE);
     
    	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
     
    	hWnd=CreateWindowEx(dwExStyle, "OpenGL", title,	dwStyle | WS_CLIPSIBLINGS |	WS_CLIPCHILDREN,
    						0, 0, WindowRect.right-WindowRect.left,	WindowRect.bottom-WindowRect.top,
    						NULL, NULL,	hInstance, NULL);
     
    	static PIXELFORMATDESCRIPTOR pfd=
    	{
    		sizeof(PIXELFORMATDESCRIPTOR),
    		1,
    		PFD_DRAW_TO_WINDOW |
    		PFD_SUPPORT_OPENGL |
    		PFD_DOUBLEBUFFER,
    		PFD_TYPE_RGBA,
    		bits,
    		0, 0, 0, 0, 0, 0,
    		0,
    		0,
    		0,
    		0, 0, 0, 0,
    		16,
    		0,
    		0,
    		PFD_MAIN_PLANE,
    		0,
    		0, 0, 0
    	};
     
    	DC=GetDC(hWnd);
     
    	SetPixelFormat(DC,ChoosePixelFormat(DC,&pfd),&pfd);
     
    	RC=wglCreateContext(DC);
     
    	wglMakeCurrent(DC,RC);
     
    	ShowWindow(hWnd,SW_SHOW);
    	SetForegroundWindow(hWnd);
    	SetFocus(hWnd);
    	ReSize(width, height);
     
    	InitGL();
     
    	return TRUE;
    }
     
    int WINAPI WinMain(HINSTANCE hInstance,	HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	MSG		msg;
    	BOOL	done=FALSE;
     
    	if (MessageBox(NULL,"Mode plein écran ?", "Plein écran ?",MB_YESNO|MB_ICONQUESTION)==IDNO)
    	{
    		fullscreen=FALSE;
    	}
     
    	CreateGLWindow("Chapitre 10",640,480,16,fullscreen);
     
    	while(!done)
    	{
    		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    			if (msg.message==WM_QUIT)
    			{
    				done=TRUE;
    			}
    			else
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    		}
    		else
    		{
    			if (!RePaint() || touche[VK_ESCAPE])
    			{
    				done=TRUE;
    			}
    			else
    			{
    				SwapBuffers(DC);
     
    				if (touche['F'] && !touchef)
    				{
    					touchef=TRUE;
    					filter+=1;
    					if (filter>4)
    					{
    						filter=0;
    					}
    				}
     
    				if (!touche['F'])
    				{
    					touchef=FALSE;
    				}
     
    				if (touche[VK_UP])
    				{
    					xpos -= (float)sin(yrot*(pi/180)) * 0.1f;
    					zpos -= (float)cos(yrot*(pi/180)) * 0.1f;
    				}
     
    				if (touche[VK_DOWN])
    				{
    					xpos += (float)sin(yrot*(pi/180)) * 0.1f;
    					zpos += (float)cos(yrot*(pi/180)) * 0.1f;
    				}
     
    				if (touche[VK_RIGHT])
    				{
    					yrot -= 0.05f;
    				}
     
    				if (touche[VK_LEFT])
    				{
    					yrot += 0.05f;
    				}
     
    				if (touche[VK_PRIOR])
    				{
    					z-=0.02f;
    					rotationx-= 0.05f;
    				}
     
    				if (touche[VK_NEXT])
    				{
    					z+=0.02f;
    					rotationx += 0.05f;
    				}
    			}
    		}
    	}
     
    	Shutdown();
    	return (msg.wParam);
    }
    Le code est lourd mais j'aimerai une explication par rapport aux objets qui disparaissent au bout d'une certaines profondeur !

    Merci d'avance

  2. #2
    Membre expert

    Avatar de IrmatDen
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 727
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 727
    Points : 3 266
    Points
    3 266
    Par défaut
    Salut,

    La distance d'affichage est définie ici grâce à la fonction gluPerspective. Les 2 derniers arguments spécifient la profondeur minimale et maximale; les objets sont visibles lorsqu'ils sont situés entre les 2.
    Lorsqu'on s'en référe à ton code, les objets ne sont visibles que s'ils sont situés entre la 1ère et la 100ème unité.
    Augmente donc la seconde valeur si tu veux voir plus loin.

    Pour ce qui est des objets à l'envers, regarde du côté des 2 appels à glRotatef. C'est eux qu'ils faut que tu modifies comme nécessaire.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 14
    Points : 8
    Points
    8
    Par défaut
    Merci IrmatDen grace à gluPerspective j'ai pu allonger la portée de la caméra

    Par contre pour les objets qui étaient à l'envers j'ai changé leurs coordonnées

    Je galere pour dessiner un grand mur de fond avec cette méthode

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 14
    Points : 8
    Points
    8
    Par défaut
    J'aimerai créer une sphere lumineuse,c'est dire qu'on pourrait déplacer et qui se comporterait comme une ampoule !

    Mais comment est-il possible de mettre la lumiere dans la sphere?Car je ne sais pas d'ou vient exactement la lumiere..;

Discussions similaires

  1. Réponses: 7
    Dernier message: 02/09/2014, 15h28
  2. Réponses: 2
    Dernier message: 16/01/2009, 16h19
  3. Problème de link ODE/OpenGL sous Dev-cpp
    Par Milanber9999 dans le forum ODE
    Réponses: 3
    Dernier message: 09/05/2007, 01h46
  4. excecution d'un programme sous dev c++
    Par balgor dans le forum Dev-C++
    Réponses: 3
    Dernier message: 01/10/2006, 16h11
  5. Réponses: 5
    Dernier message: 21/11/2004, 18h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo