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

[OpenGL, SFML 1.6] Teksturowanie sześcianu - niewyraźne ściany

Ostatnio zmodyfikowano 2013-06-06 15:00
Autor Wiadomość
anonim
Temat założony przez niniejszego użytkownika
[OpenGL, SFML 1.6] Teksturowanie sześcianu - niewyraźne ściany
» 2013-04-27 23:05:19
Cześć
Męczę się z tym już dosyć długo, robiłem to na podstawie lekcji nr.6 z NEHE, program w skrócie wygląda tak:
Mam tablicę:
std::vector < GLuint > texture( 0 );
 //są to indeksy tekstur

Tak ładuje do niej teksturę:
C/C++
void LoadGLTextures( sf::Image * img ) {
    // Create Texture
    glGenTextures( 1, & texture[ 0 ] );
    glBindTexture( GL_TEXTURE_2D, texture[ 0 ] ); // 2d texture (x and y size)
   
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // scale linearly when image bigger than texture
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // scale linearly when image smalled than texture
   
    // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
    // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
    glTexImage2D( GL_TEXTURE_2D, 0, 3, img->GetWidth(), img->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, img->GetPixelsPtr() );
}
w funkcji rysującej sześcian używam:
C/C++
glBindTexture( GL_TEXTURE_2D, texture[ index ] );

Następnie w pętli głównej wywołuję initGl(), LoadGlTextures(sf::Image * img), i rysuje
Niestety zamiast tekstury mam różnokolorowe paski biegnące od lewego górnego narożnika do prawego dolnego narożnika
P-81322
anonim
Temat założony przez niniejszego użytkownika
» 2013-04-27 23:17:37
Problem był spowodowany brakiem dodania kanału ALFA dla tekstur (foramt .png)
Teraz jednak tekstury nie są dobrze odwzorowane (są źle nałożone - rozmyte, lub zniekształcone, nie tak jak w tutorialu NEHE)

Nie wiem gdzie jest błąd, wrzucam cały kod po przeróbkach (jest to nie dokończona klasa reprezentująca sześcian, na razie rysuje tylko jedną ścianę, żeby łatwiej było zlokalizować błąd):

C/C++
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <SFML/Window.hpp>

std::vector < GLuint > texture( 0 );
int najTex = - 1;

class Cube
{
public:
    sf::Image tex;
    GLuint index; //index w tablicy tekstur
    sf::Vector3f center;
    double size;
    sf::Vector3f tab[ 8 ];
    Cube();
    Cube( sf::Image img, sf::Vector3f ce, double d );
    void draw();
    void draw( sf::Vector3f col );
    void drawEdges();
    void move( sf::Vector3f vec );
    ~Cube();
};

Cube::~Cube() { }
Cube::Cube() {
    texture.push_back( najTex + 1 );
    najTex++;
    index = najTex;
}

Cube::Cube( sf::Image img, sf::Vector3f ce, double d )
    : tex( img )
    , center( ce )
    , size( d )
{
    texture.push_back( najTex + 1 );
    najTex++;
    index = najTex;
   
    double dl = d / 2;
    tab[ 0 ] = sf::Vector3f( ce.x - dl, ce.y + dl, ce.z - dl );
    tab[ 1 ] = sf::Vector3f( ce.x + dl, ce.y + dl, ce.z - dl );
    tab[ 2 ] = sf::Vector3f( ce.x + dl, ce.y - dl, ce.z - dl );
    tab[ 3 ] = sf::Vector3f( ce.x - dl, ce.y - dl, ce.z - dl );
    tab[ 4 ] = sf::Vector3f( ce.x - dl, ce.y + dl, ce.z + dl );
    tab[ 5 ] = sf::Vector3f( ce.x + dl, ce.y + dl, ce.z + dl );
    tab[ 6 ] = sf::Vector3f( ce.x + dl, ce.y - dl, ce.z + dl );
    tab[ 7 ] = sf::Vector3f( ce.x - dl, ce.y - dl, ce.z + dl );
    std::cout << index;
    std::cout << "Tworze szescian!!!\n";
}

void glTab( sf::Vector3f v )
{
    glVertex3f( v.x, v.y, v.z );
}

void glTexTab( sf::Vector3f v )
{
    glTexCoord2d( v.x, v.y );
}

void LoadGLTexture( sf::Image * img, int x, int y ) {
    // Create Texture
    glGenTextures( 1, & texture[ najTex ] );
    glBindTexture( GL_TEXTURE_2D, texture[ 0 ] ); // 2d texture (x and y size)
   
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // scale linearly when image bigger than texture
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // scale linearly when image smalled than texture
   
    // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
    // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
    glTexImage2D( GL_TEXTURE_2D, 0, 4, img->GetWidth(), img->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img->GetPixelsPtr() );
}

void Cube::draw()
{
   
    glBindTexture( GL_TEXTURE_2D, texture[ 0 ] );
   
    glBegin( GL_QUADS );
   
    glTexCoord2f( 0.0, 0.0 );
    glTab( tab[ 0 ] );
    glTexCoord2f( 1.0, 0.0 );
    glTab( tab[ 1 ] );
    glTexCoord2f( 0.0, 1.0 );
    glTab( tab[ 2 ] );
    glTexCoord2f( 1.0, 1.0 );
    glTab( tab[ 3 ] );
   
    /*glTab(tab[0]);
        glTab(tab[4]);
        glTab(tab[5]);
        glTab(tab[1]);
   
        glTab(tab[1]);
        glTab(tab[5]);
        glTab(tab[6]);
        glTab(tab[2]);
   
        glTab(tab[2]);
        glTab(tab[6]);
        glTab(tab[7]);
        glTab(tab[3]);
   
        glTab(tab[3]);
        glTab(tab[7]);
        glTab(tab[4]);
        glTab(tab[0]);
   
        glTab(tab[4]);
        glTab(tab[5]);
        glTab(tab[6]);
        glTab(tab[7]);*/
    glEnd();
}

void Cube::move( sf::Vector3f vec )
{
    center.x += vec.x;
    center.y += vec.y;
    center.z += vec.z;
    for( int i = 0; i < 8; i++ )
    {
        tab[ i ].x += vec.x;
        tab[ i ].y += vec.y;
        tab[ i ].z += vec.z;
    }
}

void Cube::drawEdges()
{
    glDisable( GL_TEXTURE_2D );
    glLineWidth( 3.0 );
    glBegin( GL_LINES );
    glTab( tab[ 0 ] );
    glTab( tab[ 1 ] );
   
    glTab( tab[ 1 ] );
    glTab( tab[ 2 ] );
   
    glTab( tab[ 2 ] );
    glTab( tab[ 3 ] );
   
    glTab( tab[ 3 ] );
    glTab( tab[ 0 ] );
   
    glTab( tab[ 4 ] );
    glTab( tab[ 5 ] );
   
    glTab( tab[ 5 ] );
    glTab( tab[ 6 ] );
   
    glTab( tab[ 6 ] );
    glTab( tab[ 7 ] );
   
    glTab( tab[ 7 ] );
    glTab( tab[ 4 ] );
   
    glTab( tab[ 0 ] );
    glTab( tab[ 4 ] );
   
    glTab( tab[ 1 ] );
    glTab( tab[ 5 ] );
   
    glTab( tab[ 2 ] );
    glTab( tab[ 6 ] );
   
    glTab( tab[ 3 ] );
    glTab( tab[ 7 ] );
    glEnd();
}

void initGL() // We call this right after our OpenGL window is created.
{
    // Load The Texture(s)
    glEnable( GL_TEXTURE_2D ); // Enable Texture Mapping
    glClearColor( 0.0f, 0.0f, 1.0f, 0.0f ); // Clear The Background Color To Blue
    glClearDepth( 1.0 ); // Enables Clearing Of The Depth Buffer
    glDepthFunc( GL_LESS ); // The Type Of Depth Test To Do
    glEnable( GL_DEPTH_TEST ); // Enables Depth Testing
    glShadeModel( GL_SMOOTH ); // Enables Smooth Color Shading
   
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity(); // Reset The Projection Matrix
   
    gluPerspective( 90.f, 1.f, 1.f, 500.f );
   
    glMatrixMode( GL_MODELVIEW );
}

int main()
{
    sf::WindowSettings Settings;
    Settings.DepthBits = 24; // Request a 24 bits depth buffer
    Settings.StencilBits = 8; // Request a 8 bits stencil buffer
    Settings.AntialiasingLevel = 3; // Request 3 levels of antialiasing
    sf::Window app( sf::VideoMode( 800, 800, 32 ), "SFML OpenGL", sf::Style::Close, Settings );
    app.SetFramerateLimit( 60 );
   
    sf::Image imgExample;
    imgExample.LoadFromFile( "Monster.png" );
   
    Cube cube( imgExample, sf::Vector3f( 0.0, 0.0, 0.0 ), 100 );
    LoadGLTexture( & cube.tex, cube.size, cube.size );
   
    sf::Clock Clock; // Rotacja
    initGL();
    while( app.IsOpened() )
    {
        sf::Event event;
        while( app.GetEvent( event ) )
        {
            if( event.Type == sf::Event::Resized )
                 glViewport( 0, 0, event.Size.Width, event.Size.Height );
           
            if( event.Type == sf::Event::Closed )
                 app.Close();
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Escape )
                 app.Close();
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Add )
                 cube = Cube( cube.tex, cube.center, cube.size * 11 / 10 );
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Subtract )
                 cube = Cube( cube.tex, cube.center, cube.size * 9 / 10 );
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::A )
                 cube.move( sf::Vector3f( - 5, 0, 0 ) );
           
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::D )
                 cube.move( sf::Vector3f( + 5, 0, 0 ) );
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::W )
                 cube.move( sf::Vector3f( 0, + 5, 0 ) );
           
            if( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::S )
                 cube.move( sf::Vector3f( 0, - 5, 0 ) );
           
            if( event.Type == sf::Event::MouseButtonPressed && event.MouseButton.Button == sf::Mouse::Middle )
                 app.Close();
           
        }
       
        app.SetActive();
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
       
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
       
       
        glTranslatef( 0.f, 0.f, - 200.f );
        glRotatef( Clock.GetElapsedTime() * 2, 1.f, 0.f, 0.f );
        glRotatef( Clock.GetElapsedTime() * 2, 0.f, 1.f, 0.f );
        glRotatef( Clock.GetElapsedTime() * 2, 0.f, 0.f, 1.f );
        //glColor3f( 160 / 255, 0.5, 0.4);
        cube.draw();
        //glColor3f( 0.0,1,0.0);
        //cube.drawEdges();
        app.Display();
    }
    return 0;
}
P-81323
DejaVu
» 2013-04-27 23:30:00
Jeżeli tekstury się 'źle' nakładają to zapewne bufor głębi jest źle tworzony bądź jest wyłączony.

Druga opcja: wierzchołki nie są układane zgodnie z ruchem wskazówek zegara (czy tam w przeciwnym kierunku).

Trzecia opcja: ściany sześcianu zostały źle poskładane i nie tworzą one sześcianu.

Tekstury rozmazane - zazwyczaj jest to kwestia antyaliasingu, bądź jakości tekstury w stosunku do rozmiaru obiektu.
P-81324
m4tx
» 2013-06-06 11:59:21
Tekstury rozmazane - zazwyczaj jest to kwestia antyaliasingu, bądź jakości tekstury w stosunku do rozmiaru obiektu.
Prędzej filtrowania tekstur aniżeli antyaliasingu. Zresztą, tekstury rozmazane to nic nowego - praktycznie w każdej grze mamy filtrowanie tekstur, więc i w każdej tekstury te się rozmywają. Reszta tak jak powiedział DejaVu:
jakości tekstury w stosunku do rozmiaru obiektu.
P-85038
wojownik266
» 2013-06-06 13:23:22
Do tego co zostało powiedziane powyżej dodam jeszcze to że tekstury muszą być potęgą dwójki np 64x64, 256x256. Jeżeli nie są to też może być przyczyną tego że tekstury są źle nałożone. Np. po przekatnej.
P-85039
Elaine
» 2013-06-06 13:54:46
tekstury muszą być potęgą dwójki np 64x64, 256x256
Mamy 2013, karty w pełni wspierające NPOT textures na rynku pojawiły się w 2004 (NV40) i 2005 (R520).
P-85042
wojownik266
» 2013-06-06 14:11:55
Ale bibliotekę OpenGL mamy z zamierzchłej epoki i musi być tak jak pisze w 6 lekcji NeHe czyli tak jak napisałem powyzej. A po zatym karty nic tu do rzeczy nie mają. << Removed by DejaVu - raczej o Tobie to można powiedzieć niż o nim. Jak chcesz się tak zachowywać to nie zabieraj głosu >>
P-85044
DejaVu
» 2013-06-06 14:49:29
OpenGL mamy z zamierzchłej epoki i musi być tak jak pisze w 6 lekcji NeHe
Bo kurs NeHe jest wyznacznikiem wszelkich norm i standardów... zanim zaczniesz krytykować i obrażać innych, lepiej spójrz na swoją wiedzę merytoryczną. Sam dłubałem X-lat temu w OpenGL-u i jakoś nie miałem problemów z teksturami, które miały nietypowe rozmiary.
P-85045
« 1 » 2
  Strona 1 z 2 Następna strona