first commit
This commit is contained in:
131
CS4451/oglexamples/spinner.cpp
Normal file
131
CS4451/oglexamples/spinner.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/* Rotates3D points. Changes points each time mouse is clicked (courtesy of Brooks van Horn) */
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include <GL/glut.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void mouse( int, int, int, int );
|
||||
void display( void );
|
||||
void reshape( int, int );
|
||||
void keyboard( unsigned char, int, int );
|
||||
void Idle( void );
|
||||
void InitializeVariables( void ); /// forwarding functions
|
||||
|
||||
int xdim, ydim; /// screen dimensions
|
||||
|
||||
struct vPoint
|
||||
{
|
||||
inline float &operator[] ( int index )
|
||||
{
|
||||
return element[index];
|
||||
}
|
||||
float element[3];
|
||||
}; //
|
||||
|
||||
const float X_STEP = 0.005;
|
||||
const float Y_STEP = 0.020;
|
||||
const float Z_STEP = 0.025; //
|
||||
|
||||
bool flag; /// flag to tell us if there is anything to draw yet/
|
||||
|
||||
vPoint rotation; // current rotation
|
||||
|
||||
const int MAX_DOTS = 10; /// # of dots to generate and render/
|
||||
vPoint dots[ MAX_DOTS ]; /// the set of points to render/
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
InitializeVariables(); /// nothing to do with OpenGL. My function that zeroes variables and such stuff./
|
||||
|
||||
xdim = ( argc > 1 ) ? atoi( argv[ 1 ] ) : 500; ydim = ( argc > 2 ) ? atoi( argv[ 2 ] ) : 500;
|
||||
|
||||
glutInit( &argc, argv ); //* Initialization function *//
|
||||
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /// Set double buffer and GRB modes/
|
||||
glutInitWindowSize( xdim, ydim ); /// Set the screen size/
|
||||
glutCreateWindow( "This is the title of the window.");
|
||||
glClearColor( 0.0, 0.0, 0.0, 0.0 ); /// clear color and alpha at each pixel/
|
||||
glShadeModel( GL_FLAT ); /// set shading mode to flat (constant color for each polygon)/
|
||||
glutDisplayFunc( display ); /// tells OpenGL what function to call when it needs to redraw/
|
||||
glutReshapeFunc( reshape ); /// tells OpenGL what function to call when window is reshaped/
|
||||
glutMouseFunc( mouse ); /// tells OpenGL what function to call when a mouse button is pressed/
|
||||
glutKeyboardFunc( keyboard ); /// tells OpenGL to call the keyboard function with current parameters/
|
||||
glutIdleFunc( Idle );
|
||||
glutMainLoop(); /// important call to include after the above 3/
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void InitializeVariables( void ) {rotation[ 0 ] = rotation[ 1 ] = rotation[ 2 ] = 0.0; flag = true; }
|
||||
|
||||
void mouse( int button, int state, int x, int y )
|
||||
/*
|
||||
button: GLUT_LEFT_BUTTON or GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON,
|
||||
state : state of the button: pressed (GLUT_DOWN) or not pressed (GLUT_UP),
|
||||
x, y : position of mouse
|
||||
*/
|
||||
{
|
||||
|
||||
if ( state == GLUT_UP ) return ; /// we don't care when the mouse button is released/
|
||||
|
||||
for (int i = 0; i<MAX_DOTS; i++) for (int j = 0; j<3; j++) dots[i][j] = (float) rand() / (float) RAND_MAX; /// Generate dots/
|
||||
|
||||
flag = false; /// we now have something to display/
|
||||
|
||||
glutPostRedisplay();} /// need to call display again so that a new thing is put on the screen/
|
||||
|
||||
|
||||
|
||||
void keyboard( unsigned char c, int x, int y )
|
||||
//* c: ASCII character pressed (excluding shift, ctrl, alt), (x,y) = position of the mouse */
|
||||
{
|
||||
if ( c == 27 ) exit( 0 );
|
||||
} // "escape" key
|
||||
|
||||
void Idle( void )
|
||||
{
|
||||
rotation[0]+=X_STEP; rotation[1]+=Y_STEP;
|
||||
rotation[2]+=Z_STEP; glutPostRedisplay();
|
||||
}
|
||||
|
||||
void display( void )
|
||||
{
|
||||
|
||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||
|
||||
if( flag )
|
||||
{
|
||||
glutSwapBuffers();
|
||||
return;
|
||||
}
|
||||
glPushMatrix(); /// push matrix on stack/
|
||||
|
||||
glColor3f( 1.0, 1.0, 1.0 ); /// Here I specify a color by giving R-G-B values from 0 to 1./
|
||||
glTranslatef( 0.0, 0.0, -3.0 ); /// move the dots so that we can see them/
|
||||
glRotatef(rotation[0],1,0,0); glRotatef(rotation[1],0,1,0);
|
||||
glRotatef(rotation[2],0,0,1); /// rotate the dots/
|
||||
|
||||
glBegin( GL_POINTS );
|
||||
|
||||
for ( int i = 0; i < MAX_DOTS; i++ )
|
||||
glVertex3f(dots[i][0],dots[i][1],dots[i][2]);
|
||||
|
||||
glEnd();
|
||||
|
||||
glPopMatrix();
|
||||
glutSwapBuffers();
|
||||
} /// This says: "DRAW WHAT YOU'VE GOT!"/
|
||||
|
||||
|
||||
|
||||
void reshape( int new_width, int new_height )
|
||||
{
|
||||
xdim = new_width; ydim = new_height;
|
||||
|
||||
glViewport(0,0,(GLsizei) new_width, (GLsizei) new_height ); /// This resets the viewing port/
|
||||
glMatrixMode( GL_PROJECTION ); /// This resets the use of Matrices based upon the new view/
|
||||
glLoadIdentity();
|
||||
gluPerspective( 60.0, (GLfloat) xdim / (GLfloat) ydim, 0.1, 20.0 ); /// reset the viewing projection/
|
||||
glMatrixMode( GL_MODELVIEW );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user