LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   OpenGL and GLUT in Fedora 12: windows too small and don't resize (https://www.linuxquestions.org/questions/programming-9/opengl-and-glut-in-fedora-12-windows-too-small-and-dont-resize-795444/)

mejohnsn 03-15-2010 01:08 AM

OpenGL and GLUT in Fedora 12: windows too small and don't resize
 
I have verified using yum that I have the most uptodate glut, freeglut, freeglut-devel etc., yet when I compile Example 2-6 from the Red Book at http://www.glprogramming.com/red/chapter02.html#name16, I get a window that is too small for the program output, includes display from other windows, and will not redraw after being resized.

I get slightly better behavior with the SGI sample program mentioned in the same book, 'checkers.c'. Again, the initial size is too small, but at least it will resize and redraw the checkerboards entirely inside the resized window.

What is going on here? Is this some bug in glut? I can't see anything obviously wrong in their glut initialization, which looks like:

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 150);
// glutInitWindowSize (700, 300); // did not help
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;

Then again, since I am such a newbie to glut, I am not sure I would recognize what, if anything, they did wrong.

But I have a simpler question: what ARE the valid and useful command line parameters I could have passed to the main() above?

vendtagain 03-15-2010 01:44 AM

this part looks good to me,
what does your reshape function look like?


Quote:

But I have a simpler question: what ARE the valid and useful command line parameters I could have passed to the main() above?
I'm don't know of any command line arguments for this, although there may be some I don't know, it should run fine without any arguments,./myProgram

mejohnsn 03-15-2010 02:33 PM

Quote:

Originally Posted by vendtagain (Post 3898618)
this part looks good to me,
what does your reshape function look like?

Thanks for the quick reply. It is:



void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

devnull10 03-15-2010 04:29 PM

After you call gluOrtho2D, try calling

Code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


mejohnsn 03-15-2010 10:49 PM

Quote:

Originally Posted by devnull10 (Post 3899488)
After you call gluOrtho2D, try calling

Code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


O...K..., following your suggestion I now have:

Code:

void reshape (int w, int h)
{
  glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

}

At first glance, I thought this was unconditionally worse, since nothing shows up after resizing using the mouse. But after switching windows back to this browser window and then back yet again o the program output, I see the upper inch or so of the desired output -- clipped at the very bottom of the Fedora (GTK?) window!

So the placement in the output window is still very wrong, but once it is forced to redraw after losing focus, the portion that gets rendered gets rendered correctly.

Does this tell you anything?

BTW: I have both Gnome and KDE loaded on this thing. How do I tell which windowing system it is using? Could this be what is confusing GLUT?

vendtagain 03-16-2010 11:08 AM

Code:

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

This also looks good. I still have all my code from going through that same guide. I compared both your main and reshape functions to my own, and they are the same. So the code is probably not the problem.(note: i used Mac OSX)

Quote:

I have both Gnome and KDE loaded on this thing. How do I tell which windowing system it is using? Could this be what is confusing GLUT?
If your current desktop isnt showing correctly, try the other. so if you are currently using Gnome for the desktop, switch to KDE and see if you get better results. Or you might also try running it from command line, I think it only requires x11 (though not sure about this).

devnull10 03-16-2010 12:57 PM

Try compiling the following code and see if it works for you. It does on mine:

Code:

  #include <GL/glut.h>

  float r= 0.0;
  int hidden= 1, flat= 1;
 
  void init(void) {
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glEnable (GL_LIGHTING);
    glEnable (GL_LIGHT0);
  }
 
  void spin (void) {
    r+= 1.0;
    glutPostRedisplay();
  }
 
  void display(void) {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    if (hidden) glEnable(GL_DEPTH_TEST);
    else glDisable(GL_DEPTH_TEST);

    if (flat) glShadeModel (GL_FLAT);
    else glShadeModel (GL_SMOOTH);

    glLoadIdentity ();
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
     
    glColor3f (1.0, 0.0, 0.0);
    glutSolidCube(1.0);          /* Red cube */

    glRotatef(r*2.0, 0, 1, 0);  /* Orbit angle */
    glTranslatef(0.0, 0.0, 1.0); /* Orbit radius */
    glRotatef(r, 1, 0, 0);      /* Tumble in x,y,z */
    glRotatef(r, 0, 1, 0);
    glRotatef(r, 0, 0, 1);
    glColor3f (0.0, 1.0, 0.0);
    glutSolidSphere(0.5, 20, 15); /* Green sphere */

    glutSwapBuffers();
  }
 
  void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat) w / (GLfloat) h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
  }

  void keyboard(unsigned char key, int x, int y) {
    if (key == 27) { exit (0); } /* escape key */
    if (key == 'h') hidden= !hidden;
    if (key == 's') flat= !flat;
  }

 int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("ex9");
    init ();
    glutDisplayFunc (display);
    glutReshapeFunc (reshape);
    glutKeyboardFunc (keyboard);
    glutIdleFunc (spin);
    glutMainLoop ();
    return 0;
  }


mejohnsn 03-16-2010 04:16 PM

Quote:

Originally Posted by devnull10 (Post 3900701)
Try compiling the following code and see if it works for you. It does on mine:

Code:

  #include <GL/glut.h>
...
  float r= 0.0;
  int hidden= 1, flat= 1;
 int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("ex9");
    init ();
    glutDisplayFunc (display);
    glutReshapeFunc (reshape);
    glutKeyboardFunc (keyboard);
    glutIdleFunc (spin);
    glutMainLoop ();
    return 0;
  }


Cool! It painted a white square in the middle, and showed an orbiting polyhedral approximation to a sphere.

Since it was right in the middle, and did not require any switching in/out of focus, I assume it worked flawlessly.

So what does this tell us?

mejohnsn 03-16-2010 04:19 PM

Quote:

Originally Posted by vendtagain (Post 3900560)
Code:

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

This also looks good. I still have all my code from going through that same guide. I compared both your main and reshape functions to my own, and they are the same. So the code is probably not the problem.(note: i used Mac OSX)


If your current desktop isnt showing correctly, try the other. so if you are currently using Gnome for the desktop, switch to KDE and see if you get better results. Or you might also try running it from command line, I think it only requires x11 (though not sure about this).

Since I only installed KDE-base to run Konqueror and Krita, I am not even sure how this is done;)

Then again, it may not even be relevant after all, since in fact, I have always invoked the OpenGL sample programs from the command line, usually Gnome's Terminal, very rarely KDE's Konsole, which, however, gave the identical results.

So, for example, I named devnull10's code 'Ex9.c', compiled with "gcc -o Ex9 -lglut Ex9.c" and ran with "./Ex9" to see much better results.

mejohnsn 03-16-2010 05:47 PM

Quote:

Originally Posted by mejohnsn (Post 3900890)
Cool! It painted a white square in the middle, and showed an orbiting polyhedral approximation to a sphere.

Since it was right in the middle, and did not require any switching in/out of focus, I assume it worked flawlessly.

So what does this tell us?

Then again, now that I actually paused to read the code, I see that no, it is not working flawlessly: the only sphere drawn is white, not green, though the code should have drawn a green sphere with:
Code:

  glColor3f (0.0, 1.0, 0.0);
  glutSolidSphere(0.5, 20, 15); /* Green sphere */

But there really is only one sphere, and it is greyish white.

devnull10 03-16-2010 05:53 PM

Quote:

Originally Posted by mejohnsn (Post 3900890)
So what does this tell us?

Well, it tells us that your system can render OpenGL applications properly and the reason that your application isn't doing is likely an issue with your matrices, projection coordinates, or something similar, rather than an actual system error.

[edit]
Just seen your post about the colours - not too sure about that - I'll take a look later, just in the middle of something now! :)

mejohnsn 03-26-2010 11:41 PM

Quote:

Originally Posted by devnull10 (Post 3900975)
Well, it tells us that your system can render OpenGL applications properly and the reason that your application isn't doing is likely an issue with your matrices, projection coordinates, or something similar, rather than an actual system error.

[edit]
Just seen your post about the colours - not too sure about that - I'll take a look later, just in the middle of something now! :)

Any ideas about the colors? Could this be a bug in the OpenGL libraries I am using? Do I need lighting commands to be a workaround for a bug?

I can't remember how to view what version of OpenGl and GLUT I have, but I do remember installing the most up-to-date available as packages for Fedora; I did the install within the last three weeks.

devnull10 03-27-2010 04:08 AM

1 Attachment(s)
Try this - rename the attachment to ex4.tar and then extract. I think the example I provided might have been a poor one - it was just one I pulled from my old uni archive! :)
This is one I wrote when at uni.


All times are GMT -5. The time now is 08:01 AM.