USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE...

37
USHTRIME NGA GRAFIKA KOMPJUTERIKE Detyra 1. // lab01.c #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> GLint gjeresiadritares, lartesiadritares, N=10; void boshtet(void) { int i; glColor3f(0.198, .04, .740); glBegin(GL_LINES); for(i=1; i<N; i++) { glVertex2i(i*gjeresiadritares/N, 0); glVertex2i(i*gjeresiadritares/N, lartesiadritares); glVertex2i(0, i*lartesiadritares/N); glVertex2i(gjeresiadritares, i*lartesiadritares/N); } glEnd(); } void vizatimi(void) { glColor3f(0.60, .40, .170); glBegin(GL_POLYGON); glVertex2i(5*gjeresiadritares/N, 5*lartesiadritares/N); glVertex2i(7*gjeresiadritares/N, 5*lartesiadritares/N); glVertex2i(7*gjeresiadritares/N, 8*lartesiadritares/N); glVertex2i(5*gjeresiadritares/N, 8*lartesiadritares/N); glEnd(); } void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); boshtet(); vizatimi(); glFlush(); } void myReshape(int gjeresia, int lartesia) { glClearColor (.75, .75, .75, 0.0); glViewport(0,0,0.5*gjeresia,0.5*lartesia); glMatrixMode(GL_PROJECTION);

Transcript of USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE...

Page 1: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

Detyra 1. 

 //  lab01.c #include <stdio.h> #include <stdlib.h> #include <GL/glut.h>  GLint  gjeresiadritares, lartesiadritares, N=10;  void boshtet(void) {   int  i;   glColor3f(0.198, .04, .740);    glBegin(GL_LINES);   for(i=1; i<N; i++)   {     glVertex2i(i*gjeresiadritares/N, 0);     glVertex2i(i*gjeresiadritares/N, lartesiadritares);     glVertex2i(0, i*lartesiadritares/N);     glVertex2i(gjeresiadritares, i*lartesiadritares/N);   }   glEnd(); }  void vizatimi(void) {   glColor3f(0.60, .40, .170);    glBegin(GL_POLYGON);     glVertex2i(5*gjeresiadritares/N, 5*lartesiadritares/N);     glVertex2i(7*gjeresiadritares/N, 5*lartesiadritares/N);     glVertex2i(7*gjeresiadritares/N, 8*lartesiadritares/N);     glVertex2i(5*gjeresiadritares/N, 8*lartesiadritares/N);   glEnd(); }  void myDisplay() {   glClear(GL_COLOR_BUFFER_BIT);   boshtet();   vizatimi();   glFlush(); } void myReshape(int gjeresia, int lartesia) {      glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,0.5*gjeresia,0.5*lartesia);   glMatrixMode(GL_PROJECTION); 

Page 2: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glLoadIdentity();   glOrtho(0, gjeresia,0, lartesia, ‐1, 1);   gjeresiadritares=gjeresia;   lartesiadritares=lartesia;   glMatrixMode(GL_MODELVIEW); }  int main(int argc, char** argv) {   glutInit(&argc,argv);   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   glutInitWindowSize(800,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi1");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);   glutMainLoop(); }  

 

Detyra 2. 

 #include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> #define X_AXIS  0 #define Y_AXIS  1 #define Z_AXIS  2  GLfloat m=4.0; GLint  N=8, translateAxis=0; GLfloat xTranslate=0.0, yTranslate=0.0, zTranslate=0.0; GLfloat translateDelta=0.1;  void vizatimi(void) {   glColor3f(0.160, .40, .70);    glBegin(GL_POLYGON);   glVertex2f(0.5, 0.5);   glVertex2f(1.0, 0.5);   glVertex2f(1.0, 1.0);   glVertex2f(0.5, 1.);   glEnd(); } void boshtet(void) {   int  i; 

Page 3: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glColor3f(0.98, .04, .70);    glBegin(GL_LINES);   for(i=0; i<=N; i++)    {     glVertex2f(‐m+2.0*i*m/N, ‐m);     glVertex2f(‐m+2.0*i*m/N, m);     glVertex2f(‐m, ‐m+2.0*i*m/N);     glVertex2f(m, ‐m+2.0*i*m/N);   }   glEnd(); glColor3f(.25, .25, .25);    glBegin(GL_LINES);     glVertex2f(0, ‐m);     glVertex2f(0, m);     glVertex2f(‐m, 0);     glVertex2f(m, 0);   glEnd(); } void myDisplay(void) {   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   boshtet();   glTranslatef(xTranslate, yTranslate, zTranslate);   vizatimi();   glFlush();   glutSwapBuffers(); } void myReshape(int width, int height) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,width,height);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐2*m, 2*m, ‐2*m, 2*m, ‐m, m);   glMatrixMode(GL_MODELVIEW); } void myMouse(int btn, int state, int x, int y) {   if(btn==GLUT_LEFT_BUTTON &&      state == GLUT_DOWN) {     if(translateAxis==X_AXIS)        xTranslate‐=translateDelta;     else if(translateAxis == Y_AXIS)        yTranslate‐=translateDelta;     else zTranslate‐=translateDelta;   }   else if(btn==GLUT_RIGHT_BUTTON &&  

Page 4: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    state == GLUT_DOWN) {     if(translateAxis == X_AXIS)        xTranslate+=translateDelta; else if(translateAxis == Y_AXIS)              yTranslate+=translateDelta;     else zTranslate+=translateDelta;   }   else if(btn==GLUT_MIDDLE_BUTTON &&      state == GLUT_DOWN) {     translateAxis++;     if(translateAxis==3) translateAxis=0;   }   else return;   myDisplay(); } void main(int argc, char** argv) {      glutInit(&argc,argv);   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(300,300);   glutInitWindowPosition(10,0);   glutCreateWindow("ushtrimi 1");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);   glutMouseFunc(myMouse);   glutMainLoop(); } 

 

Detyra 3. 

#include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h>  GLint  N=6; GLfloat x=30.0;  void boshtet(void) {   int  i;   glColor3f(.98, .04, .70);    glBegin(GL_LINES);   for(i=0; i<=N; i++) {     glVertex2f(‐x+2.0*i*x/N, ‐x);     glVertex2f(‐x+2.0*i*x/N, x);     glVertex2f(‐x, ‐x+2.0*i*x/N); 

Page 5: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    glVertex2f(x, ‐x+2.0*i*x/N);   }   glEnd();   glColor3f(.25, .25, .25);    glBegin(GL_LINES);     glVertex2f(0, ‐x);     glVertex2f(0, x);     glVertex2f(‐x, 0);     glVertex2f(x, 0);   glEnd(); }  void trekendeshi(void) {   glBegin(GL_POLYGON);   glColor3f(.0, .0, 1.0);    glVertex3f(‐5., ‐5., 0.);   glColor3f(.0, .0, 1.0);    glVertex3f(5., ‐5., 0.);   glColor3f(1.0, .0, .0);    glVertex3f(0., 5., 0.);   glEnd(); } void myDisplay(void)  {   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   boshtet();   glLoadIdentity();   glTranslatef(0.0, 0.0, 0.0);   glRotatef(0.0, 0.0, 0.0, 1.0);   trekendeshi();   glFlush();   glutSwapBuffers(); }  void myReshape(int width, int height) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,width,height);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐x, x, ‐x, x, ‐x, x);   glMatrixMode(GL_MODELVIEW); } int main(int argc, char** argv) {   glutInit(&argc,argv); 

Page 6: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(500,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi3");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);    glutMainLoop(); }  

 

Detyra 4. 

#include <stdio.h> #include <stdlib.h> #include <GL/glut.h>  GLfloat  x=2.0; GLint    N=8; GLfloat  shkallax=1.0;  void boshtet(void) {   int  i;   glColor3f(0.98, .04, .70);    glBegin(GL_LINES);   for(i=0; i<=N; i++) {     glVertex2f(‐x+2.0*i*x/N, ‐x);     glVertex2f(‐x+2.0*i*x/N, x);     glVertex2f(‐x, ‐x+2.0*i*x/N);     glVertex2f(x, ‐x+2.0*i*x/N);   }   glEnd();   glColor3f(0.25, .24, .25);    glBegin(GL_LINES);   glVertex2f(0, ‐x);     glVertex2f(0, x);     glVertex2f(‐x, 0);     glVertex2f(x, 0);     glEnd();     } void vizatimi(void) {   glColor3f(0.60, .40, .70);    glBegin(GL_POLYGON);   glVertex2f(0., 0.);   glVertex2f(1., 0.); 

Page 7: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glVertex2f(1., 1.);   glVertex2f(0., 1.);   glEnd(); }  void myDisplay(void) {   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   boshtet();   glScalef(shkallax, 1.0, 1.0);   glTranslatef(0.0, 0.1, 0.0);   glRotatef(45.0, 0.0, 0.0, 1.0);   vizatimi();   glFlush();   glutSwapBuffers(); } void myReshape(int gjeresia, int lartesia) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,gjeresia,lartesia);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐x, x, ‐x, x, ‐x, x);   glMatrixMode(GL_MODELVIEW); }  int main(int argc, char** argv) {   glutInit(&argc,argv);   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(500,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi4");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);    glutMainLoop(); }  

 

Detyra 5. 

/*Tw  vizatohet katrorw me dimenzione 0,5. katrori tw  zhvendoset nw 3 pozita tjera, ashtu qw cdonjwra prej tjetrws tw zhvendoset nga 2 njwsi nw drejtim tw boshtit x dhe 2 njwsi nw drejtim tw y.  */  

Page 8: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

#include <Windows.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> GLint  N=6; GLfloat x=3.0; void boshtet(void) {   int  i;   glColor3f(.9, .04, .70);    glBegin(GL_LINES);   for(i=0; i<=N; i++) {     glVertex2f(‐x+2.0*i*x/N, ‐x);     glVertex2f(‐x+2.0*i*x/N, x);     glVertex2f(‐x, ‐x+2.0*i*x/N);     glVertex2f(x, ‐x+2.0*i*x/N);   }   glEnd();   glColor3f(.25, .25, .25);    glBegin(GL_LINES);     glVertex2f(0, ‐x);     glVertex2f(0, x);     glVertex2f(‐x, 0);     glVertex2f(x, 0);   glEnd(); } void vizatimi(void) {   glBegin(GL_POLYGON);      glVertex3f(0., 0., 0.);   glVertex3f(1., 0., 0.);   glVertex3f(1., 0.5, 0.);   glVertex3f(0.5, 1.0, 0.0);   glVertex3f(0., 0.5, 0.);   /* 2.954423, 8.520945, 0.);   glVertex3f(2.819078, 9.026060, 0.);   glVertex3f(2.598076, 9.500000, 0.);   glVertex3f(2.298133, 9.928363, 0.);   glVertex3f(1.928363, 10.298133, 0.);   glVertex3f(1.500000, 10.598076, 0.);   glVertex3f(1.026060, 10.819078, 0.);   glVertex3f(.520945, 10.954423, 0.);   glVertex3f(0.000000, 11.000000, 0.);   glVertex3f(‐.520945, 10.954423, 0.);   glVertex3f(‐1.026060, 10.819078, 0.);   glVertex3f(‐1.500000, 10.598076, 0.); 

Page 9: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

glVertex3f(‐1.928363, 10.298133, 0.);   glVertex3f(‐2.298133, 9.928363, 0.);   glVertex3f(‐2.598076, 9.500000, 0.);   glVertex3f(‐2.819078, 9.026060, 0.);   glVertex3f(‐2.954423, 8.520945, 0.);   glVertex3f(‐3., 8., 0.)*/   glEnd(); } void myDisplay(void)  {   int j=1;   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   boshtet();   glColor3f(.25, .25, .12);    vizatimi();   glColor3f(.98, .625, .12);    for(j=0; j<5; j++)    {      glLoadIdentity();      glTranslatef(1.0+j, 1.0+j, 0.0);      vizatimi();   }    glFlush(); glutSwapBuffers(); } void myReshape(int lart, int gjer) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,lart,gjer);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐2*x, 2*x, ‐2*x, 2*x, ‐x, x);   glMatrixMode(GL_MODELVIEW); } int main(int argc, char** argv) {   glutInit(&argc,argv);   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(500,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi5");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);    glutMainLoop(); }  

 

Page 10: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

Detyra 6. 

// Rrotullimi i katrorit #include <windows.h> #include <GL/glut.h> #include <GL/glu.h> #include <GL/gl.h> static GLfloat kendi = 0.0;  void init(void) {       glClearColor (0, 0, 0, 0); // Pastrimi i ekranit ne te zeze        glShadeModel (GL_FLAT); }  void display(void) {        glClear(GL_COLOR_BUFFER_BIT); // Pastron bufferin        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        glPushMatrix();      glScalef(1.3, 1.3, 1.3); //Madhesia e figures levizese        glRotatef(kendi, 0.0, 0.0, 1.0); //  x, y, z  kendi i rotullimit        glColor3f(1.0, 1.0, 1.0);      // glRectf(‐25, ‐25, 25, 25); // vizatimi i katrorit      glBegin ( GL_LINE_LOOP ); // metode tjeter e vizatimit  {                 glVertex2i (‐25,‐25); glColor3f(1.0, 0.0, 0.0);             glVertex2i (25,‐25); glColor3f(0.0, 1.0, 0.0);             glVertex2i (25,25); glColor3f(0.0, 0.0, 1.0);             glVertex2i (‐25,25); } glEnd ();       glPopMatrix();       glFlush();        glutSwapBuffers();  } void rotullimi(void) {  

Page 11: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

//    kendi = kendi + 1.0; // fast rotation        kendi = kendi + 0.1; // slow rotation        if (kendi > 360)    kendi = kendi ‐ 360;        glutPostRedisplay();   } void reshape(int w, int h)  {       glViewport(0, 0, (GLsizei) w, (GLsizei) h);         glMatrixMode(GL_PROJECTION);             glLoadIdentity();        glOrtho(‐50,50,‐50,50,‐1,1);         glMatrixMode(GL_MODELVIEW);        glLoadIdentity(); } void mouse(int button, int state, int x, int y)  {       switch (button) {        case GLUT_LEFT_BUTTON:              if (state == GLUT_DOWN)                    glutIdleFunc(rotullimi);              if (state == GLUT_UP)                    glutIdleFunc(NULL);              break;        default:              break;        } 

Page 12: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

 }  int main(int argc, char **argv) {     glutInit(&argc, argv);         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);         glutInitWindowPosition(100, 100);         glutInitWindowSize(200, 200);         glutCreateWindow("Rrotullimi i katrorit");//krijon dritare me emrin "Rrotullimi i katrorit"       init();          glutDisplayFunc(display);         glutReshapeFunc(reshape);         glutMouseFunc(mouse);      // glutKeyboardFunc(keyboard); // registers keyboard event callback function        glutMainLoop();         return 0;              } 

 

Detyra 7. 

#include <Windows.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h>  GLint    N=4; GLint    mouseX = 0; GLint    mouseY = 0; GLint    mouseState = 0; GLint    mouseButton = 0;  GLint    shape=1, model=1 ; GLfloat   size=2.0; GLfloat xTheta=0., yTheta=0., zTheta=0.,         thetaDelta=.25; GLfloat  scale=1.0, scaleDelta=1.01;  

Page 13: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

void myAxis(void) {   int  i;   glColor3f(0.98, .4, .070);    glBegin(GL_LINES);   for(i=0; i<=N; i++) {     glVertex2f(‐size+2.0*i*size/N, ‐size);     glVertex2f(‐size+2.0*i*size/N, size);     glVertex2f(‐size, ‐size+2.0*i*size/N);     glVertex2f(size, ‐size+2.0*i*size/N);   }   glEnd(); glColor3f(0.25,0.25,0.25); glBegin(GL_LINES); glVertex2f(‐size, 0); glVertex2f(size, 0); glVertex2f(0, ‐size); glVertex2f(0, size); glEnd(); }   void myDraw(void) {   glColor3f(.25, .25, .22);     if(model==5) glutWireTeapot(1.0);   else if(model==6) glutSolidTeapot(1.0);   else if(model==1) glutWireCube(1.0);   else if(model==2) glutSolidCube(1.0);   else if(model==3) glutWireSphere(1.0, 6, 20);   else if(model==4) glutSolidSphere(1.0, 10, 10);   // else if(model==5) glutWireTeapot(1.0);   //else if(model==6) glutSolidTeapot(1.0);  } void myDisplay(void) {   static int  i=0;   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   myAxis();   glRotatef(xTheta, 1.0, 0.0, 0.0);   glRotatef(yTheta, 0.0, 1.0, 0.0);   glRotatef(zTheta, 0.0, 0.0, 1.0);   glScalef(scale, scale, scale);   myDraw();   glFlush();   glutSwapBuffers(); } 

Page 14: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

void myReshape(int width, int height) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,width,height);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐size, size, ‐size, size, ‐size, size);   glMatrixMode(GL_MODELVIEW); } void glutMouse(int btn, int state, int x, int y) {   if(btn==GLUT_RIGHT_BUTTON &&      state == GLUT_DOWN) {      mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }   else if(btn==GLUT_RIGHT_BUTTON &&      state == GLUT_UP) {     mouseState=‐1;   }   else if(btn==GLUT_MIDDLE_BUTTON &&      state == GLUT_DOWN) {     mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }     else if(btn==GLUT_MIDDLE_BUTTON &&      state == GLUT_UP) {     mouseState=‐1;   }   else return;   glutPostRedisplay(); }   void glutMotion(int x, int y) {   if(mouseButton == GLUT_LEFT_BUTTON &&         mouseState == GLUT_DOWN) {     yTheta ‐= ( mouseX‐x)/10.;     xTheta ‐= (mouseY ‐y)/10.;   /*}   else if(mouseButton == GLUT_MIDDLE_BUTTON &&     mouseState == GLUT_DOWN) {     if(mouseY!=y) scale = scale *         pow(double (scaleDelta), int (mouseY ‐ y)/10.); 

Page 15: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  */}   else return;     mouseX = x;   mouseY = y;   glutPostRedisplay(); }  void myMenu(int id) {   if(id == 7) exit(1);   else model = id;   glutPostRedisplay(); } int main(int argc, char** argv) {   int  shape_submenu;   glutInit(&argc,argv);   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(500,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi 8");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);    glutMouseFunc(glutMouse);   glutMotionFunc(glutMotion);   glutCreateMenu(myMenu);   glutAddMenuEntry("WireKubi", 1);   glutAddMenuEntry("SolidCube", 2);   glutAddMenuEntry("WireSphere", 3);   glutAddMenuEntry("SolidSphere", 4);   glutAddMenuEntry("WireTeapot", 5);   glutAddMenuEntry("SolidTeapot", 6);   glutAddMenuEntry("Quit", 7);   glutAttachMenu(GLUT_RIGHT_BUTTON);   glutMainLoop(); }  

 

Detyra 8. 

#include <windows.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> #include <GL/glu.h>  GLint    N=6; 

Page 16: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

GLfloat  size=3.0;  GLfloat zTheta=0.;  void myAxis(void) {   int  i;   glColor3f(0.198, .04, .70);    glBegin(GL_LINES);   for(i=1; i<N; i++) {     glVertex2f(‐size+2.0*i*size/N, ‐size);     glVertex2f(‐size+2.0*i*size/N, size);     glVertex2f(‐size, ‐size+2.0*i*size/N);     glVertex2f(size, ‐size+2.0*i*size/N);   }   glEnd(); } void myDraw(void) {   glColor3f(0.198, .04, .70);    glBegin(GL_POLYGON);   glVertex3f(0., 0., 0.);   glVertex3f(0.5, 0., 0.);   glVertex3f(0.5, 0.5, 0.);   glVertex3f(0., 0.5, 0.);   glEnd();     glColor3f(1., 0., 0.);    glBegin(GL_LINE_LOOP);   glVertex3f(0., 0., 0.);   glVertex3f(0., 1., 0.);   glVertex3f(0., 1., 1.);   glVertex3f(0., 0., 1.);   glEnd();    glColor3f(0., 1., 0.);    glBegin(GL_LINE_LOOP);   glVertex3f(1., 0., 0.);   glVertex3f(1., 1., 0.);   glVertex3f(1., 1., 1.);   glVertex3f(1., 0., 1.);   glEnd();    glColor3f(0., 0., 1.);  glBegin(GL_LINE_LOOP);   glVertex3f(0., 0., 0.);   glVertex3f(1., 0., 0.);   glVertex3f(1., 0., 1.);   glVertex3f(0., 0., 1.);   glEnd();   glColor3f(1., 1., 0.);    glBegin(GL_LINE_LOOP); 

Page 17: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glVertex3f(0., 1., 0.);   glVertex3f(1., 1., 0.);   glVertex3f(1., 1., 1.);   glVertex3f(0., 1., 1.);   glEnd();  } GLfloat xTheta=0., yTheta=0.,  thetaDelta=6.0;  void myDisplay(void) {   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   myAxis();   glRotatef(xTheta, 1.0, 0.0, 0.0);   glRotatef(yTheta, 0.0, 1.0, 0.0);   glRotatef(zTheta, 0.0, 0.0, 1.0);   myDraw();   glFlush();   glutSwapBuffers(); } void myKeyboard(unsigned char theKey, int x, int y) {   switch (theKey)   {     case 'x' : xTheta ‐= thetaDelta;     break;     case 'X' : xTheta += thetaDelta;     break;     case 'y' : yTheta ‐= thetaDelta;     break;     case 'Y' : yTheta += thetaDelta;     break;     case 'z' : zTheta ‐= thetaDelta;     break;     case 'Z' : zTheta += thetaDelta;     break;     case 27: exit(‐1); // esc key   }   myDisplay(); }        void myReshape(int width, int height) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,width,height);       

Page 18: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐size, size, ‐size, size, ‐size, size);   glMatrixMode(GL_MODELVIEW); }  int main(int argc, char** argv) {   glutInit(&argc,argv);   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(500,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi9");   glutReshapeFunc(myReshape);   glutKeyboardFunc(myKeyboard);   // in main();   glutDisplayFunc(myDisplay);      glutMainLoop(); }  

 

Detyra 9. 

#include <math.h> #include <stdio.h> #include <windows.h> #include <gl/glut.h> typedef float vec3_t[3];  vec3_t cube[] = {     {‐1,‐1,‐1},     {‐1,1,‐1},   {1,1,‐1},    {1,‐1,‐1},    {1,1,1},   {1,1,‐1},   {‐1,1,‐1},   {‐1,1,1},    {1,‐1,1},   {‐1,‐1,1},     {‐1,‐1,‐1},   {1,‐1,‐1},    {‐1,1,1},   {‐1,1,‐1},   {‐1,‐1,‐1},     {‐1,‐1,1}, 

Page 19: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    {1,1,1},   {1,‐1,1},   {1,‐1,‐1},   {1,1,‐1},    {‐1,‐1,1},   {1,‐1,1},   {1,1,1},   {‐1,1,1}     }; vec3_t color[] = {   {1.0f, .0f, .0f},    //red   {0.0f, 1.0f, 0.0f},   //green   {0.0f, 0.0f, 1.0f},   //blue   {1.0f, 1.0f, 0.0f},   //yellow   {1.0f, 0.0f, 1.0f},   //magenta   {0.0f, 1.0f, 1.0f},   //cyan   {1.0f, 1.0f, 1.0f},   //white   {.25f, .25f, .25f},   //dark gray   {.60f, .40f, .70f},   //barney purple   {.98f, .625f, .12f},  //pumpkin orange   {.98f, .04f, .70f},   //pastel pink   {.75f, .75f, .75f},   //light gray   {.60f, .40f, .12f}   //brown  }; vec3_t rot = {0.,0.,0.}; vec3_t eye = {0.,5.,‐5.}; vec3_t center = {0.,0.,10.};  float  size=3.; float  theta=.0; float  thetaDelta=.125; float  eyeDelta=.125; float  scale=1.0; float  scaleDelta=1.125;  int    mouseX = 0; int    mouseY = 0; int    mouseState = 0; int    mouseButton = 0;  int    projection = 0; int    aniOn = 0; int    depthOn = 1; int    openOn = 0; int    fillOn = 0; int    windowWidth, windowHeight;  

Page 20: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

void drawCube (void){   int  i;     for(i = 0; i < 20+openOn*4; i ++) {         if(fillOn) glBegin(GL_POLYGON);     else glBegin(GL_LINE_LOOP);     glColor3fv(color[i%13]);     glVertex3fv(cube[i]);           if((i+1)%4 == 0) glEnd();   } }  void myDisplay (void){     glClear(GL_COLOR_BUFFER_BIT |          GL_DEPTH_BUFFER_BIT);   glMatrixMode(GL_MODELVIEW);     glLoadIdentity();   if(!projection)     glTranslatef(eye[0], eye[1], eye[2]);   glRotatef(rot[0], 1.0f, 0.0f, 0.0f);   glRotatef(rot[1], 0.0f, 1.0f, 0.0f);   glRotatef(rot[2], 0.0f, 0.0f, 1.0f);   glScalef(scale, scale, scale);   drawCube();   glColor3fv(color[6]);   glutWireTeapot(.5);   glFlush();   glutSwapBuffers(); } void myResize (int width, int height) {   glClear(GL_COLOR_BUFFER_BIT |          GL_DEPTH_BUFFER_BIT);     glViewport(0, 0, width, height);   glMatrixMode(GL_PROJECTION);     glLoadIdentity();     if(projection)     glOrtho(‐size, size, ‐size, size, ‐size, size);    else      gluPerspective(150., (float)width/height, .1, 100.);   glEnable(GL_DEPTH_TEST);   windowWidth=width;   windowHeight=height; }  void myKeyboard (unsigned char key, int x, int y) {     switch (key)  {       case 'o':        openOn = !openOn; 

Page 21: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    break;     case 'f':       fillOn = !fillOn;     break;     case 'p':       projection = !projection;       myResize(windowWidth, windowHeight);     break;     case 'd':       depthOn = !depthOn;       if(depthOn) glEnable(GL_DEPTH_TEST);       else glDisable(GL_DEPTH_TEST);     break;     case 'z':       scale*=scaleDelta;     break;     case 'x':       scale/=scaleDelta;     break;   }   glutPostRedisplay(); } void myMouse(int btn, int state, int x, int y) {   if(btn==GLUT_LEFT_BUTTON && state ==         GLUT_DOWN) {     mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }   else if(btn==GLUT_LEFT_BUTTON && state ==       GLUT_UP)     mouseState=‐1;   else return;   glutPostRedisplay(); } void myMotion(int x, int y) {   if(mouseButton == GLUT_LEFT_BUTTON &&         mouseState == GLUT_DOWN) {     rot[1] ‐= (mouseX ‐ x)/5.;     rot[0] ‐= (mouseY ‐ y)/5.;     glutPostRedisplay();     mouseX=x;     mouseY=y;   } }  int main (void) 

Page 22: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

{     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |       GLUT_DEPTH);     glutInitWindowSize(500,500);     glutCreateWindow("lab07");   glutDisplayFunc(myDisplay);   glutReshapeFunc(myResize);   glutKeyboardFunc(myKeyboard);   glutMouseFunc(myMouse);   glutMotionFunc(myMotion);   glutMainLoop(); } 

 

Detyra 10. 

#include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> #include <cmath>   GLint    N=6, idleFlag = 1; GLint    mouseX = 0; GLint    mouseY = 0; GLint    mouseState = 0; GLint    mouseButton = 0;  GLfloat  size=30.0, theta=0.0; GLfloat z; GLfloat xTheta=0., yTheta=0., zTheta=0.,         thetaDelta=.125; GLfloat  scale=1.0, scaleDelta=1.01;  void myAxis(void) {   int  i;   glColor3f(.98, .04, .70);    glutWireCube(size*2.0);   glBegin(GL_LINES);   for(i=0; i<=N; i++) {     glVertex2f(‐size+2.0*i*size/N, ‐size);     glVertex2f(‐size+2.0*i*size/N, size);     glVertex2f(‐size, ‐size+2.0*i*size/N);     glVertex2f(size, ‐size+2.0*i*size/N);   }   glEnd();   glColor3f(.25, .25, .25);    glBegin(GL_LINES);     glVertex2f(0, ‐size);     glVertex2f(0, size); 

Page 23: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    glVertex2f(‐size, 0);     glVertex2f(size, 0);   glEnd(); } void myDisplay(void)  {   glClear(GL_COLOR_BUFFER_BIT);   glLoadIdentity();   glRotatef(xTheta, 1.0, 0.0, 0.0);   glRotatef(yTheta, 0.0, 1.0, 0.0);   glRotatef(zTheta, 0.0, 0.0, 1.0);   glScalef(scale, scale, scale);   myAxis();      glColor3f(.98, .625, .12);       glutWireSphere(3.0, 10, 10);   glPushMatrix();   glRotatef(0.1*theta, 0., 0., 1.);   glTranslatef(20., 0., 0.);   glutWireSphere(1.0, 10, 10);        glRotatef(0.2*theta, 0., 0., 1.);     glTranslatef(5., 0., 0.);     glutWireSphere(.5, 10, 10);   glPopMatrix();   glRotatef(0.5*theta, 0., 0., 1.);   glTranslatef(‐5., 0., 0.);   glutWireSphere(.5, 10, 10);      glFlush();   glutSwapBuffers(); }  void myReshape(int width, int height) {   glClearColor (.75, .75, .75, 0.0);   glViewport(0,0,width,height);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   glOrtho(‐size, size, ‐size, size, ‐size, size);   glMatrixMode(GL_MODELVIEW); }  void myIdle(void) {   theta+=0.1;   glutPostRedisplay(); 

Page 24: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

} void myMouse(int btn, int state, int x, int y) {   if(btn==GLUT_LEFT_BUTTON &&      state ==GLUT_DOWN) {     mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }   else if(btn==GLUT_LEFT_BUTTON &&      state == GLUT_UP) {     mouseState=‐1;   }   else if(btn==GLUT_RIGHT_BUTTON &&      state == GLUT_DOWN) {     mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }   else if(btn==GLUT_RIGHT_BUTTON &&      state == GLUT_UP) {     mouseState=‐1;   }   else if(btn==GLUT_MIDDLE_BUTTON &&      state == GLUT_DOWN) {     xTheta=yTheta=zTheta=0.;     scale=1.0;   }   else return;      if(idleFlag && state==GLUT_DOWN)      glutIdleFunc(NULL);   else if(idleFlag && state==GLUT_UP)      glutIdleFunc(myIdle);   glutPostRedisplay(); }  void myMotion(int x, int y) {   //z=pow(scaleDelta, (mouseY ‐ y)/1.);   if(mouseButton == GLUT_LEFT_BUTTON &&         mouseState == GLUT_DOWN) {     yTheta ‐= (mouseX ‐ x)/10.;     xTheta ‐= (mouseY ‐ y)/10.;   } 

Page 25: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  else if(mouseButton == GLUT_RIGHT_BUTTON &&       mouseState == GLUT_DOWN) {     if(mouseY!=y) scale = scale*scaleDelta;       //*pow(scaleDelta, (mouseY ‐ y)/1.);           }   else return;   mouseX = x;   mouseY = y;   glutPostRedisplay(); } void myKeyboard(unsigned char theKey, int x, int y) {   switch (theKey) {     case ' ' : idleFlag=!idleFlag;       if(idleFlag) glutIdleFunc(myIdle);       else glutIdleFunc(NULL);       break;     case 27: exit(‐1); // esc key   }   glutPostRedisplay(); } int main(int argc, char** argv) {   glutInit(&argc,argv);   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);   glutInitWindowSize(500,500);   glutInitWindowPosition(0,0);   glutCreateWindow("ushtrimi10");   glutReshapeFunc(myReshape);   glutDisplayFunc(myDisplay);    glutMouseFunc(myMouse);   glutMotionFunc(myMotion);   glutKeyboardFunc(myKeyboard);   glutMainLoop(); } 

 

Detyra 11. 

/*katrori bazw tw duket me kwtw shkallw: (3,2;3,2;0,5). mbi cilindrin e gjelber nw vend tw 4 tw rrotullohen 2 cilindra nw kahje pozitive me reze 0.4, njwri me ngjyrw tw kuqe dhe tjetri me tw kaltwr. syri i kamerws tw jwtw nw largwsi 8. nw vend tw cilindrit  tw zi vizato cilindwr tw  kaltwr me baza 0,2 dhe lartwsi 0,6.mbi sferwn e sipwrme vizato cilindwr me reze 0.01 dhe 0.5 dhe lartwsi 0.6 pastaj mbi cilindrin e siperm vizato 8 sfera me reze 0.1 i twrw sistemi tw rrotullohet nw kahje pozitive */ #include <math.h> 

Page 26: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

#include <stdio.h> #include <windows.h> #include <gl/glut.h> typedef float vec3_t[3];  vec3_t cube[] = {     {‐1,‐1,‐1},     {‐1,1,‐1},   {1,1,‐1},    {1,‐1,‐1},   {1,1,1},   {1,1,‐1},   {‐1,1,‐1},   {‐1,1,1},    {1,‐1,1},   {‐1,‐1,1},     {‐1,‐1,‐1},   {1,‐1,‐1},     {‐1,1,1},   {‐1,1,‐1},   {‐1,‐1,‐1},     {‐1,‐1,1},    {1,1,1},   {1,‐1,1},   {1,‐1,‐1},   {1,1,‐1},    {‐1,‐1,1},   {1,‐1,1},   {1,1,1},   {‐1,1,1}     }; vec3_t color[] = {   {1.0f, 0.0f, 0.0f},   //red   {0.0f, 1.0f, 0.0f},   //green   {0.0f, 0.0f, 1.0f},   //blue   {1.0f, 1.0f, 0.0f},   //yellow   {1.0f, 0.0f, 1.0f},   //magenta   {0.0f, 1.0f, 1.0f},   //cyan   {1.0f, 1.0f, 1.0f},   //white   {.25f, .25f, .25f},   //dark gray   {.60f, .40f, .70f},   //barney purple   {.98f, .625f, .12f},    //pumpkin orange   {.98f, .04f, .70f},   //pastel pink   {.75f, .75f, .75f},   //light gray   {.60f, .40f, .12f}   //brown }; vec3_t rot = {0.,0.,0.}; vec3_t eye = {0.,0.,‐5.}; vec3_t center = {0.,0.,0.}; 

Page 27: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

 float  size=3.; float  theta=.0; float  thetaDelta=.125; float  eyeDelta=.125; float  scale=1.0; float  scaleDelta=1.125;  float  anitheta=.0; float  anithetaDelta=.125; GLUquadricObj *c;  int    mouseX = 0; int    mouseY = 0; int    mouseState = 0; int    mouseButton = 0;  int    projection = 0; int    aniOn = 0; int    depthOn = 1; int    openOn = 0; int    fillOn = 0; int    windowWidth, windowHeight;  void drawCube (void){   int  i;     for(i = 0; i < 20+openOn*4; i ++) {         if(fillOn) glBegin(GL_POLYGON);     else glBegin(GL_LINE_LOOP);     glColor3fv(color[i%12]);     glVertex3fv(cube[i]);           if((i+1)%4 == 0) glEnd();   } } void myDisplay (void) {   glClear(GL_COLOR_BUFFER_BIT |          GL_DEPTH_BUFFER_BIT);   glMatrixMode(GL_MODELVIEW);       glLoadIdentity();   if(!projection)     gluLookAt(eye[0], eye[1], eye[2], center[0],         center[1], center[2], 0, 1, 0);   glRotatef(rot[0], 1.0f, 0.0f, 0.0f);   glRotatef(rot[1], 0.0f, 1.0f, 0.0f);   glRotatef(rot[2], 0.0f, 0.0f, 1.0f);   glScalef(scale, scale, scale);   glRotatef(‐45., 1., 0., 0.);   glPushMatrix();     glScalef(3., 3., 0.1); 

Page 28: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    glColor3fv(color[0]);     if(fillOn) glutSolidCube(1.0);     else glutWireCube(1.0);    glPopMatrix(); glTranslatef(0., 0., .1);   glRotatef(‐anitheta, 0., 0., 1.);   glColor3fv(color[1]);   gluCylinder(c, 1., 1., .5, 15, 5);   glTranslatef(0., 0., .5);   glPushMatrix();     glTranslatef(‐.5, 0., 0.);     glRotatef(‐anitheta*5, 0., 0., 1.);     glColor3fv(color[2]);     gluCylinder(c, .25, .25, .25, 12, 10);   glPopMatrix();   glPushMatrix();     glTranslatef(0., ‐.5, 0.);     glRotatef(‐anitheta*5, 0., 0., 1.);     glColor3fv(color[3]);     gluCylinder(c, .25, .25, .25, 12, 10);   glPopMatrix();   glPushMatrix();     glTranslatef(0., .5, 0.);     glRotatef(‐anitheta*5, 0., 0., 1.);     glColor3fv(color[4]);     gluCylinder(c, .25, .25, .25, 12, 10);   glPopMatrix();   glPushMatrix();     glTranslatef(.5, 0., 0.);     glRotatef(‐anitheta*2, 0., 0., 1.);     glColor3fv(color[5]);     gluCylinder(c, .25, .25, .25, 12, 10);   glPopMatrix(); //   glTranslatef(0., 0., .25);   glRotatef(anitheta*2, 0., 0., 1.);    glColor3fv(color[6]);   gluCylinder(c, .5, .1, .25, 12, 10);    glTranslatef(0., 0., .25);//??????   glColor3fv(color[0]);   gluCylinder(c, .1, .1, .5, 12, 10);        glTranslatef(0., 0., .75);   glColor3fv(color[8]);   if(fillOn) glutSolidSphere(.25, 10, 10); 

Page 29: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  else glutWireSphere(.25, 10, 10);    glPushMatrix(); glTranslatef(0., 0., .25);   glRotatef(anitheta, 0., 0., 1.);    glColor3fv(color[6]);   gluCylinder(c, .05, .5, .25, 12, 10); glPopMatrix();     glTranslatef(0.0, .0, 0.6);   glPushMatrix();   glTranslatef(0.5, .0, 0.0);     glRotatef(anitheta, 0., 0., 1.);     glColor3fv(color[3]);     glutWireSphere(.1, 10, 10); glPopMatrix();      glPushMatrix();     glTranslatef(‐0.5, .0, 0.0);     glRotatef(anitheta, 0., 0., 1.);     glColor3fv(color[4]);     glutWireSphere(.1, 10, 10); glPopMatrix();    glFlush();   glutSwapBuffers(); }  void myLookAt(int key) {   if(key == GLUT_KEY_UP) {     eye[2] = eye[2]‐cos(theta)*eyeDelta;     eye[0] = eye[0]+sin(theta)*eyeDelta;   }   else if(key == GLUT_KEY_DOWN) {     eye[2] = eye[2]+cos(theta)*eyeDelta;     eye[0] = eye[0]‐sin(theta)*eyeDelta;   }   center[2] = eye[2]‐cos(theta);   center[0] = eye[0]+sin(theta); } void myIdle() {   anitheta += anithetaDelta;   glutPostRedisplay(); }  

Page 30: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

void myResize (int width, int height){       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);       glViewport(0, 0, width, height);       glMatrixMode(GL_PROJECTION);      glLoadIdentity();     if(projection) {     glOrtho((‐size), size, (‐size), size, ‐size, size);      eye[2] = 0.;   }   else {     gluPerspective(60., (float)width/height, .21, 100.);     eye[2] = 4.;     myLookAt(0);   }   glEnable(GL_DEPTH_TEST);   windowWidth=width;   windowHeight=height; }  void myKeyboard (unsigned char key, int x, int y) {     switch (key)  {       case ' ':         aniOn = !aniOn;       if(aniOn) glutIdleFunc(myIdle);       else glutIdleFunc(NULL);     break;     case 'o':        openOn = !openOn;     break;     case 'f':       fillOn = !fillOn;       if(fillOn) gluQuadricDrawStyle(c, GLU_FILL);       else gluQuadricDrawStyle(c, GLU_LINE);     break;     case 'p':       projection = !projection;       myResize(windowWidth, windowHeight);     break;     case 'd':       depthOn = !depthOn;       if(depthOn) glEnable(GL_DEPTH_TEST);       else glDisable(GL_DEPTH_TEST);     break;     case 'z':       scale*=scaleDelta;     break;     case 'x': 

Page 31: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

      scale/=scaleDelta;     break;   }   glutPostRedisplay(); }  void mySKeyboard (int key, int x, int y) {   switch (key) {     case GLUT_KEY_UP :      break;     case GLUT_KEY_DOWN :      break;     case GLUT_KEY_LEFT :        theta‐=thetaDelta;      break;     case GLUT_KEY_RIGHT :        theta+=thetaDelta;      break;     default : return;       }   myLookAt(key);   glutPostRedisplay(); }  void myMouse(int btn, int state, int x, int y) {   if(btn==GLUT_LEFT_BUTTON && state ==         GLUT_DOWN) {     if(aniOn) glutIdleFunc(NULL);     mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }   else if(btn==GLUT_LEFT_BUTTON && state ==       GLUT_UP) {     mouseState=‐1;     if(aniOn) glutIdleFunc(myIdle);   }   else return;   glutPostRedisplay(); } void myInit() {   c=gluNewQuadric();   gluQuadricDrawStyle(c, GLU_LINE); }  void myMotion(int x, int y) 

Page 32: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

{   if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) {     rot[1] ‐= (mouseX ‐ x)/5.;     rot[0] ‐= (mouseY ‐ y)/5.;     glutPostRedisplay();     mouseX=x;     mouseY=y;   } }  int main (void) {   myInit();       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |       GLUT_DEPTH);       glutInitWindowSize(800,600);       glutCreateWindow("ushtrimi 12");   glutDisplayFunc(myDisplay);   glutReshapeFunc(myResize);   glutKeyboardFunc(myKeyboard);    glutSpecialFunc(mySKeyboard);   glutMouseFunc(myMouse);   glutMotionFunc(myMotion);   glutIdleFunc(myIdle);   glutMainLoop(); } 

 

Detyra 12. 

/*madhwsia e drejtkwndwshit tw jetw 2,sipwrfaqje e hapur tw ngel  sipwrfaqja nw pjeswn e poshtme (fundi), gjatwsia e frustumit tw jetw 95*/  #include <math.h> #include <stdio.h> #include <windows.h> #include <gl/glut.h> typedef float vec3_t[3];  float  size=0.; float  theta=.0; float  thetaDelta=.125; float  eyeDelta=.125; float  scale=1.0; float  scaleDelta=1.125;  int    mouseX = 0; int    mouseY = 0; int    mouseState = 0; int    mouseButton = 0;  int    projection = 0; 

Page 33: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

int    aniOn = 0; int    depthOn = 1; int    openOn = 0; int    fillOn = 1; int    frontface = 0; int    cullface = 0; int    windowWidth; int    windowHeight;  vec3_t rot = {0.,0.,0.}; vec3_t eye = {0.,0.,‐5.}; vec3_t center = {0.,0.,0.}; vec3_t color[] = {   {1.0f, 0.0f, 0.0f},   //red   {0.0f, 1.0f, 0.0f},   //green   {0.0f, 0.0f, 1.0f},   //blue   {1.0f, 1.0f, 0.0f},   //yellow   {1.0f, 0.0f, 1.0f},   //magenta   {0.0f, 1.0f, 1.0f},   //cyan   {1.0f, 1.0f, 1.0f},   //white   {.25f, .25f, .25f},   //dark gray   {.60f, .40f, .70f},   //barney purple   {.98f, .625f, .12f},  //pumpkin orange   {.98f, .04f, .70f},   //pastel pink   {.75f, .75f, .75f},   //light gray   {.60f, .40f, .12f}   //brown };  GLfloat Vertices[8][3] = {     {0.0, 0.0, 0.0},   // vertex 0   {1.0, 0.0, 0.0},   // vertex 1   {1.0, 0.0, 1.0},   // vertex 2   {0.0, 0.0, 1.0},   // vertex 3   {0.0, 1.0, 0.0},   // vertex 4   {1.0, 1.0, 0.0},   // vertex 5   {1.0, 1.0, 1.0},   // vertex 6   {0.0, 1.0, 1.0} }; // vertex 7 GLfloat Normals[5][3] = {     { 0.0,1.0, 0.0},   // face 0   { 0.0, ‐1.0, 0.0},   // face 1   { 0.0, 0.0, ‐1.0},   // face 2   {1.0, 0.0, 0.0},   // face 3   { 0.0, 0.0,1.0} }; // face 4 GLint Faces[5][4] = {     {0, 1, 2, 3},   // face 0   {4, 7, 6, 5},   // face 1   {2, 6, 7, 3},   // face 2   {3, 7, 4, 0},   // face 3 

Page 34: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  {4, 5, 1, 0} }; // face 4  void drawMesh(GLfloat vertices[][3], GLfloat normalVectors[][3],   GLint faces[][4], GLint nFaces) {   int j;   for(j=0; j<nFaces; j++) {     glColor3fv(color[j%12]);     glBegin(GL_QUADS);     glNormal3fv(normalVectors[j]);     glVertex3fv(vertices[faces[j][0]]);     glVertex3fv(vertices[faces[j][1]]);     glVertex3fv(vertices[faces[j][2]]);     glVertex3fv(vertices[faces[j][3]]);     glEnd();   } }  void myDisplay (void) {       glClear(GL_COLOR_BUFFER_BIT |          GL_DEPTH_BUFFER_BIT);      glMatrixMode(GL_MODELVIEW);       glLoadIdentity();   if(!projection)     gluLookAt(eye[0], eye[1], eye[2], center[0],         center[1], center[2], 0, 1, 0);   glRotatef(rot[0], 1.0f, 0.0f, 0.0f);   glRotatef(rot[1], 0.0f, 1.0f, 0.0f);   glRotatef(rot[2], 0.0f, 0.0f, 1.0f);   glScalef(scale, scale, scale);   drawMesh(Vertices, Normals, Faces, 5);   glFlush();   glutSwapBuffers(); } void myLookAt(int key) {    if(key == GLUT_KEY_UP) {     eye[2] = eye[2]‐cos(theta)*eyeDelta;     eye[0] = eye[0]+sin(theta)*eyeDelta;   }   else if(key == GLUT_KEY_DOWN) {     eye[2] = eye[2]+cos(theta)*eyeDelta;     eye[0] = eye[0]‐sin(theta)*eyeDelta;   }   center[2] = eye[2]‐cos(theta);   center[0] = eye[0]+sin(theta); } void myResize (int width, int height){     

Page 35: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

  glClear(GL_COLOR_BUFFER_BIT |          GL_DEPTH_BUFFER_BIT);       glViewport(0, 0, width, height);       glMatrixMode(GL_PROJECTION);      glLoadIdentity();     if(projection) {     glOrtho(‐size, size, ‐size, size, ‐size, size);      eye[2] = 0.;   }   else {     gluPerspective(80., (float)width/height, .1, 100.);     eye[2] = 5.;     myLookAt(0);   }   glEnable(GL_DEPTH_TEST);   glCullFace(GL_BACK);   glPolygonMode(GL_BACK, GL_LINE);   glPolygonMode(GL_FRONT, GL_FILL);   windowWidth=width;   windowHeight=height; } void mySKeyboard (int key, int x, int y) {   switch (key) {     case GLUT_KEY_UP :      break;     case GLUT_KEY_DOWN :      break;     case GLUT_KEY_LEFT :        theta‐=thetaDelta;      break;     case GLUT_KEY_RIGHT :        theta+=thetaDelta;      break;     default : return;       }   myLookAt(key);   glutPostRedisplay(); } void myKeyboard (unsigned char key, int x, int y) {     switch (key)  {         case 'p':       projection = !projection;       myResize(windowWidth, windowHeight);     break;     case 'd':       depthOn = !depthOn; 

Page 36: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

      if(depthOn) glEnable(GL_DEPTH_TEST);       else glDisable(GL_DEPTH_TEST);     break;     case 'f':       frontface = !frontface;       if(frontface) glFrontFace(GL_CW);       else glFrontFace(GL_CCW);     break;     case 'c':       cullface = !cullface;       if(cullface) glEnable(GL_CULL_FACE);       else glDisable(GL_CULL_FACE);     break;     case 'z':       scale*=scaleDelta;     break;     case 'x':       scale/=scaleDelta;     break;   }   glutPostRedisplay(); }  void myMouse(int btn, int state, int x, int y) {   if(btn==GLUT_LEFT_BUTTON &&      state == GLUT_DOWN) {     glutIdleFunc(NULL);     mouseState=state;     mouseButton=btn;     mouseX=x;     mouseY=y;   }   else if(btn==GLUT_LEFT_BUTTON &&      state == GLUT_UP)     mouseState=‐1;   else return;   glutPostRedisplay(); }  void myMotion(int x, int y) {   if(mouseButton == GLUT_LEFT_BUTTON &&      mouseState == GLUT_DOWN) {     rot[1] ‐= (mouseX ‐ x)/5.;     rot[0] ‐= (mouseY ‐ y)/5.;     glutPostRedisplay();     mouseX=x; 

Page 37: USHTRIME NGA GRAFIKA KOMPJUTERIKE - Dr. Fisnik · PDF fileUSHTRIME NGA GRAFIKA KOMPJUTERIKE glLoadIdentity(); glOrtho(0, gjeresia,0, lartesia, ‐1, 1); gjeresiadritares=gjeresia;

USHTRIME NGA GRAFIKA KOMPJUTERIKE 

    mouseY=y;   } }  int main (void) {    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |      GLUT_DEPTH);     glutInitWindowSize(500,500);     glutCreateWindow("ushtrimi12");     glutDisplayFunc(myDisplay);     glutReshapeFunc(myResize);     glutKeyboardFunc(myKeyboard);       glutSpecialFunc(mySKeyboard);   glutMouseFunc(myMouse);   glutMotionFunc(myMotion);   glutMainLoop(); }