veelenar Temat założony przez niniejszego użytkownika |
[OpenGL] Kolorowanie kostki Rubika w GLUT » 2015-11-21 10:13:07 Hej, dopiero zaczynam swoją przygodę z OpenGL i pisząc kostkę Rubika chciałem w pewnym momencie przerzucić się na bibliotekę GLUT. Przepisałem program, lecz nie koloruje kostki(wyświetla czarną). Próbuję rozwiązań z tego kursu: http://cpp0x.pl/kursy/Kurs-OpenGL-C++/Kolory-i-cieniowanie/109, jednak to nie pomaga... Jakieś sugestie? Poniżej zamieszczam kod źródłowy: #include <math.h> #include <stdlib.h> #include <Windows.h> #include <gl\GL.h> #include <gl\GLU.h> #include <glut.h> #include "Kostka.h"
enum { FULL_WINDOW, ASPECT_1_1, EXIT };
int aspect = FULL_WINDOW;
const GLdouble left = - 10.0; const GLdouble right = 10.0; const GLdouble bottom = - 10.0; const GLdouble top = 10.0; const GLdouble neaar = 50.0; const GLdouble faar = 70.0;
GLfloat scale = 1.0;
GLfloat rotatex = 0.0; GLfloat rotatey = 0.0;
GLfloat translatex = 0.0; GLfloat translatey = 0.0;
int button_state = GLUT_UP;
int button_x, button_y;
extern void UstawKolor( int numerKoloru );
void RysujSzescian( float x, float y, float z, int colorx, int colory, int colorz ) { float da = 0.6f / 2; float da_2 = da - 0.01f; UstawKolor( colorz ); glBegin( GL_POLYGON ); glVertex3f( x + da_2, y - da_2, z - da_2 ); glVertex3f( x + da_2, y + da_2, z - da_2 ); glVertex3f( x - da_2, y + da_2, z - da_2 ); glVertex3f( x - da_2, y - da_2, z - da_2 ); glEnd(); glBegin( GL_POLYGON ); glVertex3f( x + da_2, y - da_2, z + da_2 ); glVertex3f( x + da_2, y + da_2, z + da_2 ); glVertex3f( x - da_2, y + da_2, z + da_2 ); glVertex3f( x - da_2, y - da_2, z + da_2 ); glEnd(); UstawKolor( colorx ); glBegin( GL_POLYGON ); glVertex3f( x + da_2, y + da_2, z - da_2 ); glVertex3f( x + da_2, y + da_2, z + da_2 ); glVertex3f( x + da_2, y - da_2, z + da_2 ); glVertex3f( x + da_2, y - da_2, z - da_2 ); glEnd(); glBegin( GL_POLYGON ); glVertex3f( x - da_2, y + da_2, z - da_2 ); glVertex3f( x - da_2, y + da_2, z + da_2 ); glVertex3f( x - da_2, y - da_2, z + da_2 ); glVertex3f( x - da_2, y - da_2, z - da_2 ); glEnd(); UstawKolor( colory ); glBegin( GL_POLYGON ); glVertex3f( x + da_2, y + da_2, z - da_2 ); glVertex3f( x + da_2, y + da_2, z + da_2 ); glVertex3f( x - da_2, y + da_2, z + da_2 ); glVertex3f( x - da_2, y + da_2, z - da_2 ); glEnd(); glBegin( GL_POLYGON ); glVertex3f( x + da_2, y - da_2, z - da_2 ); glVertex3f( x + da_2, y - da_2, z + da_2 ); glVertex3f( x - da_2, y - da_2, z + da_2 ); glVertex3f( x - da_2, y - da_2, z - da_2 ); glEnd(); }
void UstawKolor( int numerKoloru ) { switch( numerKoloru ) { case 0: glColor3ub( 0, 0, 0 ); break; case 1: glColor3ub( 7, 205, 22 ); break; case 2: glColor3ub( 255, 255, 0 ); break; case 3: glColor3ub( 255, 255, 255 ); break; case 4: glColor3ub( 0, 0, 255 ); break; case 5: glColor3ub( 255, 0, 0 ); break; case 6: glColor3ub( 255, 127, 39 ); break; } }
void RysujKostke() { for( int x = 0; x < 3; x++ ) { for( int y = 0; y < 3; y++ ) { for( int z = 0; z < 3; z++ ) { int * kolor = kostka[ x ][ y ][ z ].kolor; RysujSzescian( x * 0.6f - 0.6f,( y *- 0.6f ) + 0.6,( z *- 0.6f ) + 0.6f, kolor[ 0 ], kolor[ 1 ], kolor[ 2 ] ); } } } }
void Display() { glClearColor( 1.0, 1.0, 1.0, 1.0 ); glClear( GL_COLOR_BUFFER_BIT ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 0, 0, -( neaar + faar ) / 2 ); glTranslatef( translatex, translatey, 0.0 ); glScalef( scale, scale, scale ); glRotatef( rotatex, 1.0, 0, 0 ); glRotatef( rotatey, 0, 1.0, 0 ); RysujKostke(); glFlush(); glutSwapBuffers(); }
void Reshape( int width, int height ) { glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); if( aspect == ASPECT_1_1 ) { if( width < height && width > 0 ) glFrustum( left, right, bottom * height / width, top * height / width, neaar, faar ); else if( width >= height && height > 0 ) glFrustum( left * width / height, right * width / height, bottom, top, neaar, faar ); } else glFrustum( left, right, bottom, top, neaar, faar ); Display(); }
void Keyboard( unsigned char key, int x, int y ) { if( key == '+' ) scale += 0.1; else if( key == '-' && scale > 0.1 ) scale -= 0.1; Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) ); }
void SpecialKeys( int key, int x, int y ) { switch( key ) { case GLUT_KEY_LEFT: rotatey -= 1; break; case GLUT_KEY_UP: rotatex -= 1; break; case GLUT_KEY_RIGHT: rotatey += 1; break; case GLUT_KEY_DOWN: rotatex += 1; break; } Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) ); }
void MouseButton( int button, int state, int x, int y ) { if( button == GLUT_LEFT_BUTTON ) { button_state = state; if( state == GLUT_DOWN ) { button_x = x; button_y = y; } } }
void MouseMotion( int x, int y ) { if( button_state == GLUT_DOWN ) { translatex += 1.1 *( right - left ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x ); button_x = x; translatey += 1.1 *( top - bottom ) / glutGet( GLUT_WINDOW_HEIGHT ) *( button_y - y ); button_y = y; glutPostRedisplay(); } }
void Menu( int value ) { switch( value ) { case FULL_WINDOW: aspect = FULL_WINDOW; Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) ); break; case ASPECT_1_1: aspect = ASPECT_1_1; Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) ); break; } }
int main( int argc, char * argv[] ) { glutInit( & argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize( 400, 400 ); #ifdef WIN32 glutCreateWindow( "Przekształcenia" ); #else glutCreateWindow( "Przeksztalcenia" ); #endif glutDisplayFunc( Display ); glutReshapeFunc( Reshape ); glutKeyboardFunc( Keyboard ); glutSpecialFunc( SpecialKeys ); glutMouseFunc( MouseButton ); glutMotionFunc( MouseMotion ); int MenuAspect = glutCreateMenu( Menu ); #ifdef WIN32 glutAddMenuEntry( "Aspekt obrazu - całe okno", FULL_WINDOW ); #else glutAddMenuEntry( "Aspekt obrazu - cale okno", FULL_WINDOW ); #endif glutAddMenuEntry( "Aspekt obrazu 1:1", ASPECT_1_1 ); glutCreateMenu( Menu ); glutAddSubMenu( "Aspekt obrazu", MenuAspect ); #ifdef WIN32 glutAddMenuEntry( "Wyjście", EXIT ); #else glutAddMenuEntry( "Wyjscie", EXIT ); #endif glutAttachMenu( GLUT_RIGHT_BUTTON ); glutMainLoop(); return 0; } #include "Kostka.h"
elementKostki kostka[ 3 ][ 3 ][ 3 ];
void GenerujKostke() { for( int x = 0; x < 3; x++ ) { for( int y = 0; y < 3; y++ ) { for( int z = 0; z < 3; z++ ) { kostka[ x ][ y ][ z ].iloscScian = dwie; kostka[ x ][ y ][ z ].kolor[ 0 ] = 0; kostka[ x ][ y ][ z ].kolor[ 1 ] = 0; kostka[ x ][ y ][ z ].kolor[ 2 ] = 0; kostka[ z ][ y ][ x ].numerElementu = x + y + z; } } } kostka[ 1 ][ 1 ][ 0 ].iloscScian = jedna; kostka[ 1 ][ 1 ][ 2 ].iloscScian = jedna; kostka[ 1 ][ 0 ][ 1 ].iloscScian = jedna; kostka[ 1 ][ 2 ][ 1 ].iloscScian = jedna; kostka[ 0 ][ 1 ][ 1 ].iloscScian = jedna; kostka[ 2 ][ 1 ][ 1 ].iloscScian = jedna; for( int i = 0; i < 3; i++ ) { for( int j = 0; j < 3; j++ ) { kostka[ 0 ][ i ][ j ].kolor[ 0 ] = 6; kostka[ 2 ][ i ][ j ].kolor[ 0 ] = 5; kostka[ i ][ 0 ][ j ].kolor[ 1 ] = 3; kostka[ i ][ 2 ][ j ].kolor[ 1 ] = 2; kostka[ i ][ j ][ 0 ].kolor[ 2 ] = 4; kostka[ i ][ j ][ 2 ].kolor[ 2 ] = 1; if(( i == 0 && j == 0 ) ||( i == 0 && j == 2 ) ||( i == 2 && j == 0 ) ||( i == 2 && j == 2 ) ) { kostka[ i ][ j ][ 0 ].iloscScian = trzy; kostka[ i ][ j ][ 2 ].iloscScian = trzy; } } } } enum iloscScian { jedna, dwie, trzy };
struct elementKostki { int kolor[ 3 ]; iloscScian iloscScian; int numerElementu; };
extern elementKostki kostka[ 3 ][ 3 ][ 3 ];
extern void GenerujKostke(); Edit: Poprawiłem kod o wywołanie funkcji "GenerujKostke();", kolory się pojawiły, ale niestety, są źle wyświetlane i kostka wygląda krótko mówiąc okrutnie. Screenshot: http://s29.postimg.org/7k6pldubr/kostka_glut.png |