wojownik266 Temat założony przez niniejszego użytkownika |
[OPENGL, SFML] Dlaczego program nie wyswietla prymitywu » 2025-07-10 13:41:17 Dlaczego punkty nie chcą się wyświetlić, choć program ładnie się kompiluje i uruchamia. Gdzie popełniłem błąd? #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> #include <stdexcept> #include <SFML/Graphics.hpp> #include <SFML/OpenGL.hpp>
struct Objeto { std::vector < GLfloat > vertices; std::vector < GLint > faces; };
Objeto load_obj( const std::string & filename ) { Objeto objeto; std::ifstream file( filename ); if( !file.is_open() ) { throw std::runtime_error( "Nie można otworzyć pliku: " + filename ); } std::string line; while( std::getline( file, line ) ) { std::istringstream iss( line ); std::string type; iss >> type; if( type == "v" ) { GLfloat x, y, z; iss >> x >> y >> z; objeto.vertices.push_back( x ); objeto.vertices.push_back( y ); objeto.vertices.push_back( z ); } else if( type == "f" ) { int v1, v2, v3; char slash; iss >> v1 >> slash >> slash >> v2 >> slash >> slash >> v3; objeto.faces.push_back( v1 - 1 ); objeto.faces.push_back( v2 - 1 ); objeto.faces.push_back( v3 - 1 ); } } file.close(); return objeto; }
int main() { sf::Window window( sf::VideoMode( 800, 600 ), "Obiekt z Blendera" ); Objeto model = load_obj( "kwadrat.obj" ); while( window.isOpen() ) { sf::Event event; while( window.pollEvent( event ) ) { if( event.type == sf::Event::Closed ) window.close(); } glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); std::cout << "Wierzchołki:\n"; for( size_t i = 0; i < model.vertices.size(); i += 3 ) { std::cout << "v: " << model.vertices[ i ] << ", " << model.vertices[ i + 1 ] << ", " << model.vertices[ i + 2 ] << std::endl; glTranslated( 0.0, 0.0, - 1.0 ); glPointSize( 50 ); glBegin( GL_POINTS ); glColor3f( 1.0, 1.0, 1.0 ); glVertex3fv( & model.vertices[ i ] ); glFlush(); } window.display(); } return EXIT_SUCCESS; }
|
|
tBane |
» 2025-07-10 16:10:16 Dorzuć link do modelu to może pomogę :-) |
|
wojownik266 Temat założony przez niniejszego użytkownika |
» 2025-07-10 16:33:10 Do tego programu uzyłem modelu OBJ... najprostszego z Blendera (Plane). Zawartość pliku poniżej: # Blender 4.0.2 # www.blender.org mtllib kwadrat.mtl o Prostok ą t v - 2.960119 0.000000 2.960119 v 2.960119 0.000000 2.960119 v - 2.960119 0.000000 - 2.960119 v 2.960119 0.000000 - 2.960119 vn - 0.0000 1.0000 - 0.0000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 1.000000 1.000000 vt 0.000000 1.000000 s 0 f 1 / 1 / 1 2 / 2 / 1 4 / 3 / 1 3 / 4 / 1
|
|
tBane |
» 2025-07-10 16:52:56 Zapomniałeś dodać pozostałe dwie współrzędne (y,z). Poprawiony fragment kodu:glVertex3fv( & model.vertices[ i ] ); glVertex3fv( & model.vertices[ i + 1 ] ); glVertex3fv( & model.vertices[ i + 2 ] ); oraz nie dodałeś glEnable( GL_DEPTH_TEST ); Cały kod:#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> #include <stdexcept> #include <SFML/Graphics.hpp> #include <SFML/OpenGL.hpp>
struct Objeto { std::vector < GLfloat > vertices; std::vector < GLint > faces; };
Objeto load_obj( const std::string & filename ) { Objeto objeto; std::ifstream file( filename ); if( !file.is_open() ) { throw std::runtime_error( "Nie można otworzyć pliku: " + filename ); } std::string line; while( std::getline( file, line ) ) { std::istringstream iss( line ); std::string type; iss >> type; if( type == "v" ) { GLfloat x, y, z; iss >> x >> y >> z; objeto.vertices.push_back( x ); objeto.vertices.push_back( y ); objeto.vertices.push_back( z ); } else if( type == "f" ) { int v1, v2, v3; char slash; iss >> v1 >> slash >> slash >> v2 >> slash >> slash >> v3; objeto.faces.push_back( v1 - 1 ); objeto.faces.push_back( v2 - 1 ); objeto.faces.push_back( v3 - 1 ); } } file.close(); return objeto; }
int main() { sf::Window window( sf::VideoMode( 800, 600 ), "Obiekt z Blendera" ); Objeto model = load_obj( "mdl.obj" ); glEnable( GL_DEPTH_TEST ); glLoadIdentity(); while( window.isOpen() ) { sf::Event event; while( window.pollEvent( event ) ) { if( event.type == sf::Event::Closed ) window.close(); } glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); std::cout << "Wierzchołki:\n"; for( size_t i = 0; i < model.vertices.size(); i += 3 ) { std::cout << "v: " << model.vertices[ i ] << ", " << model.vertices[ i + 1 ] << ", " << model.vertices[ i + 2 ] << std::endl; glPointSize( 50 ); glBegin( GL_POINTS ); glColor3f( 1.0, 1.0, 1.0 ); glVertex3fv( & model.vertices[ i ] ); glVertex3fv( & model.vertices[ i + 1 ] ); glVertex3fv( & model.vertices[ i + 2 ] ); glEnd(); } window.display(); } return EXIT_SUCCESS; }
|
|
wojownik266 Temat założony przez niniejszego użytkownika |
» 2025-07-10 17:46:03 Po korekcie jest trochę lepiej ale i tak nie wyświetla mi objektu... Możesz mi podrzucić plik obj ten którego użyłeś? Chyba że użyłeś tego który Ci podesłałem. Tak czy tak dziękuję! |
|
tBane |
» 2025-07-10 17:49:27 Zmieniłem tylko ą na a w słowie prostokąt mdl.obj # Blender 4.0.2 # www.blender.org mtllib kwadrat.mtl o Prostokat v - 2.960119 0.000000 2.960119 v 2.960119 0.000000 2.960119 v - 2.960119 0.000000 - 2.960119 v 2.960119 0.000000 - 2.960119 vn - 0.0000 1.0000 - 0.0000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 1.000000 1.000000 vt 0.000000 1.000000 s 0 f 1 / 1 / 1 2 / 2 / 1 4 / 3 / 1 3 / 4 / 1
|
|
wojownik266 Temat założony przez niniejszego użytkownika |
» 2025-07-10 18:12:06 Jeśli zmienię na: glBegin( GL_POINTS ) to wyświetla jeden punkt. A jeśli zmienię na inne GL_Quads lub jeszcze inny to cisza i czarny ekran. Myślę że może być jeszcze problem z Blenderem bo ma tam jeszcze jakieś dziwne opcje przy eksporcie do .OBJ... Dziękuję za poświęcony czas i życzę miłej reszty dnia! |
|
tBane |
» 2025-07-10 18:13:39 żeby quad zadziałał musisz podać 4 x {x,y,z} |
|
« 1 » 2 |