下面是代碼。
想做的是 當滑鼠點擊時在滑鼠的位置出現小球,然后小球開始運動,遇到邊緣反彈。 小球可以是任意形狀大小的。但是,現在我運行之后,點擊沒有任何反應。麻煩幫我檢查下問題在哪!!
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
/////////////////////////////////////////////////////////////////////////////
// CONSTANTS
/////////////////////////////////////////////////////////////////////////////
#define PI 3.1415926535897932384626433832795
#define MAX_NUM_OF_DISCS 200 // Limit the number of discs.
#define MIN_RADIUS 10.0 // Minimum radius of disc.
#define MAX_RADIUS 50.0 // Maximum radius of disc.
#define NUM_OF_SIDES 18 // Number of polygon sides to approximate a disc.
#define MIN_X_SPEED 1.0 // Minimum speed of disc in X direction.
#define MAX_X_SPEED 20.0 // Maximum speed of disc in X direction.
#define MIN_Y_SPEED 1.0 // Minimum speed of disc in Y direction.
#define MAX_Y_SPEED 20.0 // Maximum speed of disc in Y direction.
#define DESIRED_FPS 30 // Approximate desired number of frames per second.
/////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
/////////////////////////////////////////////////////////////////////////////
typedef struct discType
{
double pos[2]; // The X and Y coordinates of the center of the disc.
double speed[2]; // The velocity of the disc in X and Y directions. Can be negative.
double radius; // Radius of the disc.
unsigned char color[3]; // R, G, B colors of the disc.
} discType;
int numDiscs = 0; // Number of discs that have been added.
discType disc[ MAX_NUM_OF_DISCS ]; // Array for storing discs.
bool drawWireframe = false; // Draw polygons in wireframe if true, otherwise
// otherwise polygons are filled.
int winWidth = 800; // Window width in pixels.
int winHeight = 600; // Window height in pixels.
/////////////////////////////////////////////////////////////////////////////
// Draw the disc in its color using GL_TRIANGLE_FAN.
/////////////////////////////////////////////////////////////////////////////
void DrawDisc( const discType *ds )
{
static bool firstTime = true;
static double unitDiscVertex[ NUM_OF_SIDES + 1 ][2];
if ( firstTime )
{
// Pre-compute and store the vertices' positions of a unit-radius disc.
firstTime = false;
unitDiscVertex[0][0] = disc[numDiscs].pos[0];
unitDiscVertex[0][1] = disc[numDiscs].pos[1];
for(int i=1; i<=NUM_OF_SIDES;i++){
unitDiscVertex[i][0] = disc[numDiscs].pos[0]+( 1*cos(i*2*PI/NUM_OF_SIDES));
unitDiscVertex[i][1] = disc[numDiscs].pos[1]+( 1*sin(i*2*PI/NUM_OF_SIDES));
}
}
// Draw the disc in its color as a GL_TRAINGLE_FAN.
glColor3f(disc[numDiscs].color[0], disc[numDiscs].color[1], disc[numDiscs].color[2]);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= NUM_OF_SIDES; i++) {
glVertex2f(unitDiscVertex[0][0],unitDiscVertex[0][1]);
glVertex2f(unitDiscVertex[i][0],unitDiscVertex[i][1]);
glVertex2f(unitDiscVertex[i+1][0],unitDiscVertex[i+2][1]);
}
glEnd();
}
/////////////////////////////////////////////////////////////////////////////
// The display callback function.
/////////////////////////////////////////////////////////////////////////////
void MyDisplay( void )
{
glClear( GL_COLOR_BUFFER_BIT );
if ( drawWireframe )
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
else
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
for ( int i = 0; i < numDiscs; i++ ) DrawDisc( &disc[i] );
glFlush();
}
/////////////////////////////////////////////////////////////////////////////
// The mouse callback function.
/////////////////////////////////////////////////////////////////////////////
void MyMouse( int btn, int state, int x, int y )
{
if ( btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
if ( numDiscs >= MAX_NUM_OF_DISCS )
printf( "Already reached maximum number of discs.\n" );
else
{
disc[numDiscs].pos[0]=x;
disc[numDiscs].pos[1]=y;
disc[numDiscs].radius=((MAX_RADIUS - MIN_RADIUS +1)*(double)rand()/MAX_RADIUS) + MIN_RADIUS;
disc[numDiscs].speed[0]=((MAX_X_SPEED - MIN_X_SPEED +1)*(double)rand()/MAX_X_SPEED)+ MIN_X_SPEED;
disc[numDiscs].speed[1]=((MAX_Y_SPEED - MIN_Y_SPEED +1)*(double)rand()/MAX_Y_SPEED) + MIN_Y_SPEED;
disc[numDiscs].color[0]= rand()%255;
disc[numDiscs].color[1]= rand()%255;
disc[numDiscs].color[2]= rand()%255;
numDiscs++;
glutPostRedisplay();
}
}
}
/////////////////////////////////////////////////////////////////////////////
// The reshape callback function.
// It also sets up the viewing.
/////////////////////////////////////////////////////////////////////////////
void MyReshape( int w, int h )
{
winWidth = w;
winHeight = h;
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
//****************************
//*** WRITE YOUR CODE HERE ***
double hScale = static_cast<int>(h) / static_cast<int>(600);
double wScale = static_cast<int>(w) / static_cast<int>(800);
glOrtho(0, wScale, 0, hScale, -1, 1);
//****************************
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
/////////////////////////////////////////////////////////////////////////////
// The keyboard callback function.
/////////////////////////////////////////////////////////////////////////////
void MyKeyboard( unsigned char key, int x, int y )
{
switch ( key )
{
// Quit program.
case 'q':
case 'Q': exit(0);
break;
// Toggle wireframe or filled polygons.
case 'w':
case 'W': drawWireframe = !drawWireframe;
glutPostRedisplay();
break;
// Reset and erase all discs.
case 'r':
case 'R': numDiscs = 0;
glutPostRedisplay();
break;
}
}
/////////////////////////////////////////////////////////////////////////////
// Updates the positions of all the discs.
/////////////////////////////////////////////////////////////////////////////
void UpdateAllDiscPos( void )
{
for ( int i = 0; i < numDiscs; i++ )
{
disc[i].pos[0]+=disc[i].speed[0];
disc[i].pos[1]+=disc[i].speed[1];
if (disc[i].pos[0]+disc[i].radius<=0){
disc[i].speed[0]=-disc[i].speed[0];
}
if (disc[i].pos[0]+disc[i].radius>=winWidth){
disc[i].speed[0]=-disc[i].speed[0];
}
if (disc[i].pos[1]+disc[i].radius<=0){
disc[i].speed[1]=-disc[i].speed[1];
}
if (disc[i].pos[1]+disc[i].radius>=winHeight){
disc[i].speed[1]=-disc[i].speed[1];
}
//****************************
}
glutPostRedisplay();
}
/////////////////////////////////////////////////////////////////////////////
// The timer callback function.
/////////////////////////////////////////////////////////////////////////////
void MyTimer( int v )
{
glutPostRedisplay();
glutTimerFunc(16, MyTimer, 1);
}
/////////////////////////////////////////////////////////////////////////////
// The init function.
// It initializes some OpenGL states.
/////////////////////////////////////////////////////////////////////////////
void MyInit( void )
{
glClearColor( 0.0, 0.0, 0.0, 1.0 ); // Black background color.
glShadeModel( GL_FLAT );
}
/////////////////////////////////////////////////////////////////////////////
// The main function.
/////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE ); //*** MODIFY THIS ***
glutInitWindowSize( winWidth, winHeight );
glutCreateWindow( "Lab1" );
MyInit();
// Register the callback functions.
glutDisplayFunc( MyDisplay );
glutReshapeFunc( MyReshape );
glutMouseFunc( MyMouse );
glutKeyboardFunc( MyKeyboard );
glutIdleFunc( UpdateAllDiscPos ); //*** MODIFY THIS ***
// Display user instructions in console window.
printf( "Click LEFT MOUSE BUTTON in window to add new disc.\n" );
printf( "Press 'w' to toggle wireframe.\n" );
printf( "Press 'r' to erase all discs.\n" );
printf( "Press 'q' to quit.\n\n" );
// Enter GLUT event loop.
glutMainLoop();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/24551.html
標籤:C++ 語言
下一篇:怎么做?
