Ce programme permet d'approfondir vos connaissances sur OpenGL.
logiciel : Borland C++ Bulder V5
Source / Exemple :
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "openGL.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
int ray=4; int hh0=0; int hh1=0; int ll=15; int tt=15; int XX=0; int YY=0;
int X0=0; int Y0=0; int ZZ=0; int Z0=0; int BMP; int BMP_MAX=6;
int hau;
int tmp=1; int g0; int g=0;
//---------------------------------------------------------------------------
__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);
// red green blue alpha
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
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 = 80;
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()
{
for (int i=1;i<BMP_MAX;i++)
{if (BMP==i) glBindTexture(GL_TEXTURE_2D, texture[i]); }
glPushMatrix();
// glColor3f(0.8f, 0.8f, 0.8f);
glTranslatef(XX, YY, ZZ);
// glTranslatef(0.0f, 15.0f, 20.0f);
glRotatef(90, 1.0, 00.0, 0.0);
glCallList(startoflist);
glPopMatrix();
//Top
glPushMatrix();
// glColor3f(0.8f, 0.8f, 0.8f);
glDisable(GL_CULL_FACE);
glTranslatef(XX, YY, ZZ);
// glTranslatef(0.0f, 15.0f, 20.0f);
glRotatef(-90, 1.0, 0.0, 0.0);
glCallList(startoflist + 1);
// glEnable(GL_CULL_FACE);
glPopMatrix();
//Bottom
glPushMatrix();
// glColor3f(0.8f, 0.8f, 0.8f);
glTranslatef(XX, -hau+YY, ZZ);
// glTranslatef(0.0f, -10.0f, 20.0f);
glRotatef(90, 1.0, 0.0, 0.0);
glCallList(startoflist + 2);
glPopMatrix();
///////////////////////////////////
GoldCube = gluNewQuadric();
GoldCubeTop = gluNewQuadric();
GoldCubeBottom = gluNewQuadric();
for (int i=1;i<BMP_MAX;i++)
{if (BMP==i) glBindTexture(GL_TEXTURE_2D,texture[i]); }
///////////////////////
gluQuadricTexture(GoldCube, GL_TRUE);
gluQuadricDrawStyle(GoldCube, GLU_FILL);
gluQuadricNormals(GoldCube, GLU_SMOOTH);
gluQuadricTexture(GoldCubeTop, GL_TRUE);
gluQuadricDrawStyle(GoldCubeTop, GLU_FILL);
gluQuadricNormals(GoldCubeTop, GLU_SMOOTH);
gluQuadricTexture(GoldCubeBottom, GL_TRUE);
gluQuadricDrawStyle(GoldCubeBottom, GLU_FILL);
gluQuadricNormals(GoldCubeBottom, GLU_SMOOTH);
glNewList(startoflist, GL_COMPILE);
gluCylinder(GoldCube, ll, tt, hau, ray,25);
glEndList();
glNewList(startoflist + 1, GL_COMPILE);
gluDisk(GoldCubeTop, hh0, tt, ray, 1);
glEndList();
glNewList(startoflist + 2, GL_COMPILE);
gluDisk(GoldCubeBottom, hh1, tt, ray,1);
glEndList();
SwapBuffers(hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
tmp=1;
if(Key == VK_UP) glRotatef(-5, 1.0, 0.0, 0.0);
if(Key == VK_DOWN) glRotatef(5 , 1.0, 0.0, 0.0);
if(Key == VK_LEFT) glRotatef(-5, 0.0, 1.0, 0.0);
if(Key == VK_RIGHT) glRotatef(5 , 0.0, 1.0, 0.0);
if(Key == char('Q')) X0=X0+5;
if(Key == char('W')) X0=X0-5;
if(Key == char('S')) Y0=Y0+5;
if(Key == char('X')) Y0=Y0-5;
if(Key == char('D')) Z0=Z0+5;
if(Key == char('C')) Z0=Z0-5;
}
//---------------------------------------------------------------------------
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, 4);
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);
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;
int nbr=6;
for (g=0;g<=nbr-1; g++) {
int DR[10][10]=
// ray hh0 hh1 ll tt || haut XX YY ZZ BMP exemples
{{4 ,0 ,0 ,50 ,50 ,50 ,100 ,0 ,0 ,3}, // Cube
{16 ,50 ,0 ,0 ,50 ,50 ,1 ,80 ,20 ,2}, // Cône
{16 ,20 ,20 ,50 ,50 ,50 ,10 ,60 ,-100 ,1}, // Cylindre percé
{3 ,50 ,0 ,0 ,50 ,50 ,-100 ,10 ,15 ,4}, // pyramide 3
{4 ,50 ,0 ,0 ,50 ,50 ,-10 ,0 ,150 ,3}, // pyramide 4
{4 ,50 ,0 ,50 ,50 ,50 ,8 ,0 ,80 ,5}, // Cube sans couvercle
};
ray=DR[g][0];
hh0=DR[g][1];
hh1=DR[g][2];
ll=DR[g][3];
tt=DR[g][4];
hau=DR[g][5];
XX=DR[g][6]+X0;
YY=DR[g][7]+Y0;
ZZ=DR[g][8]+Z0;
BMP=DR[g][9];
DrawObjects();
}
}
}
//---------------------------------------------------------------------------
Conclusion :
Mon soft sera mis à jours , selon les modifications reçu.
pas de bugs connus sur mon PC.
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.