PDA

View Full Version : viewport background color



dc333
05-23-2009, 01:35 AM
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:


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:



#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!

randall
05-23-2009, 05:10 AM
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.

overlay
05-23-2009, 08:07 AM
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/specs/version1.1/state.pdf