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ę: void LoadGLTextures( sf::Image * img ) { glGenTextures( 1, & texture[ 0 ] ); glBindTexture( GL_TEXTURE_2D, texture[ 0 ] ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 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: 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 |
|
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): #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; 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 ) { glGenTextures( 1, & texture[ najTex ] ); glBindTexture( GL_TEXTURE_2D, texture[ 0 ] ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 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 ] ); 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() { glEnable( GL_TEXTURE_2D ); glClearColor( 0.0f, 0.0f, 1.0f, 0.0f ); glClearDepth( 1.0 ); glDepthFunc( GL_LESS ); glEnable( GL_DEPTH_TEST ); glShadeModel( GL_SMOOTH ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 90.f, 1.f, 1.f, 500.f ); glMatrixMode( GL_MODELVIEW ); }
int main() { sf::WindowSettings Settings; Settings.DepthBits = 24; Settings.StencilBits = 8; Settings.AntialiasingLevel = 3; 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; 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 ); cube.draw(); app.Display(); } return 0; }
|
|
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. |
|
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. |
|
|
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. |
|
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). |
|
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 >> |
|
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. |
|
« 1 » 2 |