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

Author Topic: OpenGL in a SFML with Graphics module problems  (Read 1347 times)

0 Members and 1 Guest are viewing this topic.

shadowmoses

  • Newbie
  • *
  • Posts: 1
    • View Profile
OpenGL in a SFML with Graphics module problems
« on: April 02, 2014, 12:49:55 am »
As the title says I am having problems with using straight OpenGL with the graphics module (following wiki info). All of my OpenGL code works and renders properly however, I am trying to use the RenderWindow to display text over the 3D area using SFML.  Not sure why it is not working.  Any help as to why SFML is not rendering text would greatly be appreciated.

Here is a snippet of what I am trying to do.

main:
int main()
{
   // Request a 32-bits depth buffer when creating the window
   sf::ContextSettings contextSettings;
   contextSettings.depthBits = 32;
   contextSettings.stencilBits = 32;
   contextSettings.antialiasingLevel = 16;
   // create the window
   sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, contextSettings);
   window.setVerticalSyncEnabled(true);

   GLint msEnabled;
   glGetIntegerv(GL_SAMPLE_BUFFERS,&msEnabled);
   GLint msPixels;
   glGetIntegerv(GL_SAMPLES, &msPixels);
   printf ("AA enabled: %u\n", msEnabled);
   printf ("AA: %u\n", msPixels);

   // Create some text to draw on top of our OpenGL object
   sf::Font font;
   if (!font.loadFromFile("font/sansation.ttf"))
      return EXIT_FAILURE;
   sf::Text text("SFML / OpenGL demo", font);
   text.setColor(sf::Color(255, 255, 255, 170));
   text.setPosition(250.f, 450.f);

   // load resources, initialize the OpenGL states, ...
   glewInit();
   dumpInfo ();
   init ();
   glEnable(GL_DEPTH_TEST);

   torus = new Model(&shader,"models/torus.obj",  "models/");
   cube = new Model(&shader,"models/cube.obj",  "models/");
   sphere = new Model(&shader,"models/monkeysphere.obj", "models/");
   plane = new Model(&shader,"models/plane.obj",  "models/");
   cylinder = new Model( &shader, "models/cylinder.obj", "models/");
   mesh = sphere;
   gun = new Model( &shader,"models/m16_1.obj", "models/");
   muzzleCylinder = new Model(&shader, "models/cylinder.obj", "models/");

   // run the main loop
   bool running = true;
   while (running)
   {
      // handle events
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
         {
            // end the program
            running = false;
         }
         else if (event.type == sf::Event::Resized)
         {
            // adjust the viewport when the window is resized
            glViewport(0, 0, event.size.width, event.size.height);
         }
      }
     
      // clear the buffers done in display function
      display();
      // draw...
      window.pushGLStates();
      window.draw(text);
      window.popGLStates();
      // end the current frame (internally swaps the front and back buffers)
      window.display();
   }
   return 0;
}

display():
void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   //camera->OnRender();

   //view = glm::lookAt(camera->GetPos(), camera->GetLookAtPoint(), camera->GetUp());
   view = glm::lookAt(glm::vec3(0.0f, 0.0f, 20.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

   rotation += 0.5f; // Update rotation angle if rotation is enabled.

   glm::vec4 lightPos = glm::rotate(rotation,0.0f, 0.0f, 1.0f) * lightPosition;

   shader.Activate(); // Bind shader.
   shader.SetUniform("lightPosition", view*lightPos);
   shader.SetUniform("lightDiffuse", glm::vec4(1.0, 1.0, 1.0, 1.0));
   shader.SetUniform("lightSpecular", glm::vec4(1.0, 1.0, 1.0, 1.0));
   shader.SetUniform("lightAmbient", glm::vec4(1.0, 1.0, 1.0, 1.0));

   if(spotLightEnabled)
   {
      if(gunFlashLightEnabled)
      {
         shader.SetUniform("spotPosition", glm::vec4(1.2f, -1.0f, -6.5f,1.f));
         shader.SetUniform("spotDirection", glm::vec4(0.f, 0.f, -1.f,0.f));
      }
      else
      {
         shader.SetUniform("spotPosition", view*spotPosition);
         shader.SetUniform("spotDirection", view*spotDirection);
      }
      shader.SetUniform("spotColor", glm::vec3(1.f, 1.f, 1.f));
      shader.SetUniform("spotOn", 1);
      shader.SetUniform("spotConeAngle", 15.0f);
      shader.SetUniform("spotConeCosine", float(cos(15.f*3.1415f/180.f)));
      shader.SetUniform("spotLinearAtt", 0.01f);
   }
   else
      shader.SetUniform("spotOn", 0);



   bool useMat = false;

   mesh->render(view * model,projection, true); // Render current active model.
   gun->setOverrideDiffuseMaterial( glm::vec4(.3, 0.3, 0.3, 1.0));
   gun->setOverrideAmbientMaterial(  glm::vec4(0.0, 0.0, 0.0, 1.0));
   plane->setOverrideSpecularMaterial( glm::vec4(.70, 0.70, 0.70, 1.0));

   gun->setOverrideSpecularShininessMaterial( 40.0f);
   gun->setOverrideEmissiveMaterial(  glm::vec4(0.0, 0.0, 0.0, 1.0));
   gun->render(glm::translate(1.0f,-1.0f,-2.0f)* glm::scale(.05f,.05f,.05f)*glm::rotate(-90.0f,0.0f,1.0f,0.0f) , projection, false);
   muzzleCylinder->render(glm::translate(1.2f, -1.0f, -6.5f)  * glm::rotate(90.0f,1.f,0.f,0.f)* glm::scale(.2f,.2f,.2f), projection, false);

   cylinder->setOverrideDiffuseMaterial( glm::vec4(1.0, 0.0, 0.0, 1.0));
   cylinder->setOverrideAmbientMaterial(  glm::vec4(0.0, 0.0, 0.0, 1.0));
   cylinder->setOverrideSpecularMaterial( glm::vec4(1.0, 1.0, 1.0, 1.0));
   cylinder->setOverrideSpecularShininessMaterial( 90.0f);
   cylinder->setOverrideEmissiveMaterial(  glm::vec4(0.0, 0.0, 0.0, 1.0));
   cylinder->render(view*glm::translate(0.0f,5.0f,0.0f)*glm::rotate(180.0f,1.0f,0.0f,0.0f), projection, useMat);

   plane->setOverrideDiffuseMaterial( glm::vec4(1.0, 0.0, 0.0, 1.0));
   plane->setOverrideAmbientMaterial(  glm::vec4(0.2 , 0.0, 0.0, 1.0));
   plane->setOverrideSpecularMaterial( glm::vec4(1.0, 1.0, 1.0, 1.0));
   plane->setOverrideSpecularShininessMaterial( 90.0f);
   plane->setOverrideEmissiveMaterial(  glm::vec4(0.0, 0.0, 0.0, 1.0));
   plane->render(view*glm::translate(0.0f,-2.0f,0.0f)*glm::scale(20.0f,20.0f,20.0f), projection, useMat);

   mesh->setOverrideEmissiveMaterial(  glm::vec4(1.0, 1.0, 1.0, 1.0));
   mesh->render(view * glm::translate(lightPos.x,lightPos.y, lightPos.z)*glm::scale(.1f,.1f,.1f), projection, false);

   //bot->render(view  * glm::rotate(180.f, 0.f,1.f,0.f)* glm::translate(-3.f, -1.f, 0.f), projection, true);

   checkError ("display");
}