#pragma once #include "stdlib.h" #include "Dependencies\glew\glew.h" #include "Dependencies\freeglut\freeglut.h" #include #include #define PI 3.14159265f const int screenWidth = 640; const int screenHeight = 480; //1 char tittle[] = "Bouncing ball(2D)"; int windowWidth = 640; int windowHeight = 480; int windowPosX = 50; int windowPosY = 50; GLfloat ballRadius = 0.12f; GLfloat ballX = 0.0f; GLfloat ballY = 0.0f; GLfloat ballXMax, ballXMin, ballYMax, ballYMin; GLfloat xSpeed = 0.0f; GLfloat ySpedd = 0.02f; int refreshMillis = 30; bool flag = true; void keyboardFunc1(unsigned char key, int x, int y); void countFrames(void); void renderBitmapString(float x, float y, float z, void*font, char*string); bool bUsePredefinedCamera = true; bool bFullsreen = false; int nWindowID; float viewerPosition[3] = { 0.0,0.0,-50.0 }; float viewerDirection[3] = { 0.0,0.0,0.0 }; float viewerUp[3] = { 0.0,1.0,0.0 }; float navigationRotation[3] = { 0.0,0.0,0.0 }; char pixelstring[30]; int cframe = 0; int time = 0; int timebase = 0; int mousePressedX = 0, mousePressedY = 0; float lastXOffset = 0.0, lastYOffset = 0.0, lastZOffset = 0.0; int leftMouseButtonActive = 0, middleMouseButtonActive = 0, rightMouseButtonActive = 0; int shiftActive = 0, altActive = 0, ctrlActive = 0; bool init = false; #include"Header.h" GLdouble clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop; void InitGL() { glClearColor(0.0, 0.0, 0.0, 1.0); } void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(ballX, ballY, 0.0f); glBegin(GL_TRIANGLE_FAN); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f); int numSegments = 100; GLfloat angle; for (int i = 0; i <= numSegments; i++) { angle = i * 2.0f * PI / numSegments; glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius); } glEnd(); glutSwapBuffers(); if (flag) { ballX -= xSpeed; ballY -= ySpedd; } else { ballX = ballX; ballY = ballY; } if (ballX > ballXMax) { ballX = ballXMax; xSpeed = -xSpeed; } else if (ballX < ballXMin) { ballX = ballXMin; xSpeed = -xSpeed; } if (ballY > ballYMax ) { ballY = ballYMax; ySpedd = -ySpedd; } else if (ballY < ballYMin) { ballY = ballYMin; ySpedd = -ySpedd; } } void reshape(GLsizei width, GLsizei height) { if (height == 0) height = 1; GLfloat aspect = (GLfloat)width / (GLfloat)height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (width >= height) { clipAreaXLeft = -1.0 * aspect; clipAreaXRight = 1.0 * aspect; clipAreaYBottom = -1.0; clipAreaYTop = 1.0 ; } else { clipAreaXLeft = -1.0; clipAreaXRight = 1.0; clipAreaYBottom = -1.0 / aspect; clipAreaYTop = 1.0 / aspect; } gluOrtho2D(clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop); ballXMin = clipAreaXLeft + ballRadius; ballXMax = clipAreaXRight - ballRadius; ballYMin = clipAreaYBottom + ballRadius; ballYMax = clipAreaYTop - ballRadius; } void keyboardFunc1(unsigned char key, int x, int y) { switch (key) { #ifdef WIN32 case 32: flag = !flag; break; #endif case'f': bFullsreen = !bFullsreen; if (bFullsreen) glutFullScreen(); else { glutSetWindow(nWindowID); glutPositionWindow(100, 100); glutReshapeWindow(640, 480); } break; } } void Timer(int value) { glutPostRedisplay(); glutTimerFunc(refreshMillis, Timer, 0); } //2 //3 void countFrames(void) { time = glutGet(GLUT_ELAPSED_TIME); cframe++; glPushMatrix(); glLoadIdentity(); glDisable(GL_LIGHTING); glColor4f(1.0, 1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, 200, 0, 200); glMatrixMode(GL_MODELVIEW); renderBitmapString(5, 5, 0.0, GLUT_BITMAP_HELVETICA_10, pixelstring); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); } void renderBitmapString(float x, float y, float z, void*font, char*string) { char*c; glRasterPos3f(x, y, z); for (c = string; *c != '\0'; c++) { glutBitmapCharacter(font, *c); } } //4 void menu(int argc, char **argv) { int chon; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE); glutInitWindowSize(windowWidth, windowHeight); glutInitWindowPosition(windowPosX, windowPosY); glutCreateWindow("Pixel"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutTimerFunc(0, Timer, 0); glutKeyboardFunc(keyboardFunc1); InitGL(); glutMainLoop(); } int main() { menu(__argc, __argv); return 0; }