Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL cube  (Read 3474 times)

0 Members and 1 Guest are viewing this topic.

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
OpenGL cube
« on: June 20, 2011, 06:30:06 pm »
i'm trying to draw a cube using openGL and then to view it from different angles. i did it using GLUT and it works well. But, using SFML, it doesn't look right because i can see 4 faces of the cube when i should be seeing only 3, and i can see 2 faces when i should be seeing only 2 :-s

here is the code:
Code: [Select]
#include <SFML/Graphics.hpp>
int main()
{
    sf::Window App(sf::VideoMode(600, 600, 32), "SFML OpenGL", sf::Style::Close);
    GLdouble h = 0.0d;
    GLdouble v = 0.0d;
    GLdouble l = 0.0d;
    while (App.IsOpened())
    {
        App.SetActive();

        glClearColor(0, 0, 0, 0);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glRotated(l,0.0d,0.0d,0.2d);
        glRotated(h,0.2d,0.0d,0.0d);
        glRotated(v,0.0d,0.2d,0.0d);
        glBegin(GL_QUADS);
        //back
        glColor3f(1.0f,0.0f,0.0f); //red
        glVertex3f(-0.2f,0.2f,0.2f); //top left
        glVertex3f(0.2f,0.2f,0.2f); //top right
        glVertex3f(0.2f,-0.2f,0.2f); //bottom right
        glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
        //bottom
        glColor3f(0.0f,1.0f,0.0f); //green
        glVertex3f(0.2f,-0.2f,0.2f); //back right
        glVertex3f(-0.2f,-0.2f,0.2f); //back left
        glVertex3f(-0.2f,-0.2f,-0.2f); //front left
        glVertex3f(0.2f,-0.2f,-0.2f); //front right
        //front
        glColor3f(0.0f,0.0f,1.0f); //blue
        glVertex3f(0.2f,-0.2f,-0.2f); //bottom right
        glVertex3f(-0.2f,-0.2f,-0.2f); //bottom left
        glVertex3f(-0.2f,0.2f,-0.2f); //top left
        glVertex3f(0.2f,0.2f,-0.2f); //top right
        //top
        glColor3f(1.0f,1.0f,0.0f); //yellow
        glVertex3f(0.2f,0.2f,-0.2f); //front right
        glVertex3f(-0.2f,0.2f,-0.2f); //front left
        glVertex3f(-0.2f,0.2f,0.2f); //back left
        glVertex3f(0.2f,0.2f,0.2f); //back right
        //left
        glColor3f(0.0f,1.0f,1.0f); //pink
        glVertex3f(-0.2f,-0.2f,-0.2f); //bottom front
        glVertex3f(-0.2f,-0.2f,0.2f); //bottom back
        glVertex3f(-0.2f,0.2f,0.2f); //top back
        glVertex3f(-0.2f,0.2f,-0.2f); //top front
        //right
        glColor3f(1.0f,0.0f,1.0f); //cyan
        glVertex3f(0.2f,-0.2f,-0.2f); //bottom front
        glVertex3f(0.2f,-0.2f,0.2f); //bottom back
        glVertex3f(0.2f,0.2f,0.2f); //top back
        glVertex3f(0.2f,0.2f,-0.2f); //top front
        glEnd();

        glFlush();

        App.Display();

        sf::Event Event;
        while (App.GetEvent(Event))
        {
            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::Right))
                v+=10.0d;
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
                v-=10.0d;
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
                h+=10.0d;
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
                h-=10.0d;
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageUp))
                l+=10.0d;
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageDown))
                l-=10.0d;
        }
    }
    return EXIT_SUCCESS;
}





am i using SFML in a wrong way ?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
OpenGL cube
« Reply #1 on: June 20, 2011, 06:51:55 pm »
Depth buffer is missing. See http://www.sfml-dev.org/tutorials/1.6/window-opengl.php for more information.
SFML / OS X developer

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
OpenGL cube
« Reply #2 on: June 20, 2011, 07:32:19 pm »
thanks, it works now.

 

anything