📄 square.c
字号:
/* square.c */
/* E. Angel, Interactive Computer Graphics */
/* A Top-Down Approach with OpenGL, Third Edition */
/* Addison-Wesley Longman, 2003 */
/* This program illustrates the use of the glut library forinterfacing with a Window System *//* The program opens a window, clears it to black,then draws a box at the location of the mouse each time theleft button is clicked. The right button exits the programThe program also reacts correctly when the window ismoved or resized by clearing the new window to black*/#include <GL/glut.h>
#include <stdlib.h>/* globals */GLsizei wh = 500, ww = 500; /* initial window size */GLfloat size = 3.0; /* half side length of square */void drawSquare(int x, int y){ y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size); glEnd(); glFlush();}/* rehaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h){/* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();/* set global size for use by drawing routine */ ww = w; wh = h; }void myinit(void){ glViewport(0,0,ww,wh);/* Pick 2D clipping window to match size of screen window This choice avoids having to scale object coordinateseach time window is resized */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);/* set clear color to black and clear window */ glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();/* callback routine for reshape event */ glutReshapeFunc(myReshape);}void mouse(int btn, int state, int x, int y){ if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(0);
//if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) exit(0);}/* display callback required by GLUT 3.0 */void display(void){}int main(int argc, char** argv){ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow("square"); myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutMotionFunc(drawSquare); glutDisplayFunc(display); glutMainLoop();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -