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

[OpenGL] Nie mogę ustawić koloru rysowanego wielokąta

Ostatnio zmodyfikowano 2011-07-21 23:45
Autor Wiadomość
qba10
Temat założony przez niniejszego użytkownika
[OpenGL] Nie mogę ustawić koloru rysowanego wielokąta
» 2011-07-21 18:05:45
Witam.
Mam programik który rysuje mi za pomocą tekstur w 2D pewną mapę;
Po wygenerowaniu chcę na ten mapie narysować czerwony prostokąt. Próbuje zrobić to w taki sposób:
C/C++
glBegin( GL_POLYGON );
glColor3f( 1.0f, 0.0f, 0.0f );
// kolejne wierzchołki wielokąta
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
glVertex3f( 1.0, 1.0, 0.0 );
glVertex3f( 1.0, 0.0, 0.0 );

// koniec definicji prymitywu
glEnd();
Jednak gdy to zrobię to, funckja
C/C++
glColor3f( 1.0f, 0.0f, 0.0f );
powoduje że cała na rysowana mapa jest czerwona (generalnie wszystko jakby miało tylko odcień czerwony)
P-36495
DejaVu
» 2011-07-21 18:11:16
Ustaw kolor biały to model będzie pokryty teksturą z prawidłowymi barwami.
C/C++
glColor3f( 1.0f, 1.0f, 1.0f );
P-36497
qba10
Temat założony przez niniejszego użytkownika
» 2011-07-21 18:21:06
Ok tylko że ja chcę narysować ten prostokąt i chcę żeby miał czerwony kolor. A nie wiem czemu ta funkcja glcolor3f zamiast ustawic kolor tego prostokątu to zmienia barwe tekstur.
P-36499
m4tx
» 2011-07-21 18:22:08
Ta funkcja nie zmienia tylko barwy tekstur, a zarówno kolor prostokąta jak i tekstury.
P-36500
qba10
Temat założony przez niniejszego użytkownika
» 2011-07-21 18:27:22
Zmieniłem tamto na to bo się zorientowałem że tamto nie rysuje mi prostokąta:
C/C++
glColor3f( 1.0f, 1.0f, 1.0f );
glRecti( 100, 100, 200, 200 );

Efekt jest taki że mam narysowaną mapę i fioletowy prostokąt na tej mapie;
Jak zmienić teraz kolor tego prostokąta ?
P-36503
DejaVu
» 2011-07-21 20:49:51
Daj więcej kodu bo Twoje zeznania są sprzeczne z kodem, który wklejasz :)
P-36517
qba10
Temat założony przez niniejszego użytkownika
» 2011-07-21 21:08:54
No to ok. Całego projektu nie dam  bo nie wiem czy chce wam się przeglądać wszystkie pliki (jeżeli tak to pisać dam paczkę z cały projektem)
Dam te metody gdzie używam OpenGl'a
To inicjalizacja samego OpenGL
C/C++
void Core::initOpenGL() {
    glClearColor( 0, 0, 0, 0 );
   
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture
    glEnable( GL_BLEND );
    glEnable( GL_DEPTH_TEST );
    /* Enable smooth shading */
    glShadeModel( GL_SMOOTH );
    /* Depth buffer setup */
    glClearDepth( 1.0f );
    /* Enables Depth Testing */
    glEnable( GL_DEPTH_TEST );
    /* The Type Of Depth Test To Do */
    glDepthFunc( GL_LEQUAL );
    /* Really Nice Perspective Calculations */
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    glViewport( 0, 0,( GLsizei ) this->windowWidth,( GLsizei ) this->windowHeight );
   
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
   
    glOrtho( 0,( GLsizei ) this->windowWidth,( GLsizei ) this->windowHeight, 0,
    - 1, 1 );
   
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
   
   
   
}
Ładiowanie Sprite'a
C/C++
void Map::loadSpriteOpenGL( const char * file ) {
    SDL_Surface * surface; // this surface will tell us the details of the image
    GLenum texture_format;
    GLint nofcolors;
   
    if(( surface = IMG_Load( file ) ) ) {
       
        // Check that the image’s width is a power of 2
        if(( surface->w &( surface->w - 1 ) ) != 0 ) {
            cout << "OpenGL warning: texture's width is not a power of 2 "
            << endl;
        }
        this->widthOpenGLTexture = surface->w;
        // Also check if the height is a power of 2
        if(( surface->h &( surface->h - 1 ) ) != 0 ) {
            cout << "OpneGL warning: texture's width is not a power of 2"
            << endl;
        }
        this->heightOpenGLTexture = surface->h;
       
        //get number of channels in the SDL surface
        nofcolors = surface->format->BytesPerPixel;
       
        //contains an alpha channel
        if( nofcolors == 4 ) {
            cout << "OpenGL: texture format: 4 BYTES PER PIXEL" << endl;
            if( surface->format->Rmask == 0x000000ff )
                 texture_format = GL_RGBA;
            else
                 texture_format = GL_BGRA;
           
        } else if( nofcolors == 3 ) //no alpha channel
        {
            cout << "OpenGL: texture format: 3 BYTES PER PIXEL" << endl;
           
            if( surface->format->Rmask == 0x000000ff )
                 texture_format = GL_RGB;
            else
                 texture_format = GL_BGR;
           
        } else {
            cout
            << "OpenGL warning: the texture is not truecolor…this will break "
            << endl;
        }
       
        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, & this->openGLTexture );
       
        // Bind the texture object
        glBindTexture( GL_TEXTURE_2D, this->openGLTexture );
       
        // Set the texture’s stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
       
        glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h - 50,
        0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );
    } else {
        printf( "SDL could not load texture: %s\n", SDL_GetError() );
        SDL_Quit();
    }
   
    // Free the SDL_Surface only if it was successfully created
    if( surface ) {
        SDL_FreeSurface( surface );
    }
   
}
Rysowanie mapy
C/C++
void Map::drawTileOpenGL() {
    // Clear the screen before drawing
    glClear( GL_COLOR_BUFFER_BIT );
   
    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, this->openGLTexture );
   
    GLfloat a;
    GLfloat b;
    a =( GLfloat ) this->tileSizeX / this->widthOpenGLTexture;
    b =( GLfloat ) this->tileSizeY / this->heightOpenGLTexture;
   
    int i;
    int bitmapElementsRows;
    int srcX;
    int srcY;
    int ax = 0;
    int ay = 0;
    for( int y = 0; y <=( int )( this->winHeight / this->tileSizeY ); y++ ) {
        for( int x = 0; x <=( int )( this->winWidth / this->tileSizeX ); x++ ) {
           
            ax = x + this->visibleX;
            ay = y + this->visibleY;
            if( ax < this->mapSizeX && ay < this->mapSizeY && ax >= 0 && ay >= 0 )
                 i = Area[ ax ][ ay ];
            else
                 i = 0;
           
            //cout << this->visibleY << endl;
            bitmapElementsRows = this->widthOpenGLTexture / this->tileSizeX;
            srcX = i % bitmapElementsRows;
            srcY = i / bitmapElementsRows;
           
            glBegin( GL_QUADS );
            // Top-left vertex (corner)
            //  *_|_
            //    |
           
            glTexCoord2f( srcX * a, srcY * b );
            glVertex3i(( x ) * this->tileSizeX,( y ) * this->tileSizeY, 0 );
           
            // Dolny-lewy vertex (corner)
            //   _|_
            //   *|
            glTexCoord2f( srcX * a, srcY * b + b );
            glVertex3i(( x ) * this->tileSizeX,
            ( y + 1 ) * this->tileSizeY, 0 );
           
            // dolny-prawy  vertex (corner)
            //   _|_
            //    |*
            glTexCoord2f( srcX * a + a, srcY * b + b );
            glVertex3i(( x + 1 ) * this->tileSizeX,
            ( y + 1 ) * this->tileSizeY, 0 );
           
            // Top-right vertex (corner)
            //   _|_*
            //    |
            glTexCoord2f( srcX * a + a, srcY * b );
            glVertex3i(( x + 1 ) * this->tileSizeX,
            ( y ) * this->tileSizeY, 0 );
           
            glEnd();
           
        }
    }
   
    //glLoadIdentity();
    //SDL_GL_SwapBuffers();
}
No i rysowanie tego kochanego prostokąta
C/C++
glColor3f( 1.0f, 1.0f, 1.0f );
glRecti( 100, 100, 200, 200 );

Idzie to mniej więcej tak:
1) włączam program
2) inicjalizuje sie opengl
3) ładuje plik graficzny z sprite
4) dalej jest pętla i w petli na początku rysuje mape a potem próbuje narysować ten prostokąt;
potem jeszcze rysuje napis  za pomocą freetype przykładem z nehe lekcja 43  przykład dla SDL
P-36520
DejaVu
» 2011-07-21 21:13:26
Spróbuj wyłączyć kanał alfa i GL_TEXTURE (czy coś podobnego) na czas rysowania prostokąta.
P-36521
« 1 » 2
  Strona 1 z 2 Następna strona