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

[OpenGL] VBO, wyświetla tylko jeden trójkąt...

Ostatnio zmodyfikowano 2012-12-22 11:41
Autor Wiadomość
Mrusio
Temat założony przez niniejszego użytkownika
[OpenGL] VBO, wyświetla tylko jeden trójkąt...
» 2012-12-21 20:01:09
Witam. Uczę się openGL. Używając Vertex Arrays zrobiłem planszę składającą się z kwadratów (2 trójkąty). Wszystko było ok, następnie spróbowałem zrobić to samo za pomocą VBO i plansza nie renderuje się poza jednym trójkątem...

generowanie Vertex Array i VBO:

C/C++
void Program1::init_map() {
    //utworzenie mapy
   
    float heightmap[ 100 ][ 100 ];
   
    for( int i = 0; i < this->map_width; i++ ) {
        for( int j = 0; j < this->map_height; j++ ) {
            heightmap[ i ][ j ] = 0; //wysokość danego punktu
        }
    }
   
    this->vertices1 = new GLfloat[ this->map_width * this->map_height * 4 * 3 * 2 ];
    this->texVertices = new GLfloat[ this->map_width * this->map_height * 4 * 2 * 2 ];
    int n = 0;
   
    for( int i = 0; i < this->map_width - 1; i++ ) {
        for( int j = 0; j < this->map_height - 1; j++ ) {
           
            //trojkat1
            this->vertices1[ n ] = i + 1; n++; //x3
            this->vertices1[ n ] = heightmap[ i + 1 ][ j + 1 ]; n++; //y3
            this->vertices1[ n ] = j + 1; n++; //z3
           
            this->vertices1[ n ] = i + 1; n++; //x2
            this->vertices1[ n ] = heightmap[ i + 1 ][ j ]; n++; //y2
            this->vertices1[ n ] = j; n++; //z2
           
            this->vertices1[ n ] = i; n++; //x4
            this->vertices1[ n ] = heightmap[ i ][ j + 1 ]; n++; //y4
            this->vertices1[ n ] = j + 1; n++; //z4
           
           
            ///trojkat2
           
            this->vertices1[ n ] = i; n++; //x4
            this->vertices1[ n ] = heightmap[ i ][ j + 1 ]; n++; //y4
            this->vertices1[ n ] = j + 1; n++; //z4
           
           
            this->vertices1[ n ] = i + 1; n++; //x2
            this->vertices1[ n ] = heightmap[ i + 1 ][ j ]; n++; //y2
            this->vertices1[ n ] = j; n++; //z2
           
            this->vertices1[ n ] = i; n++; //x1
            this->vertices1[ n ] = heightmap[ i ][ j ]; n++; //y1
            this->vertices1[ n ] = j; n++; //z1
        }
    }
   
    //textury
    n = 0;
    for( int i = 0; i < this->map_width; i++ ) {
        for( int j = 0; j < this->map_height; j++ ) {
            //trojkat 1
           
            this->texVertices[ n ] = 1; n++;
            this->texVertices[ n ] = 1; n++;
           
            this->texVertices[ n ] = 1; n++;
            this->texVertices[ n ] = 0; n++;
           
            this->texVertices[ n ] = 0; n++;
            this->texVertices[ n ] = 1; n++;
           
            //trojkat 2
            this->texVertices[ n ] = 0; n++;
            this->texVertices[ n ] = 1; n++;
           
            this->texVertices[ n ] = 1; n++;
            this->texVertices[ n ] = 0; n++;
           
            this->texVertices[ n ] = 0; n++;
            this->texVertices[ n ] = 0; n++;
           
           
        }
    }
   
    ////////VBO
   
    glGenBuffers( 1, & vboId );
    glBindBuffer( GL_ARRAY_BUFFER, vboId );
    glBufferData( GL_ARRAY_BUFFER, sizeof( this->vertices1 ) + sizeof( this->texVertices ), 0, GL_STATIC_DRAW );
    glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof( this->vertices1 ), this->vertices1 ); // copy vertices starting from 0 offest
    glBufferSubData( GL_ARRAY_BUFFER, sizeof( this->vertices1 ), sizeof( this->texVertices ), this->texVertices ); // copy normals after vertices
   
   
    glBindBuffer( GL_ARRAY_BUFFER, 0 );
   
   
   
   
   
}

Renderowanie:
C/C++
void Program1::draw_map() {
    int ile_punktow = 3;
    int size = this->map_width * this->map_height * ile_punktow;
   
   
   
   
   
    glBindTexture( GL_TEXTURE_2D, core->textures[ "trawa" ] );
   
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    glEnableClientState( GL_VERTEX_ARRAY );
   
   
    glBindBuffer( GL_ARRAY_BUFFER, vboId );
   
   
   
    glVertexPointer( 3, GL_FLOAT, 0, 0 );
    glTexCoordPointer( 2, GL_FLOAT, 0,( void * )( sizeof( this->vertices1 ) ) );
   
   
    glDrawArrays( GL_TRIANGLES, 0, size );
   
   
   
   
    ////
   
    glDisableClientState( GL_VERTEX_ARRAY ); // disable vertex arrays
    glDisableClientState( GL_TEXTURE_COORD_ARRAY );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );
   
}

Zamiast całej planszy otrzymuję:
(W dodatku tekstura jest jakaś rozmazana)
Co robię źle?
P-71577
Mrusio
Temat założony przez niniejszego użytkownika
» 2012-12-22 11:41:20
ok, znalazłem problem:

C/C++
this->vertices1 = new GLfloat[ this->map_width * this->map_height * 4 * 3 * 2 ];
this->texVertices = new GLfloat[ this->map_width * this->map_height * 4 * 2 * 2 ];

zapomniałem, że sizeof() nie zwraca rozmiaru danych zawartych w pamięci wskazywanej przez wskaźnik ;)
P-71636
« 1 »
  Strona 1 z 1