c++openglglfwopengl-compat

Adjust OpenGL view


I am trying to draw some particles using GLFW and OpenGL. However, the output of the scene is weird. I have 8 positions that they are a coordinates of a cubic in space:

-0.5 -0.5 -0.5  \\ position of particle 1
-0.5 -0.5  0.5  \\ position of particle 2
-0.5  0.5 -0.5  \\ position of particle 3
-0.5  0.5  0.5  \\ position of particle 4
 0.5 -0.5 -0.5  \\ position of particle 5
 0.5 -0.5  0.5  \\ position of particle 6
 0.5  0.5 -0.5  \\ position of particle 7
 0.5  0.5  0.5  \\ position of particle 8

and here is my OpenGL code for drawing them:

\\ First initialising:
void initializationGL()
{
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_NORMALIZE);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_CULL_FACE);
  glEnable(GL_DEPTH_TEST);
}

\\ Code for resizing the window:
void resizeGL(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-50.0, 50.0, -50.0, 50.0, -100.0, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

\\ Code for drawing:
void PaintGL()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glLoadIdentity();
  for (int i=0; i<env.Particles.size(); i++){
    glTranslatef(Particles[i].X(0), Particles.X(1), Particles[i].X(2));  \\ X is an attribute of Particle that store poistion of that particle in space (X(0)=x, X(1)=y, X(2)=z)
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glLineWidth(2.0);
    GLUquadric *quad;
    quad = gluNewQuadric();
    gluSphere(quad, 1, 100, 20);
  }
  glPopMatrix();
}

Here is the code for windowing and update the scene:

int main(int argc, char **argv)
{
  GLFWwindow* window;

   if (!glfwInit())
   {
    exit(EXIT_FAILURE);
   }
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

  window = glfwCreateWindow(800, 800, "PBsimulation", NULL, NULL);
  if (!window)
  {
      glfwTerminate();
      exit(EXIT_FAILURE);
  }
  
  glfwMakeContextCurrent(window);
  glfwSwapInterval(1);


while (!glfwWindowShouldClose(window))
{
  float ratio;
  int width, height;

  glfwGetFramebufferSize(window, &width, &height);
  ratio = width / (float) height;

  initializationGL();
  resizeGL(width, height);
  PaintGL();

  glfwSwapBuffers(window);
  glfwPollEvents();

}

  glfwDestroyWindow(window);
 
  glfwTerminate();
  exit(EXIT_SUCCESS);


  return 0;
}

However, the output of the code is: enter image description here

Which is weird. They should be 8 vertex of a cube. I think I need to adjust OpenGL code to adjust the view port so that all particle become visible and show vertexes of a cube. Any idea how can I do it?


Solution

  • I think I need to adjust OpenGL code to adjust the view port

    No, the viewport rectangle is fine. You do not need to adjust the viewport, but you might need to adjust the viewing volume. The viewing volume is the area of the scene projected onto the viewport and defined by the projection matrix. However, that is also not the problem in this case.

    glTranslate does not just set a translation, but specifies a translation matrix and multiplies the current matrix by the translation matrix. This permanently changes a global state.
    Each of your spheres has its own translation, but the translations of the spheres should not be concatenated. Therefore, the current matrix must be saved and restored in the loop that draws the spheres:

    void PaintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
      
        for (int i=0; i<env.Particles.size(); i++){
            glPushMatrix();
    
            glTranslatef(Particles[i].X(0), Particles.X(1), Particles[i].X(2));  \\ X is an attribute of Particle that store poistion of that particle in space (X(0)=x, X(1)=y, X(2)=z)
            glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
            glLineWidth(2.0);
            GLUquadric *quad;
            quad = gluNewQuadric();
            gluSphere(quad, 1, 100, 20);
    
            glPopMatrix();
        }
    }