GLUT & glViewPort problem

I suppose that to use subwindows under GLUT could be easier than to use viewports with glViewPort. But I’d like to know the mechanism of glViewPort too.
I’ve made an little program to test it. In the program viewprot should be the quarter of the entire window and should take place in the lower-left corner, but it doesn’t want to work. Please check this code and tell me what did I wrong:

#include <stdio.h>
#include <GL\glut.h>
#include “LC_gui.h”

// The Following Directive Fixes The Problem With Extra Console Window
#pragma comment(linker, “/subsystem:"windows" /entry:"mainCRTStartup"”)

//-------------------->> Global variables <<--------------------
//GLint GUIwindowWidth, GUIwindowHeight;
GLint GUIwindow;

void GUIwindowInit(void);
void NormalKeyboardEvent(unsigned char key, int x, int y);
void MouseEvent(int button, int state, int x, int y);
void GUIScene();

//-------------------->> The Main Function <<--------------------
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(500, 500);

GUIwindow = glutCreateWindow(" — LightCounstruct v001 —");

GUIwindowInit();
glutDisplayFunc(&GUIScene);
glutKeyboardFunc(&NormalKeyboardEvent);
glutMouseFunc(&MouseEvent);
// glutFullScreen();

glutMainLoop();
return 0;
}

//-------------------- Funkciók —<<

void GUIwindowInit(void)
{
GLdouble AspectRatio;

glClearColor (0.0, 0.2, 0.0, 0.0);
glShadeModel (GL_FLAT);
AspectRatio = (GLfloat)LC_GUI_WINDOW_WIDTH / (GLfloat)LC_GUI_WINDOW_HEIGHT;
glViewport(0, 0, 250, 250);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, 250/250, 0, 20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void NormalKeyboardEvent(unsigned char key, int x, int y)
{
switch (key) {
case 27 :
glutDestroyWindow(GUIwindow);
exit(0);
}
}

void MouseEvent(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
exit(0);
break;
default:
break;
}
}

void GUIScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3f(-30.0, 30.0, 0.0);
glVertex3f(30.0, 30.0, 0.0);
glVertex3f(30.0, -30.0, 0.0);
glVertex3f(-30.0, -30.0, 0.0);
glEnd();

glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-40.0, 40.0, 0.0);
glVertex3f(40.0, 40.0, 0.0);
glVertex3f(40.0, -40.0, 0.0);
glVertex3f(-40.0, -40.0, 0.0);
glEnd();
glColor3f(1.0, 0.5, 0.0);
glutWireSphere(2.0, 20, 16);
glFlush();
}

brown

Can you be a little more clear about what is wrong?

Does it run?
Does it draw just a blank screen?
Draws not the why as expected?

Originally posted by nexusone:
Can you be a little more clear about what is wrong?

OK. It’s run and draws not what I expect.
The program draws first a white, filled rectangle (and a sphere over it… to see the aspect ratio). We can’t see the entire rectangle, it fills the entire viewport, because of viewport and projection setting(I hope). I’ve set the viewport to a 250x250 rectangle in the 500x500 window.
So I expected to see a white rectangle in the lower-left corner of the window, but my scene fills the entire window.

gluPerspective(60.0f, 250/250, 0, 20);

The near clip plane (second last parameter) must be a positive value.

Oh, and the above doesn’t have anything to do with the viewport, but it’s still a problem.

Anyways, the problem is that you don’t have a resize callback. When the window is first shown the default viewport is set to cover the entire window, but you set your viewport before the window is first shown (and than happens in the main loop, not when you crate the window), therefore the default viewport replaces the one you set. Make GUIwindowInit a resize callback instead.

It works, thank you Bob!