Results 1 to 3 of 3

Thread: viewport background color

  1. #1
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    1

    viewport background color

    Hey.
    I'm trying to create a window with two viewports, one for 2d and one for 3d. As far as I can tell, I managed to do that. The problem is that I want one of the viewports to have a white background and I haven't figured out how to do that. In the mean time I drew a rectangle the size of the viewport:

    Code :
     glRectf(-1,1, 1, -1);
    But it dissapears if the size of the window changes or if I change windows.
    In addition, I drew two cubes in the 3d viewport but if I shrink the window vertically, the cubes don't fit themselves to the new size (horizontally they do).

    This is a short version of my whole code so far:

    Code :
    #include <windows.h>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
     
     
    static int WIDTH = 640, HEIGHT = 480;
     
     
    void draw3d(){
    	// Define the viewport
    	glViewport(10, 10, WIDTH/2.0f-15, HEIGHT-20);	
    	// Define the projection matrix 
    	glMatrixMode(GL_PROJECTION);	
    	glLoadIdentity();	
    	// Define perspective
    	gluPerspective(25.0, (float) WIDTH / HEIGHT, 0.1f, 500.0);									glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();	
    	gluLookAt(10.0, 10.0, 15.0, 0.7, 1.0, 0.0, 0.0, 1.0, 0.0);
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_LIGHTING);
    	glShadeModel(GL_FLAT);
    	// Draw two cubes (just for testing) 
    	glPushMatrix();
    	glTranslatef(-3.0, 0.0, 0.0);
    	glutSolidCube(WIDTH*0.001);
    	glTranslatef(3.0, 0.0, 0.0);
    	glutSolidCube(WIDTH*0.002);
    	glEnd();
    }
     
    void draw2d(){	
    	// Define the viewport
    	glViewport(WIDTH/2.0f+5, 10, WIDTH/2.0f-15, HEIGHT-20);
    	// Define the projection matrix 
    	glMatrixMode(GL_PROJECTION);	
    	glLoadIdentity();
    	// clear color and depth buffers
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glColor3f(1.0f, 1.0f, 1.0f);	
    	glRectf(-1,1, 1, -1);		
    }
     
    /* Handler for redisplaying the window */
    void displayWindow()
    {		
    	// Fill the background 
    	glClearColor(0.0f, 0.0f, 0.2f, 0.0f);
    	draw2d();
    	draw3d();
    	glutSwapBuffers();
    }
     
    /* Handler for window resizing */
    void reshapeWindow(int width, int height)
    {	
    	//glViewport(0, 0, width, height);
    	WIDTH = width;
    	HEIGHT = height;
    }
     
    /* Program entry point */
    int main(int argc, char **argv)
    {	
    	// Init windows Functions 
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); /* Double buffer, Depth buffer (Z-buffer) */
    	glutInitWindowSize(WIDTH, HEIGHT);
    	glutInitWindowPosition(100, 100);
    	glutCreateWindow("Basic 3D");
    	// Turn on Light0 
    	glEnable(GL_LIGHT0);
    	// Auto-normalize the normals 
    	glEnable(GL_NORMALIZE);
    	// Shade both sides of the polygons 
    	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
    	// Call-Back functions 
    	glutReshapeFunc(&amp;reshapeWindow);
    	glutDisplayFunc(&amp;displayWindow);
    	// Wait for events 
    	glutMainLoop();
    	return 0;
    }


    Any help appreciated.
    Thanks!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2009
    Posts
    153

    Re: viewport background color

    Hey,
    I think that you should use glScissor. This function can control what part of the buffer glClear affects. So, set up first viewport, set clear color, set scissor and then clear. The same for the second viewport. Hope that helps.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Apr 2007
    Posts
    268

    Re: viewport background color

    randall is right:
    1. set scissor size
    glScissor(left,bottom,width,height);

    2. enable scissor test
    glEnable(GL_SCISSOR_TEST);
    3. use clear
    glClearDepth(1.0);
    glClearColor(r,g,b,a);
    glClear(COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    However, clear does not require to set the viewport at all.
    The viewport is used for vertex coordinate operations only.

    You can visualize that as well on the OpenGL 1.1 state machine (locate the ClearControl box in the lower right corner):
    http://www.opengl.org/documentation/...n1.1/state.pdf

Similar Threads

  1. How can i set color to a viewport area?
    By colagl in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-04-2012, 07:43 AM
  2. how to set viewport background color
    By linuxxy in forum OpenGL ES
    Replies: 0
    Last Post: 09-16-2011, 12:11 AM
  3. Specifying background (image) in a viewport?
    By BigShooter in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-28-2003, 05:47 PM
  4. How to set the background color?
    By vandana_pv in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 07-07-2002, 09:56 PM
  5. Background color
    By amstel in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-17-2002, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean