/* * Reshape.cpp * Demonstrates reshape of window * * Created on: Mar 1, 2013 * Author: debure */ #include #include #include #include #include using namespace std; GLint winHeight = 500, winWidth = 500; // initial window size for gluInitWindowSize GLint view_xmin, view_xmax, view_ymin, view_ymax; // extents of viewing volume GLdouble aspectX = 1, aspectY = 1; // winWidth/winHeight GLfloat theta; bool sqStart = false; GLfloat rsFactor = 50; // scale factor for rotating square GLint rsX = 250, rsY = 325; // center of rotating square void myinit(void) { glClearColor(0.0, 0.0, 0.0, 1.0); glColor3f(0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 500, 500, 0); glMatrixMode(GL_MODELVIEW); glEnableClientState(GL_VERTEX_ARRAY); } void display(void) { // square is created at the origin GLfloat sq_vertices[4][2] = { { -1.0, -1.0 }, { 1.0, -1.0 }, { 1.0, 1.0 }, { -1.0, 1.0 } }; GLubyte sqIndices[4] = { 0, 3, 2, 1 }; glClear(GL_COLOR_BUFFER_BIT); // code for rotating square glVertexPointer(2, GL_FLOAT, 0, sq_vertices); glColor3ub(75, 75, 255); // assign color using unsigned byte 0-255 glPushMatrix(); glLoadIdentity(); glTranslatef(rsX, rsY, 0.0); // center of rotating square glScalef(rsFactor, rsFactor, 0.0); glRotatef(theta, 0.0, 0.0, 1.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, sqIndices); glPopMatrix(); glutSwapBuffers(); glFlush(); } void idle(void) { if (sqStart) { theta += 1; // modify rotation angle that is used in display } glutPostRedisplay(); } void mouse(int button, int state, int x, int y) { if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(0); } void reshape(int w, int h) { // Set viewport & save window dimensions in globals glViewport(0, 0, w, h); winWidth = w; winHeight = h; // Set up projection // Save max/min x/y coords in globals // Projection is orthographic. Aspect ratio is correct, if (w > h) { aspectX = (GLdouble) w / h; aspectY = 1; rsFactor = winWidth / 10.0; } else { aspectY = (GLdouble) h / w; aspectX = 1; rsFactor = winHeight / 10.0; } rsX = winWidth / 2.0; rsY = winHeight / 2.0; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, winWidth, winHeight, 0); glMatrixMode(GL_MODELVIEW); // Next transforms affect objects } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(winHeight, winWidth); glutInitWindowPosition(0, 0); glutCreateWindow("Reshape"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutIdleFunc(idle); myinit(); glutMainLoop(); return 0; }