Masala 2: OpenGL yoritish modeli
1. Ushbu dastur OpenGL modelini ishlatishda oddiy sahna orqali tor, kub, konus va shardan tuzilgan bo‘ladi. Obyekt qilib har xil materiallarni olamiz. Albatta sahnada yorug‘lik manbai bo‘ladi.
#include
#include
/*tor materialining parametrlari*/
float mat1_dif[]={0.8f,0.8f,0.0f};
float mat1_amb[]= {0.2f,0.2f,0.2f};
float mat1_spec[]={0.6f,0.6f,0.6f};
float mat1_shininess=0.5f*128;
/* kub materialining parametrlari */
float mat2_dif[]={0.2f,0.6f,0.0f,0.0f};
float mat2_amb[]= {0.4f,0.4f,0.2f,0.0f};
float mat2_spec[]={0.4f,0.6f,0.4f,0.0f};
float mat2_shininess=0.3f*128;
/* shar materialining parametrlari */
float mat3_dif[]={0.9f,0.2f,0.0f};
float mat3_amb[]= {0.2f,0.2f,0.2f};
float mat3_spec[]={0.6f,0.6f,0.6f};
float mat3_shininess=0.1f*128;
/* konus materialining parametrlari */
float mat4_dif[]={0.0f,0.0f,0.8f};
float mat4_amb[]= {0.2f,0.2f,0.2f};
float mat4_spec[]={0.6f,0.6f,0.6f};
float mat4_shininess=0.7f*128;
/* yorug‘lik manbai va materiallar parametrlarini inisiallashtirish */
void init (void)
{
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
/* yorug‘lik manbaining parametrlarini o‘rnatish */
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
/* yorug‘lik manbai va yoritgichni qo‘shish va yoqish */
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
/* z-buferni yoqamiz*/
glEnable(GL_DEPTH_TEST);
}
/* Tasvirlarni chizish uchun kerak bo‘ladigan funksiyani chaqirish. Unda barcha geometrik shakllarni chiqarish amalga oshiriladi. */
void display (void)
{
/* kadr buferi va chuqurlik buferini tozalash*/
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glRotatef (45.0, 1.0, 0.0, 0.0);
/* tor tasviri*/
glMaterialfv (GL_FRONT,GL_AMBIENT,mat1_amb);
glMaterialfv (GL_FRONT,GL_DIFFUSE,mat1_dif);
glMaterialfv (GL_FRONT,GL_SPECULAR,mat1_spec);
glMaterialf (GL_FRONT,GL_ShININESS,mat1_shininess);
glPushMatrix ();
glTranslatef (-0.75, 0.5, 0.0);
glRotatef (90.0, 1.0, 0.0, 0.0);
glutSolidTorus (0.275, 0.85, 15, 15);
glPopMatrix ();
/*kub tasviri */
glMaterialfv (GL_FRONT,GL_AMBIENT,mat2_amb);
glMaterialfv (GL_FRONT,GL_DIFFUSE,mat2_dif);
glMaterialfv (GL_FRONT,GL_SPECULAR,mat2_spec);
glMaterialf (GL_FRONT,GL_ShININESS,mat2_shininess);
glPushMatrix();
glTranslatef(1,-0.5,1);
glRotatef(901, 1.0, 0.0, 0.0);
glutSolidCube(1);
glPopMatrix();
/* shar tasviri */
glMaterialfv (GL_FRONT,GL_AMBIENT,mat3_amb);
glMaterialfv (GL_FRONT,GL_DIFFUSE,mat3_dif);
glMaterialfv (GL_FRONT,GL_SPECULAR,mat3_spec);
glMaterialf (GL_FRONT,GL_ShININESS,mat3_shininess);
glPushMatrix ();
glTranslatef (0.75, 0.0, -1.0);
glutSolidSphere (1.0, 25, 25);
glPopMatrix ();
/*konus tasviri */
glMaterialfv (GL_FRONT,GL_AMBIENT,mat4_amb);
glMaterialfv (GL_FRONT,GL_DIFFUSE,mat4_dif);
glMaterialfv (GL_FRONT,GL_SPECULAR,mat4_spec);
glMaterialf (GL_FRONT,GL_ShININESS,mat4_shininess);
glPushMatrix ();
glTranslatef (-0.75, -0.5, 0.0);
glRotatef (270.0, 1.0, 0.0, 0.0);
glutSolidCone (1.0, 2.0, 15, 15);
glPopMatrix ();
glPopMatrix ();
/*ekranga sa?nani chi?arish */
glFlush ();
}
/* foydalanuvchi tomonidan chaqiriladigan oyna o‘lchamlarini o‘zgartiruvchi funksiya */
void reshape(int w, int h)
{
/* oyna o‘lchamiga teng bo‘lgan chiqarish sohasi o‘lchamini o‘rnatish */
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
/* oyna o‘lchamini hisobga olib proeksiyalar matritsasini beramiz */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(
40.0, /* gradusda berilgan ko‘rish burchagi */
(GLfloat)w/h, /*oynaning siqilish koeffisienti */
1,100.0); /*tekislik kesimigacha masofa*/
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt(
0.0f,0.0f,8.0f, /* kamera holati */
0.0f,0.0f,0.0f, /* sahnalar markazi */
0.0f,1.0f,0.0f); /* y o‘qining musbat yo‘nalishi*/
}
/* klaviaturadagi klavishlarni bosganda chaqiriladigan funksiya */
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: /* escape */
exit(0);
break;
}
}
/* Ilovaning asosiy tsikli.
* Chuqurlik buferi bilan ekran rejimi o‘rnatiladi, oyna yaratiladi. */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
| GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc (display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
Do'stlaringiz bilan baham: |