aezakmi737 Temat założony przez niniejszego użytkownika |
[c++][OpenGL][Glut] piramida sierpińskiego » 2015-11-27 01:08:25 Witam Mam mały problem z piramidą sierpińśkiego. Pierwszym etapem zadania było stworzenie trójkąta sierpińskiego, co udało mi się zrobić na podstawie tego kodu. #include <windows.h> #include <gl\gl.h> #include "gl\glut.h" int g_depth = 7;
void init( void ) { glClearColor( 0, 0, 0, 0 ); }
void reshape( int w, int h ) { glViewport( 0, 0, w, h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); if( h == 0 ) gluPerspective( 80,( float ) w, 1.0, 5000.0 ); else gluPerspective( 80,( float ) w /( float ) h, 1.0, 5000.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); }
void sierpin( float x1, float y1, float x2, float y2, float x3, float y3, int depth ) { float x12 =( x1 + x2 ) / 2.0, y12 =( y1 + y2 ) / 2.0, x13 =( x1 + x3 ) / 2.0, y13 =( y1 + y3 ) / 2.0, x23 =( x2 + x3 ) / 2.0, y23 =( y2 + y3 ) / 2.0; if( depth == 1 ) { glBegin( GL_TRIANGLES ); glVertex3f( x1, y1, 0 ); glVertex3f( x2, y2, 0 ); glVertex3f( x3, y3, 0 ); glEnd(); } else { sierpin( x1, y1, x12, y12, x13, y13, depth - 1 ); sierpin( x12, y12, x2, y2, x23, y23, depth - 1 ); sierpin( x13, y13, x23, y23, x3, y3, depth - 1 ); } }
void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); glLoadIdentity(); glTranslatef( 0, 0.1, - 1 ); glScalef( 0.8, 0.7, 0.8 ); glColor3f( 0, 1, 0 ); glBegin( GL_LINE_LOOP ); glVertex3f( - 1, - 1, 0 ); glVertex3f( 0, 1, 0 ); glVertex3f( 1, - 1, 0 ); glEnd(); sierpin( - 1, - 1, 0, 1, 1, - 1, g_depth ); glutSwapBuffers(); }
int main( int argc, char * argv[] ) { glutInit( & argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize( 500, 500 ); glutInitWindowPosition( 100, 200 ); glutCreateWindow( "Trojkat Sierpinskiego" ); init(); glutDisplayFunc( display ); glutReshapeFunc( reshape ); glutMainLoop(); return 0; }
Na tej podstawie zabrałem się też za piramidę i tu pojawia się mały problem, bo po kompilacji efekt nie do końca jest taki jak powinien ;) #include <windows.h> #include <gl\gl.h> #include "gl\glut.h" #include <cmath>
float g_angley = 0; float g_anglex = 0;
int licznik = 3; float a1 = - 1.0; float a2 = 1.0; float a3 =( a1 + a2 ) / 2; float a4 = 0.0; float b1 = - 1.0; float b2 = - 1.0; float h =( a2 - a1 ) * sqrt( 3 ) / 2; float b3 = b1 + h; float b4 =( 1 / 3 ) * h; float c1 = 0.0; float c2 = 0.0; float c3 = 0.0;
float c4 = 1.0;
void init( void ) { glClearColor( 0, 0, 0, 0 ); glEnable( GL_COLOR_MATERIAL ); glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); glClearDepth( 1.0f ); glEnable( GL_DEPTH_TEST ); glDepthFunc( GL_LEQUAL ); }
void reshape( int w, int h ) { glViewport( 0, 0, w, h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); if( h == 0 ) gluPerspective( 30,( float ) w, 1.0, 5000.0 ); else gluPerspective( 30,( float ) w /( float ) h, 1.0, 5000.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); }
void sierpinski( float a1, float b1, float c1, float a2, float b2, float c2, float a3, float b3, float c3, float a4, float b4, float c4, int licznik ) { float a12 =( a1 + a2 ) / 2, b12 =( b1 + b2 ) / 2, c12 =( c1 + c2 ) / 2.0; float a23 =( a2 + a3 ) / 2, b23 =( b2 + b3 ) / 2, c23 =( c2 + c3 ) / 2; float a13 =( a1 + a3 ) / 2, b13 =( b1 + b3 ) / 2, c13 =( c1 + c3 ) / 2; float a14 =( a1 + a4 ) / 2, b14 =( b1 + b4 ) / 2, c14 =( c1 + c4 ) / 2; float a24 =( a2 + a4 ) / 2, b24 =( b2 + b4 ) / 2, c24 =( c2 + c4 ) / 2; float a34 =( a3 + a4 ) / 2, b34 =( b3 + b4 ) / 2, c34 =( c2 + c4 ) / 2; if( licznik == 0 ) { glBegin( GL_TRIANGLES ); glVertex3f( a1, b1, c1 ); glVertex3f( a2, b2, c2 ); glVertex3f( a3, b3, c3 ); glVertex3f( a4, b4, c4 ); glEnd(); } else { sierpinski( a1, b1, c1, a12, b12, c12, a13, b13, c13, a14, b14, c14, licznik - 1 ); sierpinski( a12, b12, c12, a2, b2, c2, a23, b23, c23, a24, b24, c24, licznik - 1 ); sierpinski( a3, b3, c3, a23, b23, c23, a13, b13, c13, a34, b34, c34, licznik - 1 ); sierpinski( a14, b14, c14, a24, b24, c24, a34, b34, c34, a4, b4, c4, licznik - 1 ); } }
void display( void ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glTranslatef( 0, - 1, - 10 ); glRotatef( g_anglex, 1, 1, 1 ); glRotatef( g_angley, 1, 1, 1 ); glColor3f( 0, 1, 0 ); sierpinski( a1, b1, c1, a2, b2, c2, a3, b3, c3, a4, b4, c4, licznik ); glutSwapBuffers(); }
int main( int argc, char * argv[] ) { glutInit( & argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize( 500, 500 ); glutInitWindowPosition( 100, 200 ); glutCreateWindow( "Trojkat Sierpinskiego" ); init(); glutDisplayFunc( display ); glutReshapeFunc( reshape ); glutMainLoop(); return 0; }
To początki OpenGL i nie bardzo wiem gdzie jest błąd- czy w funkcji tworzącej trójkąty, czy też może coś nie tak zrobiłem z funkcjami OGL Będę wdzięczny za każdą pomoc i wskazówki |