SFML community forums

Help => Graphics => Topic started by: short_name on January 26, 2022, 12:54:52 am

Title: [SOLVED] OpenGL oddness
Post by: short_name on January 26, 2022, 12:54:52 am
The below code results in a greyscale grid of spheres rather than the ywllow spheres I expected to see. If I uncomment the lines:

 
  //sf::Text testtext;
  //testtext.setString("HELLO OPENGL");
 

Then the spheres turn yellow. but something is still off about the depth testing. The spheres on the left side of the window look correct but the ones on the right side are being rendered in the wrong order.

Anyone see what mistake(s) I am making?

#include <SFML/Graphics.hpp>
#include <math.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

int main(int argc, char **argv) {
  sf::RenderWindow window( sf::VideoMode(800,600,32), "SFML + OpenGL" );
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  glEnable(GL_DEPTH_TEST);
  GLfloat lightpos[] = { 55.0, 55.0, -55.0 };
  GLfloat lightcolor[] = { 1.0, 1.0, 0.0 };
  GLfloat ambcolor[] = { 0.0, 0.0, 1.0 };
  glEnable(GL_LIGHTING);
  glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambcolor);
  glEnable(GL_LIGHT0);
  glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
  glLightfv(GL_LIGHT0,GL_AMBIENT,lightcolor);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,lightcolor);
  glLightfv(GL_LIGHT0,GL_SPECULAR,lightcolor);

  //sf::Text testtext;
  //testtext.setString("HELLO OPENGL");

  while( window.isOpen() )
  {
    sf::Event event;
    while( window.pollEvent( event ) )
    {
      if( event.type==sf::Event::Closed )
        window.close();
    }
    window.setActive();

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50.0, 1.0, 1.0, 70.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt( 0.0, 1.0, 5.0,
               0.0, 1.0, 4.0,
               0.0, 1.0, 0.0);

    //draw the spheres
    glColor3f(0.4f, 0.4f, 0.4f);
    int h = 10;
    for( int i = -h ; i <= h ; i++ )
    {
      for( int j = -h ; j <= h ; j++ )
      {
        glPushMatrix();
        glTranslatef(i*.4,.2,j*.4);
        glutSolidSphere(.1,30,30);
        glPopMatrix();
      }
    }

    window.display();
  }
  return 1;
}

 
Title: Re: [SOLVED] OpenGL oddness
Post by: short_name on January 26, 2022, 02:09:55 am
Realized I shouldn't be mixing glut with sfml