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

Author Topic: [SOLVED]OpenGL Colored Cube Render Issue SFML 2.3.2  (Read 4841 times)

0 Members and 2 Guests are viewing this topic.

kipbits

  • Newbie
  • *
  • Posts: 49
    • View Profile
[SOLVED]OpenGL Colored Cube Render Issue SFML 2.3.2
« on: August 13, 2016, 05:06:04 pm »
Hey all, I'm trying to get into some OpenGL with SFML, and am having some trouble with some example code for drawing a colored cube. I am still using 2.3.2 and was hoping to keep my current setup for a little while before updating to 2.4. I've read somewhere about some bugs with 2.3.2's OpenGL implementation. Is that an issue here or is there some dumb thing I'm missing in my code? Also I'm on mac; Could this be a mac problem?
here is my code:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

int main(int, char const**)
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML OpenGL");
     
    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);
   
    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    //glDepthFunc(GL_LESS);
   
    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluPerspective(90.f, 1.f, 1.f, 500.f);
    gluPerspective(70.f, 1.f, 1.f, 500.f);

    sf::Clock Clock;
   
    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            // Escape pressed: exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
           
            if (event.type == sf::Event::Resized)
                glViewport(0, 0, event.size.width, event.size.height);
        }
       
        // Clear screen
        window.clear();
       
        sf::Time myTime = Clock.getElapsedTime();
        float myftime = myTime.asSeconds();

        window.setActive();
       
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);
        glRotatef(myftime * 50, 1.f, 0.f, 0.f);
        glRotatef(myftime * 30, 0.f, 1.f, 0.f);
        glRotatef(myftime * 90, 0.f, 0.f, 1.f);
       
        glBegin(GL_QUADS);
       
        glColor3f(1.f, 0.f, 0.f);//red
        glVertex3f(-50.f, -50.f, -50.f);
        glVertex3f(-50.f,  50.f, -50.f);
        glVertex3f( 50.f,  50.f, -50.f);
        glVertex3f( 50.f, -50.f, -50.f);
       
        //glColor3f(1.f, 0.f, 0.f);//red
        glColor3f(0.f, 1.f, 1.f);//cyan
        glVertex3f(-50.f, -50.f, 50.f);
        glVertex3f(-50.f,  50.f, 50.f);
        glVertex3f( 50.f,  50.f, 50.f);
        glVertex3f( 50.f, -50.f, 50.f);
       
        glColor3f(0.f, 1.f, 0.f);//green
        glVertex3f(-50.f, -50.f, -50.f);
        glVertex3f(-50.f,  50.f, -50.f);
        glVertex3f(-50.f,  50.f,  50.f);
        glVertex3f(-50.f, -50.f,  50.f);
       
        //glColor3f(0.f, 1.f, 0.f);//green
        glColor3f(1.f, 0.f, 1.f);//magenta
        glVertex3f(50.f, -50.f, -50.f);
        glVertex3f(50.f,  50.f, -50.f);
        glVertex3f(50.f,  50.f,  50.f);
        glVertex3f(50.f, -50.f,  50.f);
       
        glColor3f(0.f, 0.f, 1.f);//blue
        glVertex3f(-50.f, -50.f,  50.f);
        glVertex3f(-50.f, -50.f, -50.f);
        glVertex3f( 50.f, -50.f, -50.f);
        glVertex3f( 50.f, -50.f,  50.f);
       
        //glColor3f(0.f, 0.f, 1.f);//blue
        glColor3f(1.f, 1.f, 0.f);//yellow
        glVertex3f(-50.f, 50.f,  50.f);
        glVertex3f(-50.f, 50.f, -50.f);
        glVertex3f( 50.f, 50.f, -50.f);
        glVertex3f( 50.f, 50.f,  50.f);
       
        glEnd();
       
        // Update the window
        window.display();
    }
   
    return EXIT_SUCCESS;
}
 

and here is a gif:

« Last Edit: August 26, 2021, 08:23:03 pm by kipbits »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL Colored Cube Render Issue SFML 2.3.2[Mac?]
« Reply #1 on: August 13, 2016, 05:18:03 pm »
Check the window's OpenGL context settings (window.getSettings()), chances are that you have no Z-Buffer.
Laurent Gomila - SFML developer

kipbits

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: OpenGL Colored Cube Render Issue SFML 2.3.2[Mac?]
« Reply #2 on: August 13, 2016, 06:12:09 pm »
Fixed: Thanks!
I was mix matching sample code from different version numbers.
replace
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML OpenGL");
 
with   
sf::ContextSettings Settings;
Settings.depthBits = 24; // Request a 24-bit depth buffer
Settings.stencilBits = 8;  // Request a 8 bits stencil buffer
Settings.antialiasingLevel = 2;  // Request 2 levels of antialiasing
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML OpenGL", sf::Style::Close, Settings);
 

pic:


« Last Edit: August 26, 2021, 08:17:58 pm by kipbits »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: [SOLVED]OpenGL Colored Cube Render Issue SFML 2.3.2
« Reply #3 on: August 14, 2016, 09:56:08 am »
Also use sf::Window if you are not going to mix SFML rendering and raw opengl

 

anything