OPENGL ZajOpenGLok


// ==========================================================================
// 01
// --------------------------------------------------------------------------
#include
#include
#include
#include
// ==========================================================================
// 03
// --------------------------------------------------------------------------
HDC hdc;
HGLRC hrc;
// ==========================================================================
// 05
// --------------------------------------------------------------------------
hdc=GetDC( PanelOpenGL->Handle); // pobieramy kontekst wyświetlania Windows
//przygotowujemy strukture opisujaca nasze żadania do formatu wyswietlania
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,0};
//pobieramy indeks formatu wyświetlania najbardziej zblizonego do naszych żadań
int formatIndex = ChoosePixelFormat( hdc, &pfd );
//ustalamy format wyświetlania dla podanego kontekstu wyswietlania
SetPixelFormat( hdc, formatIndex, &pfd );
hrc=wglCreateContext(hdc ); // tworzymy kontekst wyświetlania dla OpneGL
wglMakeCurrent( hdc, hrc ); // ustawiamy kontekst zwiazany z Panel1
// ==========================================================================
// 06
// --------------------------------------------------------------------------
wglMakeCurrent( hdc, NULL ); // zwalniamy kontekst wyświetlania
wglDeleteContext( hrc ); // usuwamy kontekst wyświetlania OpenGL
ReleaseDC( PanelOpenGL->Handle, hdc ); // usuwamy kontekst Windows
// ==========================================================================
// 07
// --------------------------------------------------------------------------
int h=PanelOpenGL->Height;
int w=PanelOpenGL->Width;
if(h==0) h=1; // Zabezpiecznie przed dzieleniem przez zero
glViewport(0, 0, w, h); // Ustawienie widoku w obszarze roboczym okna
glMatrixMode( GL_MODELVIEW);
glLoadIdentity(); // Wyzerowanie układu współrzędnych
// ==========================================================================
// 08
// --------------------------------------------------------------------------
glClearColor( 0.0, 0.0, 0.0, 1.0); // Ustawienie koloru tła
glClear(GL_COLOR_BUFFER_BIT); // Wyczyszczenie okna
glColor3f(1.0f, 0.0f, 0.0f); // Kolor rysowania czerwony
glRectf(0.0,0.0,0.5,0.5); // Narysowanie prostokąta
glFlush(); // Wykonanie kolejki polecen
SwapBuffers(hdc); // Podmiana bufora obrazu
// ==========================================================================
// 09
// --------------------------------------------------------------------------
FormResize(Sender);
PaintBox1Paint(Sender);
// ==========================================================================
// 09 A
// --------------------------------------------------------------------------
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
// Ustalenie bryły obcinania (lewa, prawa, dolna, górna, bliższa, dalsza)
GLfloat roz=1.0f*pow(10.0,0.05*(50-SBarSkala->Position));
if (w <= h) glOrtho (-roz, roz, -roz*h/w, roz*h/w, -10.f*roz, 10.f*roz);
else glOrtho (-roz*w/h, roz*w/h, -roz, roz, -10.f*roz, 10.f*roz);
// ==========================================================================
// 11
// --------------------------------------------------------------------------
if(RadioGroup1->ItemIndex==0) glRectf(0.0,0.0,0.5,0.5);
else if(RadioGroup1->ItemIndex==1)
{
glBegin( GL_TRIANGLES);
glVertex3d( 0.0f, 0.5f, 0.0f);
glVertex3d(-0.5f, 0.0f, 0.0f);
glVertex3d( 0.5f, 0.0f, 0.0f);
glEnd();
}
// ==========================================================================
// 12
// --------------------------------------------------------------------------
Scena(Sender); // zamiast glRectf(...);
// ==========================================================================
// 13
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==2)
{
glBegin( GL_TRIANGLES);
glColor3d(0.0f,0.0f,1.0f); glVertex3d( 0.0f, 0.5f, 0.0f);
glColor3d(0.0f,1.0f,0.0f); glVertex3d(-0.5f, 0.0f, 0.0f);
glColor3d(1.0f,0.0f,0.0f); glVertex3d( 0.5f, 0.0f, 0.0f);
glEnd();
}
// ==========================================================================
// 14
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==3)
{
glColor3f(1.0f, 0.0f, 0.0f);
glBegin( GL_TRIANGLES);
glVertex3d( 0.0f, 0.5f, 0.0f);
glVertex3d(-0.5f, 0.0f, 0.0f);
glVertex3d( 0.5f, 0.0f, 0.0f);
glEnd();
glColor3f(1.0f, 1.0f, 0.0f);
glBegin( GL_TRIANGLES);
glVertex3d( 0.0f, 0.5f, 0.1f);
glVertex3d(-0.5f, 0.0f, 0.1f);
glVertex3d( 0.5f, 0.0f, 0.1f);
glEnd();
}
// ==========================================================================
// 15
// --------------------------------------------------------------------------
glPushMatrix();
GLfloat alfa= SBarAlfa->Position;
GLfloat beta= SBarBeta->Position;
GLfloat gama= SBarGama->Position;
glRotatef(alfa, 1.0f, 0.0f, 0.0f);
glRotatef(beta, 0.0f, 1.0f, 0.0f);
glRotatef(gama, 0.0f, 0.0f, 1.0f);
//glColor3f(1.0f, 0.0f, 0.0f);
//Scena(Sender);
glPopMatrix();
// ==========================================================================
// 16
// --------------------------------------------------------------------------
glEnable(GL_DEPTH_TEST);
// ==========================================================================
// 16 A
// --------------------------------------------------------------------------
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// ==========================================================================
// 17
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==4)
{
GLfloat x,y,z,angle;
for(int j=0; j<2; j++)
{
//glFrontFace(j==0?GL_CW:GL_CCW);
glBegin( GL_TRIANGLE_FAN); // Wachlarze trojkatow
glColor3f( 1.0f, 0.0f, 0.0f);
glVertex3f( 0.f, 0.f, 1.0f*j);
for( int i=0; i<=8; i++)
{
x=0.5*cos( 2*M_PI*i/8);
y=0.5*sin( 2*M_PI*i/8);
if((i+j)%2==0) glColor3f( 0.0f, 1.0f, 0.0f);
else glColor3f( 1.0f, 0.0f, 0.0f);
glVertex3f(x,y,0.0f);
}
glEnd();
}
}
// ==========================================================================
// 18
// --------------------------------------------------------------------------
if(ChBoxCieniowanie->Checked==true) glShadeModel(GL_FLAT);
else glShadeModel(GL_SMOOTH);
// ==========================================================================
// 19
// --------------------------------------------------------------------------
if(CBoxTylneSiatka->Checked==true) glPolygonMode(GL_BACK,GL_LINE);
else glPolygonMode(GL_BACK,GL_FILL);
// ==========================================================================
// 20
// --------------------------------------------------------------------------
if(ChBoxUsunNiewPow->Checked==true) glEnable(GL_CULL_FACE);
else glDisable(GL_CULL_FACE);
// ==========================================================================
// 21
// --------------------------------------------------------------------------
if(ChBoxSwiatlo->Checked==true)
{
GLfloat otoczenia[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat rozproszone[]={ 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat odbite[] ={ 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat pozycja[] ={ 150.0f, 150.0f, -50.0f, 1.0f };

glEnable(GL_LIGHTING); // włącz analizę oświetlenia
glLightfv( GL_LIGHT0, GL_AMBIENT, otoczenia); // światło oblewające
glLightfv( GL_LIGHT0, GL_DIFFUSE, rozproszone);// światło rozproszone
glLightfv( GL_LIGHT0, GL_SPECULAR, odbite); // światło odbite
glLightfv( GL_LIGHT0, GL_POSITION, pozycja); // pozycja światła
glEnable( GL_LIGHT0); // włączenie żarówki nr 0
glEnable(GL_COLOR_MATERIAL); // śledzenia koloru materiału
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);// właściwości materiału
glMaterialfv(GL_FRONT, GL_SPECULAR,odbite); //wszystkie materiały maja
glMateriali(GL_FRONT,GL_SHININESS,128); //odblysk z wysokim połyskiem
} else glDisable( GL_LIGHTING);
// ==========================================================================
// 22
// --------------------------------------------------------------------------
void Graniastoslup( int n, int kod)
{
GLfloat x0,z0,x1,z1;
for(int i=0; i<=n; i++)
{
glBegin(GL_QUADS);
x0=0.5*cos(i*2.0*M_PI/n); z0=0.5*sin(i*2.0*M_PI/n);
x1=0.5*cos((i+1)*2.0*M_PI/n); z1=0.5*sin((i+1)*2.0*M_PI/n);
if(kod==1) glNormal3f(x0+x1, 0.0f, z0+z1);
else if(kod==2) glNormal3f(x0, 0.0f, z0);
glVertex3f(x0,-0.5f, z0);
glVertex3f(x0, 0.5f, z0);
if(kod==2) glNormal3f(x1, 0.0f, z1);
glVertex3f(x1, 0.5f, z1);
glVertex3f(x1,-0.5f, z1);
glEnd();
}
}
// ==========================================================================
// 23
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==5) Graniastoslup( 6,0);
// ==========================================================================
// 24
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==6) Graniastoslup( 6,1);
// ==========================================================================
// 24 A
// --------------------------------------------------------------------------
glEnable( GL_NORMALIZE);
// ==========================================================================
// 25
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==7) Graniastoslup( 20,1);
// ==========================================================================
// 26
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==8) Graniastoslup( 20,2);
// ==========================================================================
// 27
// --------------------------------------------------------------------------
void Czasteczka( void)
{
glPushMatrix();
glColor3d( 0.0f, 1.0f, 0.0f);
glTranslatef(-0.6f,0.0f,0.0f);
auxSolidSphere(0.35f);
glPopMatrix();

glPushMatrix();
glColor3d( 0.0f, 0.6f, 0.6f);
glTranslatef( 0.6f,0.0f,0.0f);
auxSolidSphere(0.35f);
glPopMatrix();

glPushMatrix();
glColor3d( 1.0f, 0.0f, 0.0f);
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
glTranslatef( 0.0f,-0.6f,0.0f);
auxSolidCylinder(0.1f, 0.8f);
glPopMatrix();
}
// ==========================================================================
// 28
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==9) Czasteczka();
// ==========================================================================
// 29
// --------------------------------------------------------------------------
GLfloat katWidz=50.0f; // <- UWAGA!!! ZMIENNA GLOBALNA
// ==========================================================================
// 29 A
// --------------------------------------------------------------------------
if(ChBoxPerspektywa->Checked==true)
{
GLfloat fAspect = 1.0f*w/h;
gluPerspective(katWidz, fAspect, 0.1, 2000.0); // Rzutowania perspektywiczne
glTranslatef(0.0f,0.0f,-2.2f);
}
else
{
}
// ==========================================================================
// 30
//---------------------------------------------------------------------------
void Podloga( GLfloat ax, GLfloat az, int nx, int nz)
{
GLfloat x,z;
glBegin(GL_LINES);
z=0.5f*az;
for(int i=0; i<=nx; i++)
{
x=-0.5f*ax+i*ax/nx;
glVertex3f(x, 0.0f, -z);
glVertex3f(x, 0.0f, +z);
}
x=0.5f*ax;
for(int i=0; i<=nx; i++)
{
z=-0.5f*az+i*az/nz;
glVertex3f(+x, 0.0f, z);
glVertex3f(-x, 0.0f, z);
}
glEnd();
}
//---------------------------------------------------------------------------
void Filar( GLfloat hp, GLfloat hr, GLfloat a, GLfloat r , int n)
{
glPushMatrix();
glTranslatef( 0.0f, 0.5f*hp, 0.0f);
auxSolidBox(a, hp, a);
glPopMatrix();

glPushMatrix();
glTranslatef( 0.0f,1.5f*hp+hr,0.0f);
auxSolidBox(a, hp, a);
glPopMatrix();

glPushMatrix();
glTranslatef( 0.0f, hp+0.5*hr, 0.0f);
glScalef(r,hr,r);
Graniastoslup( n,1);
glPopMatrix();
}
// ==========================================================================
// 31
// --------------------------------------------------------------------------
else if(RadioGroup1->ItemIndex==10)
{
int Px[10]={-14,-3,6,12,-17,8,8,-9,-19,-8};
int Pz[10]={-4,9,-3,6,12,-17,8,8,-9,-19};
glColor3f(0.0f, 0.0f, 1.0f);
Podloga( 40.0f, 40.0f, 40, 40);
Czasteczka();
glColor3f(1.0f, 0.0f, 0.0f);
for(int i=0; i<10; i++)
{
glPushMatrix();
glTranslatef(Px[i],0,Pz[i]);
Filar(0.2,1.5, 0.8, 0.35 , 8);
glPopMatrix();
}
}
// ==========================================================================
// 32
// --------------------------------------------------------------------------
GLfloat kier=-0.5*M_PI, eyeX=0.0f, eyeZ=0.0f, eyeY=0.0f; //ZMIENNE GLOBALNE
// ==========================================================================
// 32 A
// --------------------------------------------------------------------------
if(Key==VK_HOME) eyeY+=0.2f;
else if(Key==VK_END) eyeY-=0.2f;
else if(Key==VK_PRIOR) katWidz+=(katWidz>88.0f?0.0f:2.0f);
else if(Key==VK_NEXT) katWidz-=(katWidz<4.0f?0.0f:2.0f);
else if(Key==VK_LEFT) kier-=GLfloat(M_PI/30);
else if(Key==VK_RIGHT) kier+=GLfloat(M_PI/30);
else if(Key==VK_UP) {eyeX+=GLfloat(cos(kier)); eyeZ+=GLfloat(sin(kier));}
else if(Key==VK_DOWN) {eyeX-=GLfloat(cos(kier)); eyeZ-=GLfloat(sin(kier));}
else if(Key==VK_ESCAPE)Close();
if(Key==VK_PRIOR ||Key==VK_NEXT) FormResize(Sender);
if(eyeX>20.0f) eyeX=20.0f; if(eyeX<-20.0f) eyeX=-20.0f;
if(eyeZ>20.0f) eyeZ=20.0f; if(eyeZ<-20.0f) eyeZ=-20.0f;
PaintBox1Paint(Sender);
// ==========================================================================
// 33
// --------------------------------------------------------------------------
if(ChBoxPerspektywa->Checked==true)
{
gluLookAt( eyeX, eyeY,eyeZ, eyeX+GLfloat(cos(kier)), eyeY,
eyeZ+GLfloat(sin(kier)), 0.0f, 1.0f, 0.0f);
// Ulozenie kamery: 1-3 polozenie kamery, 4-6 - punkt patrzenia
// 7-9 gora kamery
}
// --------------------------------------------------------------------------


Wyszukiwarka

Podobne podstrony:
opengl przeksztalcenia geometryczne
OpenGL narzędzie dla bardzo ambitnych
Grafika komputerowa i OpenGL
Modelowanie oświetlenia w OpenGl
opengl podstawy
opengl?finiowanie sceny=
Modelowanie oświetlenia z wykorzystaniem OpenGL
02 opengl 3 2 szablon aplikacji OpenGL
opengl
Using Opengl In Visual C
Multitexturing w OpenGL
NeHe Productions OpenGL Article #02
Multitexturing w OpenGL
OpenGL 8hpp source
NeHe Productions OpenGL Article #01

więcej podobnych podstron