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

[OPENGL, SFML] Dlaczego program nie wyswietla prymitywu

Ostatnio zmodyfikowano 2025-07-10 18:43
Autor Wiadomość
wojownik266
Temat założony przez niniejszego użytkownika
» 2025-07-10 18:17:05
Ok. Pobawię się tym jeszcze i jak by co to się odezwę może jeszcze dzisiaj... Dzieki:)
P-182695
wojownik266
Temat założony przez niniejszego użytkownika
» 2025-07-10 18:43:43
Wziąłem pod uwagę Twoje wskazówki tzn. dodałem jeszcze jeden Vertex, pozmieniałem trochę zapis w blenderze np. zmieniłem oś Z z ujemnej na taką +Z, pogrupowałem wierzchołki, Dodałem parę funkcji Ogl'a i teraz program wczytuje w zasadzie "każdy" model z pliku. Możesz sobie fundnąć piwko... Dziękuję za pomoc. HeJ!!!

C/C++
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stdexcept>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

struct Objeto {
   
std::vector < float > vertices;
   
std::vector < int > 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" ) {
           
float 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; // Dla ignorowania indeksów tekstur i normalnych
           
iss >> v1 >> slash >> slash >> v2 >> slash >> slash >> v3;
           
           
objeto.faces.push_back( v1 - 1 ); // Pliki OBJ liczą od 1, tablice od 0
           
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( "mtl.obj" ); // Zastąp nazwą pliku
   
GLfloat angle = 0.1;
   
glEnable( GL_DEPTH_TEST );
   
glLoadIdentity();
   
   
while( window.isOpen() )
   
{
       
sf::Event event;
       
while( window.pollEvent( event ) )
       
{
           
if( event.type == sf::Event::Closed )
               
 window.close();
           
       
}
       
angle += 0.05;
       
       
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( 5 );
           
glScalef( 0.5, 0.5, 0.5 );
           
glRotated( angle, 0.1, 0.0, - 1.0 );
           
glBegin( GL_QUADS );
           
glColor3f( 1.0, 0.5, 1.0 );
           
glVertex3fv( & model.vertices[ i + 0 ] );
           
glVertex3fv( & model.vertices[ i + 1 ] );
           
glVertex3fv( & model.vertices[ i + 2 ] );
           
glVertex3fv( & model.vertices[ i + 3 ] );
           
glEnd();
           
       
}
       
       
window.display();
       
   
}
   
   
return EXIT_SUCCESS;
}
P-182697
1 « 2 »
Poprzednia strona Strona 2 z 2