C++/OpenGL und ein Kreisel
-
Hi.
ich hab da n lustigen Code der einen Kreisel darstellt. und der kreisel dreht sich auch noch.
nun soll er sich nicht nur um sich selbst drehen, sondern auch noch im raum "schlingern".
hat jemand ne idee? code-schnipsel? vorlagen? :S#include <windows.h>
#include <iostream>
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>#include <iostream>
static GLint T0 = 0;
static GLint Frames = 0;
static GLint autoexit = 0;
static GLint win = 0;float cursorX = 0;
float cursorY = 0;static void drawCone(){
glNormal3f( 0.0, 1.0, 0.0);
glutSolidCone (2.0, 5.0, 15, 15);
glShadeModel( GL_SMOOTH);
}static GLfloat view_rotx = 100.0, view_roty = -10.0, view_rotz = -30.0;
static GLint kegel;
static GLfloat angle = 20.0;/*
*
*/
static void cleanup( void)
{
glDeleteLists( kegel, 1);
glutDestroyWindow( win);
}/*
*
*/
static void draw( void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glRotatef( view_rotx, 1.0, 0.0, 0.0);
glRotatef( view_roty, 0.0, 1.0, 0.0);
glRotatef( view_rotz, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef( -3.0, -2.0, 0.0);
glRotatef( angle, 0.0, 0.0, 1.0);
glCallList( kegel);
glPopMatrix();
glPopMatrix();
glPopMatrix();glutSwapBuffers();
Frames++;
{
GLint t = glutGet( GLUT_ELAPSED_TIME);
if ( t - T0 >= 5000) {
GLfloat seconds = (t - T0) / 1000.0;
GLfloat fps = Frames / seconds;
printf(
"%d frames in %6.3f seconds = %6.3f FPS\n",
Frames, seconds, fps
);
T0 = t;
Frames = 0;
if ( (t >= 999.0 * autoexit) && (autoexit)) {
cleanup();
exit(0);
}
}
}
}/*
*
*/
static void idle( void)
{
static double t0 = -1.;
double dt, t = glutGet( GLUT_ELAPSED_TIME) / 1000.0;
if ( t0 < 0.0)
t0 = t;
dt = t - t0;
t0 = t;angle += 70.0 * dt; /* 70 degrees per second /
angle = fmod( angle, 360.0); / prevents eventual overflow */glutPostRedisplay();
}/*
*
/
static void key( unsigned char k, int x, int y)
{
switch ( k) {
case 'z':
view_rotz += 5.0;
break;
case 'Z':
view_rotz -= 5.0;
break;
case 27: / Escape */
cleanup();
exit( 0);
break;
default:
return;
}
glutPostRedisplay();
}/*
*
*/
static void specialKey( int k, int x, int y)
{
switch ( k) {
case GLUT_KEY_UP:
view_rotx += 5.0;
break;
case GLUT_KEY_DOWN:
view_rotx -= 5.0;
break;
case GLUT_KEY_LEFT:
view_roty += 5.0;
break;
case GLUT_KEY_RIGHT:
view_roty -= 5.0;
break;
default:
return;
}
glutPostRedisplay();
}static void mouseListener( int button, int state, int x, int y ){
if( button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) {
return;
}cursorX = x;
cursorY = y;
std::cout<<"X: "<<cursorX<<std::endl;
std::cout<<"Y: "<<cursorY<<std::endl;glutPostRedisplay();
}/*
*
*/
static void reshape( int width, int height)
{
GLfloat h = (GLfloat) height / (GLfloat) width;glViewport( 0, 0, (GLint) width, (GLint) height);
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
glFrustum( -1.0, 1.0, -h, h, 5.0, 60.0);
glMatrixMode( GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0.0, 0.0, -40.0);
}/*
*
*/
static void init( int argc, char *argv[])
{
static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
GLint i;GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 0.6 };
GLfloat light_diffuse[] = { 2.0, 2.0, 1.0, 2.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 2.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);glEnable( GL_LIGHTING);
glEnable( GL_LIGHT0);
glEnable( GL_DEPTH_TEST);kegel = glGenLists( 1);
glNewList( kegel, GL_COMPILE);
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
drawCone();
glEndList();glEnable( GL_NORMALIZE);
for ( i=1; i<argc; i++ ) {
if ( strcmp( argv[i], "-info")==0) {
printf( "GL_RENDERER = %s\n", (char
glGetString( GL_RENDERER));
printf( "GL_VERSION = %s\n", (char
glGetString( GL_VERSION));
printf( "GL_VENDOR = %s\n", (char
glGetString( GL_VENDOR));
printf( "GL_EXTENSIONS = %s\n", (char
glGetString( GL_EXTENSIONS));
}
else if ( strcmp( argv[i], "-exit")==0) {
autoexit = 30;
printf( "Auto Exit after %i seconds.\n", autoexit );
}
}
}/*
*
*/
static void visible( int vis)
{
if ( vis == GLUT_VISIBLE)
glutIdleFunc( idle);
else
glutIdleFunc( NULL);
}/*
*
*/
int main( int argc, char *argv[])
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);glutInitWindowPosition( 0, 0);
glutInitWindowSize( 300, 300);
win = glutCreateWindow( "Exercise 2");
init( argc, argv);glutDisplayFunc( draw);
glutReshapeFunc( reshape);
glutMouseFunc( mouseListener);
glutKeyboardFunc( key);
glutSpecialFunc( specialKey);
glutVisibilityFunc( visible);glutMainLoop();
return 0;
}
-