Ogl v1.1

Description

Ce programme permet d'approfondir vos connaissances sur OpenGL.

logiciel : Borland C++ Bulder V5

Je réalise un soft qui permet de réaliser des formes géométriques, comme Cube, Cube ½, Pyramide 4, Pyramide 3, Cylindre, Cylindre ½, Cône, Sphère, Sphère ½, Trapèze, Tore, Tore ½,....... pour réaliser des personnages, des paysages,....

Appui sur la touche F10, pour le reste des touches regard mon logiciel.

Possibilité d’étirer les 4 coins du cube pour réaliser plus de forme comme
Pyramide 4, Pyramide 3, trapèze en 3D,...et de changer les images en BMP des 6 fasses
Modifie dans la source à l’adroit de : int Dr[1][31]

Source / Exemple :


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "openGL.h"
#include"math.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "Unit1.h"
TForm2 *Form2;

 int BMP1;  int BMP2;   int BMP3;  int BMP4;  int BMP5;  int BMP6; int BMP_MAX=6;
 int tmp=1;    float zoom=2;     int X=0;   int Z=0; int Y=0;

 float coul0=0.0f;
 float coul1=0.0f;
 float coul2=0.0f;
 float coul3=0.0f;

 char C0,C1;
 char XX,YY,ZZ;

  float PosX;
  float PosY;
  float PosZ;
  int Nbfasse;     // 4 8 12 16 20
  int Draw0;
  int n;
  int Dr[10][10];

int X1,X2,X3,X4,X5,X6,X7,X8;
int Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8;
int Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8;

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
    hdc = GetDC(Handle);

    PIXELFORMATDESCRIPTOR pfd = {
    sizeof(PIXELFORMATDESCRIPTOR),
    1,PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,24,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,PFD_MAIN_PLANE,0,0,0,};
    PixelFormat = ChoosePixelFormat(hdc, &pfd);
    SetPixelFormat(hdc, PixelFormat, &pfd);

    hrc = wglCreateContext(hdc);

    if(wglMakeCurrent(hdc, hrc) == false)

    w = ClientWidth;
    h = ClientHeight;

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    SetupTextures();

    GLfloat MaterialAmbient[]      = {0.5, 0.5, 0.5, 1.0};
    GLfloat MaterialDiffuse[]      = {1.0, 1.0, 1.0, 1.0};
    GLfloat MaterialSpecular[]     = {1.0, 1.0, 1.0, 1.0};
    GLfloat MaterialShininess[]    = {50.0};
    GLfloat AmbientLightPosition[] = {0.5, 1.0, 1.0, 0.0};
    GLfloat LightAmbient[]         = {0.5, 0.5, 0.5, 1.0};

    glMaterialfv(GL_FRONT,  GL_AMBIENT,   MaterialAmbient);
    glMaterialfv(GL_FRONT,  GL_DIFFUSE,   MaterialDiffuse);
    glMaterialfv(GL_FRONT,  GL_SPECULAR,  MaterialSpecular);
    glMaterialfv(GL_FRONT,  GL_SHININESS, MaterialShininess);
    glLightfv(   GL_LIGHT0, GL_POSITION,  AmbientLightPosition);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, LightAmbient);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glShadeModel(GL_SMOOTH);

    startoflist = glGenLists(4);

    }
//---------------------------------------------------------------------------
void __fastcall TForm2::FormResize(TObject *Sender)
{
    GLfloat nRange = 100;
    w = ClientWidth;
    h = ClientHeight;

    if(h == 0)  h = 1;

    //        x, y, width, height
    glViewport(0, 0, w    , h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (w <= h)
    glOrtho (-nRange*2,nRange*2,-nRange*h*2/w,nRange*h*2/w,-nRange*2,nRange*2);
    else
    glOrtho (-nRange*w*2/h,nRange*w*2/h,-nRange*2,nRange*2,-nRange*2,nRange*2);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    }
//---------------------------------------------------------------------------
void __fastcall TForm2::DrawObjects()
{
//////////////////////////// Cube  ////////////////////////////////////////////
if (Draw0==1) {
    //           C0 C1   XX  YY  ZZ
  int CU[36][6]={{0,0   ,X1 ,Y1 ,Z1},   // dessous
	         {1,0   ,X5 ,Y5 ,Z5},
                 {1,1   ,X6 ,Y6 ,Z6},
     	         {0,1   ,X2 ,Y2 ,Z2},

                 {0,0   ,X1 ,Y1 ,Z1},   //face
                 {1,0   ,X2 ,Y2 ,Z2},
                 {1,1   ,X3 ,Y3 ,Z3},
                 {0,1   ,X4 ,Y4 ,Z4},

                 {0,0   ,X2 ,Y2 ,Z2},   // droite
                 {1,0   ,X6 ,Y6 ,Z6},
                 {1,1   ,X7 ,Y7 ,Z7},
                 {0,1   ,X3 ,Y3 ,Z3},

                 {0,0   ,X1 ,Y1 ,Z1},   // gauche
                 {1,0   ,X5 ,Y5 ,Z5},
                 {1,1   ,X8 ,Y8 ,Z8},
                 {0,1   ,X4 ,Y4 ,Z4},

                 {0,0   ,X5 ,Y5 ,Z5},  //  derrière
                 {1,0   ,X6 ,Y6 ,Z6},
                 {1,1   ,X7 ,Y7 ,Z7},
                 {0,1   ,X8 ,Y8 ,Z8},

                 {0,0   ,X4 ,Y4 ,Z4},   // dessus
	         {1,0   ,X8 ,Y8 ,Z8},
                 {1,1   ,X7 ,Y7 ,Z7},
                 {0,1   ,X3 ,Y3 ,Z3}};
            ///////////////////////////////////////////////
for (int face=1; face<=6; face++) {

  if    ( face==1)  glBindTexture(GL_TEXTURE_2D, texture[BMP1]);
  if    ( face==2)  glBindTexture(GL_TEXTURE_2D, texture[BMP2]);
  if    ( face==3)  glBindTexture(GL_TEXTURE_2D, texture[BMP3]);
  if    ( face==4)  glBindTexture(GL_TEXTURE_2D, texture[BMP4]);
  if    ( face==5)  glBindTexture(GL_TEXTURE_2D, texture[BMP5]);
  if    ( face==6)  glBindTexture(GL_TEXTURE_2D, texture[BMP6]);

  glBegin(GL_QUADS);
  for (int i=0; i<=4*face;i++)
  {C0=CU[i][0];  C1=CU[i][1];  XX=CU[i][2];  YY=CU[i][3];  ZZ=CU[i][4];
   glTexCoord2i(C0,C1);glVertex3i((XX+X+PosX)*zoom,(YY+Y+PosY)*zoom,(ZZ+Z+PosZ)*zoom);
  }
 glEnd();
 }
  }
         glDisable(GL_CULL_FACE);
         SwapBuffers(hdc);

}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormKeyDown(TObject *Sender, WORD &Key,  TShiftState Shift)
{

    if(Key == VK_UP)      glRotatef(-4, 1.0, 0.0, 0.0);
    if(Key == VK_DOWN)    glRotatef(4 , 1.0, 0.0, 0.0);
    if(Key == VK_LEFT)    glRotatef(-4, 0.0, 1.0, 0.0);
    if(Key == VK_RIGHT)   glRotatef(4 , 0.0, 1.0, 0.0);

    if(Key == VK_F1)      coul0=coul0+0.01f;  //  red
    if(Key == VK_F2)      coul1=coul1+0.01f;  //  green
    if(Key == VK_F3)      coul2=coul2+0.01f;  //  blue
    if(Key == VK_F4)      coul3=coul3+0.01f;  //  alpha
    if(Key == VK_F6)      zoom=zoom-.4;
    if(Key == VK_F5)      zoom=zoom+.4;
     if(Key == VK_F10)   { Form1 = new TForm1(Application);
                           Form1->ShowModal();}
                                   

         if(Key == char('X')) X=X+5;
         if(Key == char('W')) X=X-5;
         if(Key == char('D')) Y=Y+5;
         if(Key == char('C')) Y=Y-5;
         if(Key == char('F')) Z=Z+5;
         if(Key == char('V')) Z=Z-5;

    if (coul0>=1.0f)     coul0=0.0f;
    if (coul1>=1.0f)     coul1=0.0f;
    if (coul2>=1.0f)     coul2=0.0f;
    if (coul3>=1.0f)     coul3=0.0f;
               tmp=1;
   }
//---------------------------------------------------------------------------
void __fastcall TForm2::SetupTextures()
{

              bitmap = new Graphics::TBitmap;
              GLubyte bits[64][64][4];

for ( int BMP=1; BMP <BMP_MAX; BMP++) {

         if (BMP == 1 ) bitmap->LoadFromFile("blue.bmp");
         if (BMP == 2 ) bitmap->LoadFromFile("red.bmp");
         if (BMP == 3 ) bitmap->LoadFromFile("Texture.bmp");
         if (BMP == 4 ) bitmap->LoadFromFile("Wall.bmp");
         if (BMP == 5 ) bitmap->LoadFromFile("gold.bmp");
  for(int i = 0; i < 64; i++)
    {
    	for(int j = 0; j < 64; j++)
        {
            bits[i][j][0]= (GLbyte)GetRValue(bitmap->Canvas->Pixels[i][j]);
            bits[i][j][1]= (GLbyte)GetGValue(bitmap->Canvas->Pixels[i][j]);
            bits[i][j][2]= (GLbyte)GetBValue(bitmap->Canvas->Pixels[i][j]);
            bits[i][j][3]= (GLbyte)255;
        }
    }
    glPixelStorei(GL_UNPACK_ALIGNMENT, 5);
    glGenTextures(1, &texture[BMP]);
    glBindTexture  (GL_TEXTURE_2D, texture[BMP]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

  // 	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  //	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  // 	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  //  	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

    glTexImage2D(   GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);  }

  glEnable(GL_TEXTURE_2D);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    }
//-----------------------------------------------------------------------
void __fastcall TForm2::Timer1Timer(TObject *Sender)
{
 if (tmp==1) {
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   tmp=0;

 glClearColor(coul0,coul1 ,coul2,coul3);

// Draw  PosX  PosY PosZ haute  Large  Diago Nbfasse || BMP1  BMP2  BMP3 BMP4 BMP5 BMP6

int Dr[1][31] =
// Draw | X1 Y1 Z1 | X2 Y2 Z2 | X3 Y3 Z3 | Z4 Y4 Z4 | X5 Y5 Z5 | X6 Y6 Z6 | X7 Y7 Z7 | X8 Y8 Z8 |BMP1  BMP2  BMP3 BMP4 BMP5 BMP6
     {1,  0, 0, 0,   30,0, 0   ,30 ,30,0,  0, 30,0,   0, 0, 30,  30,0, 30,  30,30,30,  0, 30,30, 1,    2,    3,   4,   1,   2};

//X1=0;  Y1=0;  Z1=0;
//X2=30; Y2=0;  Z2=0;
//X3=30; Y3=30; Z3=0;
//X4=0;  Y4=30; Z4=0;
//X5=0;  Y5=0;  Z5=30;
//X6=30; Y6=0;  Z6=30;
//X7=30; Y7=30; Z7=30;
//X8=0;  Y8=30; Z8=30;
//hauteur=Dr[n][4];   Largeur=Dr[n][5];   diago=Dr[n][6];  Nbfasse=Dr[n][7];

for ( n=0; n<=0;n++) {
  Draw0=Dr[n][0];
  X1=Dr[n][1];   Y1=Dr[n][2];    Z1=Dr[n][3];
  X2=Dr[n][4];   Y2=Dr[n][5];    Z2=Dr[n][6];
  X3=Dr[n][7];   Y3=Dr[n][8];    Z3=Dr[n][9];
  X4=Dr[n][10];  Y4=Dr[n][11];   Z4=Dr[n][12];
  X5=Dr[n][13];  Y5=Dr[n][14];   Z5=Dr[n][15];
  X6=Dr[n][16];  Y6=Dr[n][17];   Z6=Dr[n][18];
  X7=Dr[n][19];  Y7=Dr[n][20];   Z7=Dr[n][21];
  X8=Dr[n][22];  Y8=Dr[n][23];   Z8=Dr[n][24];
  BMP1=Dr[n][25];  BMP2=Dr[n][26];  BMP3=Dr[n][27];  BMP4=Dr[n][28]; BMP5=Dr[n][29];  BMP6=Dr[n][30];

           ////////////////////   cercle

//glEnable(GL_POINT_SMOOTH )    pour les points
//glEnable(GL_LINE_SMOOTH )     pour les lignes
//glEnable(GL_POLYGON_SMOOTH )  pour les polys
//glColor3d(1,0,0)     glColor3d(0.5,0.5,0.5);

                   DrawObjects();
                    }
}
}
//---------------------------------------------------------------------------

Conclusion :


pas de bugs connus sur mon PC

Codes Sources

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.