I’ve been coding the following application in Linux. It works ok with multiple viewports, but when I compile it in Visual Studio on Windows none of the subwindows work, and the application is not longer interactive. Can someone take a look. I’m loosing sleep over this
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::string;
void translate(float x, float y, float z);
void readFile(string s);
static float vAngle, hAngle, mouseX, mouseY = 0;
static float zoom = 1.3f;
static float pan = 0.0;
unsigned char Buttons[2] = {0};
static int w = 600;
static int h = 600;
static int border = 6;
float x=0.0f,y=1.5f,z=5.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
int mainWindow, subWindow1, subWindow2, subWindow3;
void display(void)
{
glutSetWindow(mainWindow);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
void subdisplay(int currentWindow)
{
//clear screen and buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, (GLfloat)w/(GLfloat)h, 0.3, 50);
//draw objects
glMatrixMode(GL_MODELVIEW);
glTranslatef(pan,0, -zoom);
glRotatef(hAngle, 0.0f, 1.0f, 0.0f);
glRotatef(vAngle, 0.0f, 0.0f, 1.0f);
translate(-0.5f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.2f, 20, 16);
glPopMatrix();
translate(0.0f, 0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidCube(0.2f);
glPopMatrix();
translate(0.5f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidTeapot(0.1f);
glPopMatrix();
//flush buffers
glutSwapBuffers();
glFlush();
}
void display_window1() {
glutSetWindow(subWindow1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(x, 0.1f, 0.1f, x + lx,0.1f,-0.1f, 0.0f,1.0f,0.0f);
subdisplay(subWindow1);
}
void display_window2() {
glutSetWindow(subWindow2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0.0, 0.0, 0.0, 0.1, 0.1, 0.1, 0, 1, 0);
subdisplay(subWindow2);
}
void display_window3() {
glutSetWindow(subWindow3);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(x-lz*10 , y, z+lx*10,x ,y ,z ,0.0f,1.0f,0.0f);
subdisplay(subWindow3);
}
void translate(float x, float y, float z)
{
glPushMatrix();
glTranslatef(x,y,z); // position object
}
void orient(int x, int y)
{
int xDiff = (x - mouseX);
int yDiff = (y - mouseY);
mouseX = x;
mouseY = y;
if( Buttons[1] )
{
zoom += (float) 0.1f * yDiff;
pan += (float) 0.1f * xDiff;
if (zoom < 1.3)
{
zoom = 1.3;
}
else if (zoom > 48)
{
zoom = 48;
}
}
else if( Buttons[0] )
{
vAngle += (float) yDiff;
hAngle += (float) xDiff;
}
glutPostRedisplay();
}
void clicker(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;
switch(button)
{
case GLUT_LEFT_BUTTON:
Buttons[0] = ((GLUT_DOWN==state)?1:0);
break;
case GLUT_RIGHT_BUTTON:
Buttons[1] = ((GLUT_DOWN==state)?1:0);
break;
default:
break;
}
}
void renderSceneAll()
{
display_window1();
display_window2();
display_window3();
}
void readFile(string s)
{
ifstream fin(s.c_str());
if (!fin.good())
{
cout << "Cannot find " + s << endl;
}
else
{
char ch;
while ((ch = fin.get()) != '
')
{
cout << ch;
}
cout << endl;
}
fin.close();
}
void init(void)
{
GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat mat_shininess[] = {50.0f};
GLfloat light_position[] = {1.0f, 1.0f, 1.0f, 0.0f};
GLfloat white_light[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
glShadeModel(GL_SMOOTH); // smooth shading
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST); // depth testing
glEnable(GL_COLOR_MATERIAL);
//glEnable(GL_LINE_SMOOTH);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glDepthFunc(GL_LEQUAL); // depth testing type
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(w, h);
mainWindow = glutCreateWindow("Graphics Coursework");
glutDisplayFunc(display);
glutIdleFunc(renderSceneAll);
init();
subWindow1 = glutCreateSubWindow(mainWindow,border,border,w-2*border, h/2 - border*3/2);
glutDisplayFunc(display_window1);
glutMouseFunc(clicker);
glutMotionFunc(orient);
init();
subWindow2 = glutCreateSubWindow(mainWindow,border,(h+border)/2,w/2-border*3/2, h/2 - border*3/2);
glutDisplayFunc(display_window2);
glutMouseFunc(clicker);
glutMotionFunc(orient);
init();
subWindow3 = glutCreateSubWindow(mainWindow,(w+border)/2,(h+border)/2,w/2-border*3/2,h/2 - border*3/2);
glutDisplayFunc(display_window3);
glutMouseFunc(clicker);
glutMotionFunc(orient);
init();
glutMainLoop();
return 0;
}