2. Ushbu dastur OpenGL muhitida beshta kubdan tashkil topgan va markaziy kubdan bir xil masofada yotuvchi kublardan tuzilgan bo‘ladi. Albatta sahnada yorug‘lik manbai bo‘ladi.
#include
void init()
{
/* z-buferni yoqamiz*/
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
}
/* Tasvirlarni chizish uchun kerak bo‘ladigan funksiyani chaqirish. Unda barcha kublarni chiqarish amalga oshiriladi. */
void display()
{
/* kadr buferi va chuqurlik buferini tozalash*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* oyna o‘lchamini hisobga olib proeksiyalar matritsasini beramiz */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
double aspect = (double)viewport[2] / (double)viewport[3];
gluPerspective(60, aspect, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// move back a bit
glTranslatef( 0, 0, -35 );
static float angle = 0;
angle += 1.0f;
/* markaziy kub parametrlarini o‘rnatish */
glPushMatrix();
glTranslatef(0,0,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(255,0,255);
glutSolidCube(5);
glPopMatrix();
/* qo‘yidan ikkinchi kub parametrlarini o‘rnatish */
glPushMatrix();
glTranslatef(10,-10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(255,0,0);
glutSolidCube(5);
glPopMatrix();
/* yuqoridan ikkinchi kub parametrlarini o‘rnatish */
glPushMatrix();
glTranslatef(10,10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(0,255,0);
glutSolidCube(5);
glPopMatrix();
/* yuqoridan birinchi kub parametrlarini o‘rnatish */
glPushMatrix();
glTranslatef(-10,10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(0,0,255);
glutSolidCube(5);
glPopMatrix();
/* qo‘yidan birinchi kub parametrlarini o‘rnatish */
glPushMatrix();
glTranslatef(-10,-10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(255,255,0);
glutSolidCube(5);
glPopMatrix();
glutSwapBuffers();
}
/* foydalanuvchi tomonidan chaqiriladigan oyna o‘lchamlarini o‘zgartiruvchi funksiya */
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("CUBES");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
init();
glutMainLoop();
return 0;
}
3. Ushbu dastur OpenGL muhitida oddiy sahna orqali sfera, konus va tordan tuzilgan materiallarni ifodalaydi. Ushbu obyektlarning tur shaklida berilgan tasvirini va uning ranglar bilan to‘ldirilgan variantlari turli burchak ostidagi harakati keltirilgan va sahnada yorug‘lik manbai tushurilgan.
#include
#include
#include
static int slices = 26;
static int stacks = 16;
/* GLUT qayta aloqalarga ishlov berish */
static void
resize(int width, int height)
{
/* oyna o‘lchamini hisobga olib proeksiyalar matritsasini beramiz */
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 180.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
Do'stlaringiz bilan baham: |