View Full Version : SFML/OpenGL draw nothing

reDo reDo
07-26-2011, 03:52 AM
Hi, I am absolute beginner in OpenGL and 3D and I am trying to write basic program from first article NeHe tutorials
,but I am using SFML for window and other stuff, but it doesn't work, please can somebody check my code? Thanks a lot for any help :(

#include <SFML/Window.hpp>

int InitGL(void) // All Setup For OpenGL Goes Here
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return 1; // Initialization Went OK

int DrawGLScene(void) // Here's Where We Do All The Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad
return 1; // Everything Went OK

int main()
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");


// Start game loop
while (App.IsOpened())
// Process events
sf::Event Event;
while (App.GetEvent(Event))
// Close window : exit
if (Event.Type == sf::Event::Closed)

// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) &amp;&amp; (Event.Key.Code == sf::Key::Escape))

// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);


return 0;

07-26-2011, 04:30 AM
Three things,
1) Don't modify the depth func just enable depth test in other words comment the 2 lines


2) You have not setup your projection matrix usually u would add in a resize handler like this,

void OnResize(int nw, int nh) {
glViewport(0, 0, nw, nh);

//call it from main in the resize event like this
if (Event.Type == sf::Event::Resized)
OnResize(Event.Size.Width, Event.Size.Height);

3) Just move the camera 5 units on Z axis only and see if u get something. Then later change it to whatever position u want.

int DrawGLScene(void)
///rest of draw function

See if this helps. Note that I have never used SFML but this is how the GL part should be structured.

reDo reDo
07-26-2011, 07:28 AM
Thanks, I try learn 2D technique in OpenGL rather first :D .