Besoin d'aide en prog OpenGL - Divers - Programmation
Marsh Posté le 09-06-2004 à 16:46:46
euh franchement, là, comme ça, j'ai pas vraiment le courage d'éplucher tout ton code... tu saurais pas plus précisément ce qui ne marche pas, par hasard ? et puis utilise les balises cpp et /cpp pour que ton code soit afficher mieux que ça...
Marsh Posté le 27-05-2004 à 01:46:24
Salut, merci à ceux qui tenteront de m'aider.
Je travaille en sur OpenGL avec gestion des évènements clavier / souris Windows ( pas glut).
Mon prog est censé afficher une grille (matrice). Une sorte d'échiquier.
Si l'utilisateur fait un clic gauche sur une case, il change sa couleure en rouge, un clic droit en bleu.
Le prog marche mais il est bugué, je ne saurais vous expliker trop ce kil fait, il n'attribue pas les couleurs là ou il faut! Kelkun aurait une idée? ou une fason plus simple de faire?
Merci à vous
ci - joint le code.
/* les headers OpenGl sont généralement dans le répertoire \include\gl
du compilateur. Ces headers se rapportent aux dll opengl32.dll et
glu32.dll, il faut donc inclure les libs dans Project>Settings>Links
et les inclurent les libs dans la liste Object/library modules */
#include <windows.h> //Header Window
#include <gl/gl.h> // Header OpenGL
#include <gl/glu.h> // Header GLU
#include <iostream>
using std::cout;
WNDCLASS wc; // paramètres Windows
MSG msg;
HWND hWnd;
HDC DC;
HGLRC RC;
float x=8, y=6, z=14.5;
float a=0, b=0;
int startX = 3, startY = 6; //Location initial du départ
int targetX = 12, targetY = 6;//Location initial de la destination
const int grille = 1, depart = 2, destination = 3, bloc = 4;
const longueur = 16, largeur = 12;
int matrice[longueur][largeur];
int mouseXCoordinate, mouseYCoordinate;
int positionX =0, positionY=0;
void RePaint ()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //efface tous les pixels avant l'affichage d'une scène
glMatrixMode (GL_MODELVIEW); //** A écrire avant
glLoadIdentity (); //** les commandes de transformation
//code relatif à l'affichage
gluLookAt (x,y,z,8,6,0,0,1,0); //caméra, vu en (x,y,z, sur le point x,y,z
matrice[startX][startY]= depart;
matrice[targetX][targetY]= destination;
int i, j;
for(i=0; i<longueur; i++)
{
for(j=0; j<largeur; j++)
{
//intérieur
glBegin (GL_QUADS);
glColor3d(1,0.5,1); glVertex2i(0+i,1+j);//violet
glColor3d(1,0.5,1); glVertex2i(1+i,1+j);
glColor3d(1,0.5,1); glVertex2i(1+i,0+j);
glColor3d(1,0.5,1); glVertex2i(0+i,0+j);
glEnd ();
//Contour
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(0+i,1+j);
glColor3d(1,1,1); glVertex2i(1+i,1+j);
glEnd();
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(1+i,1+j);
glColor3d(1,1,1); glVertex2i(1+i,0+j);
glEnd ();
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(1+i,0+j);
glColor3d(1,1,1); glVertex2i(0+i,0+j);
glEnd ();
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(0+i,0+j);
glColor3d(1,1,1); glVertex2i(0+i,1+j);
glEnd ();
if(matrice[i][j] == bloc)
{
glBegin (GL_QUADS);
glColor3d(0,0,1); glVertex2i(0+i,1+j); //bleu
glColor3d(0,0,1); glVertex2i(1+i,1+j);
glColor3d(0,0,1); glVertex2i(1+i,0+j);
glColor3d(0,0,1); glVertex2i(0+i,0+j);
glEnd ();
}
if(matrice[i][j] == depart)
{
glBegin (GL_QUADS);
glColor3d(0,1,0); glVertex2i(0+i,1+j); //vert
glColor3d(0,1,0); glVertex2i(1+i,1+j);
glColor3d(0,1,0); glVertex2i(1+i,0+j);
glColor3d(0,1,0); glVertex2i(0+i,0+j);
glEnd ();
}
if(matrice[i][j] == grille)
{
//intérieur
glBegin (GL_QUADS);
glColor3d(1,0.5,1); glVertex2i(0+i,1+j);//violet
glColor3d(1,0.5,1); glVertex2i(1+i,1+j);
glColor3d(1,0.5,1); glVertex2i(1+i,0+j);
glColor3d(1,0.5,1); glVertex2i(0+i,0+j);
glEnd ();
//Contour
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(0+i,1+j);
glColor3d(1,1,1); glVertex2i(1+i,1+j);
glEnd();
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(1+i,1+j);
glColor3d(1,1,1); glVertex2i(1+i,0+j);
glEnd ();
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(1+i,0+j);
glColor3d(1,1,1); glVertex2i(0+i,0+j);
glEnd ();
glBegin(GL_LINES);
glColor3d(1,1,1); glVertex2i(0+i,0+j);
glColor3d(1,1,1); glVertex2i(0+i,1+j);
glEnd ();
}
if(matrice[i][j] == destination)
{
glBegin (GL_QUADS);
glColor3d(1,0,0); glVertex2i(0+i,1+j); //rouge
glColor3d(1,0,0); glVertex2i(1+i,1+j);
glColor3d(1,0,0); glVertex2i(1+i,0+j);
glColor3d(1,0,0); glVertex2i(0+i,0+j);
glEnd ();
}
}
}
glFlush(); //Force l'affichage
SwapBuffers (DC);
}
void InitPixelFormat (HDC hDC) //Initalise le format
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof (PIXELFORMATDESCRIPTOR),
1,
PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER,
16,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16,
0, 0, 0, 0, 0, 0, 0
};
SetPixelFormat (hDC, ChoosePixelFormat (hDC, &pfd), &pfd);
}
LRESULT CALLBACK WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ // récupère les évènements Windows (clavier / souris ...)
int xPos = LOWORD(lParam); // horizontal position of cursor
int yPos = HIWORD(lParam); // vertical position of cursor
mouseXCoordinate = xPos/50;
mouseYCoordinate = yPos/50;
switch (uMsg)
{
case WM_CREATE:
DC=GetDC (hwnd);
InitPixelFormat (DC);
RC = wglCreateContext (DC);
wglMakeCurrent (DC, RC);
break;
case WM_CLOSE:
wglMakeCurrent (NULL, NULL);
wglDeleteContext (RC);
ReleaseDC (hwnd,DC);
PostQuitMessage (0);
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;
case WM_PAINT:
RePaint ();
break;
case WM_KEYDOWN: //Clavier
switch(wParam)
{
case VK_DOWN: a-=0.2; break;
case VK_UP: a+=0.2; break;
case VK_RIGHT: b+=0.2; break;
case VK_LEFT: b-=0.2; break;
case 'E': {z+=0.1; cout<<" z = \t" <<z <<"\n"; }break;
case 'D': {z-=0.1; cout<<" z = \t" <<z <<"\n"; }break;
case 'S': {y+=1; cout<<" y = \t" <<y <<"\n"; }break;
case 'Q': {y-=1; cout<<" y = \t" <<y <<"\n"; }break;
case 'Z': {x+=1; cout<<" x = \t" <<x <<"\n"; }break;
case 'A': {x-=1; cout<<" x = \t" <<x <<"\n"; }break;
/*case VK_ENTER:
{
//Start A* pathfinding search if return/enter key is hit
if (path == notfinished) //if path not searched
{
path=FindPath(pathfinderID,startX*50,startY*50,targetX*50,targetY*50);
if (path == found) ReadPath(pathfinderID);
}
}*/
default:
break;
}
break;
//Souris
case WM_LBUTTONUP: //bouton gauche cliqué (UP)
{
if(matrice[mouseXCoordinate][mouseYCoordinate] != destination)
{
matrice[startX][startY] = grille;
startX = mouseXCoordinate;
startY = mouseYCoordinate;
matrice[startX][startY] = depart;
}
}
case WM_RBUTTONUP: //bouton droit cliqué (UP)
{
if(matrice[mouseXCoordinate][mouseYCoordinate] != depart)
{
matrice[targetX][targetY] = grille;
targetX = mouseXCoordinate;
targetY = mouseYCoordinate;
matrice[targetX][targetY] = destination;
}
}
case WM_MBUTTONDOWN: //roulette pressé (DOWN)
{
if((matrice[mouseXCoordinate][mouseYCoordinate] != depart) || (matrice[mouseXCoordinate][mouseYCoordinate] != destination))
{
//Erase walls
if (matrice[mouseXCoordinate][mouseYCoordinate] == bloc )
{
matrice[mouseXCoordinate][mouseYCoordinate] = grille;
}
//Draw walls
if (matrice[mouseXCoordinate][mouseYCoordinate] == grille)
{
matrice[mouseXCoordinate][mouseYCoordinate] = bloc;
}
}
}
default:
return DefWindowProc (hwnd,uMsg,wParam,lParam);
break;
}
return 0;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{ //Initialise les paramètres Windows de la Fenêtre
wc.style = CS_OWNDC;
wc.lpfnWndProc = WindowProc;
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(LTGRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "OGL";
RegisterClass(&wc);
hWnd = CreateWindow
("OGL", "Fenetre OpenGL",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
positionX, positionY, 772, 600, NULL, NULL, hInstance, NULL
);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}