Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GLUT Shapes Demo
- *
- * Written by Nigel Stewart November 2003
- *
- * This program is test harness for the sphere, cone
- * and torus shapes in GLUT.
- *
- * Spinning wireframe and smooth shaded shapes are
- * displayed until the ESC or q key is pressed. The
- * number of geometry stacks and slices can be adjusted
- * using the + and - keys.
- */
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdlib.h>
- #include <stdio.h>
- static int slices = 16;
- static int stacks = 16;
- /* GLUT callback Handlers */
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-320, 319, -240, 239, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- }
- void draw8waySyemmtry(int x,int y,int zone)
- {
- if(zone==0)
- glVertex2i(x,y);
- else if(zone==1)
- glVertex2i(y,x);
- else if(zone==2)
- glVertex2i(-y,x);
- else if(zone==3)
- glVertex2i(-x,y);
- else if(zone==4)
- glVertex2i(-x,-y);
- else if(zone==5)
- glVertex2i(-y,-x);
- else if(zone==6)
- glVertex2i(y,-x);
- else if(zone==7)
- glVertex2i(x,-y);
- }
- int cx0,cx1,cy0,cy1,cnt=0;
- void mouse(int button,int state,int x,int y)
- {
- if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
- {
- printf("%d %d\n",x,y);
- if(cnt==0)
- {
- cx0=x-320;
- cy0=-y+240;
- cx1 = cx0;
- cy1 = cy0;
- cnt=1;
- }
- else if(cnt==1)
- {
- cx1=x-320;
- cy1=-y+240;
- cnt=2;
- printf("%d %d %d %d\n",cx0,cy0,cx1,cy1);
- int dx=cx1-cx0,dy=cy1-cy0;
- if(dx>=0 && dy>=0)
- {
- if(abs(dx)>abs(dy))
- {
- printf("Zone 0, ");
- printf("COLOR = Blue\n");
- }
- else
- {
- printf("Zone 1, ");
- printf("COLOR = Green\n");
- }
- }
- else if(dx<=0 && dy>=0)
- {
- if(abs(dx)>abs(dy))
- {
- printf("Zone 3, ");
- printf("COLOR = Red\n");
- }
- else
- {
- printf("Zone 2, ");
- printf("COLOR = Yellow\n");
- }
- }
- else if(dx<=0 && dy<=0)
- {
- if(abs(dx)>abs(dy))
- {
- printf("Zone 4, ");
- printf("COLOR = White\n");
- }
- else
- {
- printf("Zone 5, ");
- printf("COLOR = Orange\n");
- }
- }
- else if(dx>=0 && dy<=0)
- {
- if(abs(dx)>abs(dy))
- {
- printf("Zone 7, ");
- printf("Color = Thin Green\n");
- }
- else
- {
- printf("Zone 6, ");
- printf("Color = Sky Blue\n");
- }
- }
- //lineDraw(cx0,cy0,cx1,cy1);
- }
- }
- }
- void drawLine_0(int x0,int y0,int x1,int y1,int zone)
- {
- int dx=x1-x0,dy=y1-y0;
- int x,y,d=2*dy-dx;
- int dE=2*dy,dNE=2*(dy-dx);
- x=x0,y=y0;
- draw8waySyemmtry(x,y,zone);
- while(!(x==x1 && y==y1))
- {
- if(d<0)
- {
- x++;
- d+=dE;
- }
- else
- {
- x++,y++;
- d+=dNE;
- }
- draw8waySyemmtry(x,y,zone);
- }
- }
- void lineDraw(int x0,int y0,int x1,int y1)
- {
- int dx=x1-x0,dy=y1-y0;
- if(dx>=0 && dy>=0)
- {
- if(abs(dx)>abs(dy))
- {
- glColor3d(0,0,1);
- drawLine_0(x0,y0,x1,y1,0);
- }
- else
- {
- glColor3d(0,1,0);
- drawLine_0(y0,x0,y1,x1,1);
- }
- }
- else if(dx<=0 && dy>=0)
- {
- if(abs(dx)>abs(dy))
- {
- glColor3d(1,0,0);
- drawLine_0(-x0,y0,-x1,y1,3);
- }
- else
- {
- glColor3d(1,1,0);
- drawLine_0(y0,-x0,y1,-x1,2);
- }
- }
- else if(dx<=0 && dy<=0)
- {
- if(abs(dx)>abs(dy))
- {
- glColor3d(1,1,1);
- drawLine_0(-x0,-y0,-x1,-y1,4);
- }
- else
- {
- glColor3d(1,.5,0);
- drawLine_0(-y0,-x0,-y1,-x1,5);
- }
- }
- else if(dx>=0 && dy<=0)
- {
- if(abs(dx)>abs(dy))
- {
- glColor3d(0,.5,0);
- drawLine_0(x0,-y0,x1,-y1,7);
- }
- else
- {
- glColor3d(0,.5,.5);
- drawLine_0(-y0,x0,-y1,x1,6);
- }
- }
- }
- static void display(void)
- {
- int x = 10, y = 20;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3d(1,1,1);
- glBegin(GL_LINES);
- glVertex2i(-320, 0);
- glVertex2i(319, 0);
- glVertex2i(0, -240);
- glVertex2i(0, 239);
- glEnd();
- glBegin(GL_POINTS);
- //glColor3d(.4,.6,.8);
- if(cnt == 2){
- cnt = 0;
- }
- else{
- lineDraw(cx0,cy0,cx1,cy1);
- }
- glEnd();
- glutSwapBuffers();
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27 :
- case 'q':
- exit(0);
- break;
- case '+':
- slices++;
- stacks++;
- break;
- case '-':
- if (slices>3 && stacks>3)
- {
- slices--;
- stacks--;
- }
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- /* Program entry point */
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Experiment 01");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutMouseFunc(mouse);
- glutIdleFunc(idle);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement