1

**Graphics / [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:

and here is a gif:

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;

}

#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: