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

[SFML OpenGl] Kamera nie chce się poruszać

Ostatnio zmodyfikowano 2025-08-15 07:05
Autor Wiadomość
wojownik266
Temat założony przez niniejszego użytkownika
[SFML OpenGl] Kamera nie chce się poruszać
» 2025-08-14 13:42:00
Dlaczego kamera nie chce się poruszać? Co źle napisałem? Proszę o pomoc..

C/C++
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <GL/glu.h>

GLfloat angle = 0.05;

void rysujScene()
{
   
//angle += 0.05;
    // glRotatef(angle,0.0,0.1,0.0);
   
glBegin( GL_QUADS );
   
glColor3f( 1.0, 0.5, 0.0 );
   
glVertex3f( - 1.0f, 0.0f, - 1.0f );
   
glVertex3f( 1.0f, 0.0f, - 1.0f );
   
glVertex3f( 1.0f, 0.0f, 1.0f );
   
glVertex3f( - 1.0f, 0.0f, 1.0f );
   
glEnd();
}
void rysujTlo()
{
   
   
glBegin( GL_QUADS );
   
glColor3f( 0.5, 0.5, 0.5 );
   
glVertex3f( - 10.0f, - 10.0f, - 1.0f );
   
glVertex3f( 10.0f, - 10.0f, - 1.0f );
   
glVertex3f( 10.0f, 10.0f, - 1.0f );
   
glVertex3f( - 10.0f, 10.0f, - 1.0f );
   
glEnd();
}
int main()
{
   
// Create the main window
   
sf::RenderWindow window( sf::VideoMode( { 800, 600 } ), "SFML window" );
   
GLfloat aspect = 800.0 / 600.0;
   
GLfloat cam_x = 0.0, cam_y = 1.0, cam_z = - 5.0;
   
glEnable( GL_DEPTH );
   
   
// Load a sprite to display
   
const sf::Texture texture( "tlo.jpg" );
   
sf::Sprite sprite( texture );
   
   
// Create a graphical text to display
    // const sf::Font font("arial.ttf");
    // sf::Text text(font, "Hello SFML", 50);
   
    // Load a music to play
    // sf::Music music("nice_music.ogg");
   
    // Play the music
    // music.play();
   
    // Start the game loop
   
while( window.isOpen() )
   
{
       
// Process events
       
while( const std::optional event = window.pollEvent() )
       
{
           
// Close window: exit
           
if( event->is < sf::Event::Closed >() )
               
 window.close();
           
       
}
       
       
       
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Key::Left ) )
       
{
           
cam_x--;
       
}
       
else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Key::Right ) )
       
{
           
cam_x++;
       
}
       
       
gluLookAt(
       
cam_x, cam_y, cam_z, // pozycja kamery
       
0.0, 0.0, 0.0, // punkt w ktory patrzy
       
0.0, 0.0, 1.0
       
);
       
glViewport( 0, 0, 800, 600 );
       
glMatrixMode( GL_PROJECTION );
       
glLoadIdentity();
       
// Ustawiamy perspektywê
       
gluPerspective( 45.0, aspect, 0.1, 100.0 );
       
// rysuj scenę
       
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
       
rysujTlo();
       
rysujScene();
       
       
//window.clear();
       
        // Draw the sprite
        //window.draw(sprite);
       
        // Draw the string
        //window.draw(text);
       
        // Update the window
       
window.display();
   
}
}
P-182878
wojownik266
Temat założony przez niniejszego użytkownika
» 2025-08-14 15:32:12
Problem rozwiązałem samodzielnie tworząc poniższy program...

C/C++
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <GL/glu.h>

void rysujPunkt()
{
   
glPointSize( 50 );
   
glBegin( GL_POINTS );
   
glColor3f( 1.0, 0.0, 0.5 );
   
glVertex3f( 0.0, 0.0, - 1.0 );
   
glEnd();
}
int main()
{
   
sf::RenderWindow window( sf::VideoMode( { 800, 600 } ), "TEST KAMERY" );
   
GLfloat camX = 0.0, camY = 0.0, camZ = - 1.0;
   
   
while( window.isOpen() )
   
{
       
while( const std::optional event = window.pollEvent() )
       
{
           
if( event->is < sf::Event::Closed >() )
           
{
               
window.close();
           
}
        }
       
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Key::Up ) )
       
{
           
camY += 0.01;
       
}
       
else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Key::Down ) )
       
{
           
camY -= 0.01;
       
}
       
else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Key::Left ) )
       
{
           
camX -= 0.01;
       
}
       
else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Key::Right ) )
       
{
           
camX += 0.01;
       
}
       
       
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
       
glEnable( GL_DEPTH );
       
glLoadIdentity();
       
gluLookAt( camX, camY, camZ, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 );
       
rysujPunkt();
       
       
       
//window.clear();
       
window.display();
   
}
   
return EXIT_SUCCESS;
}
P-182879
tBane
» 2025-08-15 07:05:39
Czyli trzeba było ustawiać zawsze macierz kamery po glClear() a przed rysowaniem. Dzięki :-)
P-182883
« 1 »
  Strona 1 z 1