PDA

View Full Version : Can somebody help me to Create Multiple Viewports?



deviputhran
11-21-2006, 07:21 AM
The code to draw a table is below. I want the same figure to be displayed in multiple viewports with in a window.



#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>

static float x = 0.0, z = 0.0;

void init(void){
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}

void reshape(int w, int h){

glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20.0, 20.0, -20.0, 20.0, -10.0, 25.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void table(){
glTranslatef(x,-2.5,z);
glScalef(1.0,3.0,1.0);
glutWireCube(1.0);
}

void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
gluLookAt(6.0, 1.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0);
glPushMatrix();
glScalef(8.0, 1.0, 8.0);
glutWireCube(2.0);
glPopMatrix();
glPushMatrix();
x = -6.0; z = -6.0;
table();
glPopMatrix();
glPushMatrix();
x = 6.0; z = -6.0;
table();
glPopMatrix();
glPushMatrix();
x = -6; z = 6.0;
table();
glPopMatrix();
glPushMatrix();
x = 6; z = 6.0;
table();
glPopMatrix();
glPopMatrix();
glFlush();
}

int main(int argc, char* argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();

return 0;
}

yalmar
11-21-2006, 09:14 AM
I believe that you can implement it using subwindows, see Nate Robin's web
Another way to do it is redefining the glViewPort function, see code below:


void draw_something(int x, int y, int w, int h){
/// mini viewport
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75.0, 1.0, 0.1, 200.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0, 0.0, -5.0);
glRotatef(-45.0, 0.0, 1.0, 0.0);
glClearColor(0.0, 0.0, 0.0, 0.0);

glutSolidCube(0.5);
}
}

typedef void (*DrawFunc)(int x, int y, int w, int h);
/// creates a sub-window attaching a draw function pointer
void sub_window(int x, int y, int w, int h, DrawFunc draw_func){
/// customized viewport
{
/// set viewport size
glViewport(x, y, w, h);
glClear(...); // optional

/// call a render function
draw_func(x, y, w, h);
}
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

gluLookAt(...);

draw_model();

/// save matrix states
glGetDoublev(GL_MODELVIEW_MATRIX, _modelview);
glGetDoublev(GL_PROJECTION_MATRIX, _projection);

/// call to draw sub windows here
sub_window(0, 0, 255, 255, draw_some_A);
sub_window(0, 255, 255, 255, draw_some_B);
...
/// reload saved matrix states
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(_projection);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(_modelview);
}

deviputhran
11-26-2006, 06:41 AM
Thank you very much. I will try this idea.