Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[c++][OpenGL][Glut] piramida sierpińskiego

Ostatnio zmodyfikowano 2015-11-30 18:00
Autor Wiadomość
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.
C/C++
#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 ;)

C/C++
#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 H=sqrt(((2.0/3.0)*h)^2+(a2-a1)(a2-a1));
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 );
        //glLineWidth(10);
        //glColor3f(1.0, 0.0f, 0.0f);
        glVertex3f( a1, b1, c1 );
        //glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f( a2, b2, c2 );
        //glColor3f(0.0f, 0.0f, 1.0f);
        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 ); //Wywolanie funkcji sierpinskiego
    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
P-140891
michal11
» 2015-11-27 09:28:34
Może napisz dokładnie co się dzieje, bo problem "efekt nie do końca jest taki jak powinien" niewiele mówi.
P-140897
aezakmi737
Temat założony przez niniejszego użytkownika
» 2015-11-28 14:45:02
Tak wygląda po kompilacji
Tak wygląda po kompilacji
 
Po skompilowaniu wygląda jakby było w 2d
Nie wiem właśnie, czy źle ustawiłem funkcje w rzutni czy to błąd logiczny kodu.
P-140988
ArgonZapan
» 2015-11-28 19:42:44
jak dla mnie rysunek wygląda tak jak powinien wyglądać, tylko go nie obróciłeś, a kolory są wszędzie takie same i się zlewa.
A po za tym

C/C++
if( licznik == 0 )
{
    glBegin( GL_TRIANGLES );
    //glLineWidth(10);
    //glColor3f(1.0, 0.0f, 0.0f);
    glVertex3f( a1, b1, c1 );
    //glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f( a2, b2, c2 );
    //glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f( a3, b3, c3 );
    glVertex3f( a4, b4, c4 );
   
    glEnd();
}

GL_TRIANGLES obsługuje 3 wierzchołki, a dostał 4
P-141020
aezakmi737
Temat założony przez niniejszego użytkownika
» 2015-11-30 18:00:03
Faktycznie to co się kompilowało było ok
Problem polegał na tym, że rzutnia była ustawiona statycznie i w dodatku pod takimi kątami, że ciężko było zaobserwować 3D
Dodałem funkcję zmieniającą wartości g_angle z klawiatury i teraz można swobodnie obracać piramidą.
Co do funkcji GL_TRIANGLES
Faktycznie - zamiast 4 funkcji rysujących trójkąty, dałem jedną 'rysującą' trójkąt o 4 wierzchołkach.
Dzięki za pomoc. Problem uważam za rozwiązany.
P-141182
« 1 »
  Strona 1 z 1