Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: GLUT & glViewPort problem

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2003
    Posts
    12

    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:
    Code :
    #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(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    	glutInitWindowSize(500, 500);
     
    	GUIwindow = glutCreateWindow(" --- LightCounstruct v001 ---");
     
    	GUIwindowInit();
    	glutDisplayFunc(&amp;GUIScene);
    	glutKeyboardFunc(&amp;NormalKeyboardEvent);
    	glutMouseFunc(&amp;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

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: GLUT & glViewPort problem

    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?

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2003
    Posts
    12

    Re: GLUT & glViewPort problem

    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.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: GLUT & glViewPort problem

    Code :
    gluPerspective(60.0f, 250/250, 0, 20);
    The near clip plane (second last parameter) must be a positive value.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: GLUT & glViewPort problem

    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.

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2003
    Posts
    12

    Re: GLUT & glViewPort problem

    It works, thank you Bob!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •