Sign in to follow this  
OgbobeBuko

OpenGL new Opengl Window

Recommended Posts

hi, just wanted to ask if it was possible to have two opengl windows.....something of this nature......run program first window displays click button in the first window and the second window displays covering the first one...close the second window the first window dislays again...

if this is possible can anyone please tell me how i could achieve that......
working with opengl and glut

thank you.

Share this post


Link to post
Share on other sites
http://docs.google.com/viewer?a=v&q=cache:o0oeSgPKnqIJ:www.cs.uml.edu/~hmasterm/Charts/Managing_Multiple_Windows.ppt+glut+multiple+windows+demo&hl=sv&gl=se&pid=bl&srcid=ADGEESihxzk1y3TDCXn6d5irTvT0eoQWFZmh6L-rLJo0UKQNO5WLd7tjoe8DxW6_wOzOu1F6U7VqTnFtTkhbGkd-gyJn6TpPkbNUcjAtHqDgSsGSZ-keOGkPA16TRsnBflwj8NMrIshN&sig=AHIEtbQDybhldTyKsm014-eghrMqGlOgHQ

or if you have powerpoint:

http://www.cs.uml.edu/~hmasterm/Charts/Managing_Multiple_Windows.ppt

Share this post


Link to post
Share on other sites
I don't think GLUT can get you multiple windows. It's really intended as a simple framework for abstracting some of the OS interaction, but that simplification comes at a cost and the cost is flexibility.

Share this post


Link to post
Share on other sites
thanks guys for your reply....

@Brother Bob ..i have a window already with 2D buttons rendered...but i want to also add animations into my program..but i want the animations to run in another opengl window after the animation button has been clicked....then return back to the first window after the animation is finished...

@SimonForsman...thanks for the link ..i'll take a look at it...

Share this post


Link to post
Share on other sites
All you need to do is create the second window as you need it, draw as usual, and destroy it when you're done. As I said in my first post, I'm not sure what specific part, or if it's the whole concept of having multiple window, you're having problem with.

From your vague description of your program, it sounds like you just need to do the following:
[list][*]When clicking the button, create a new window and register the callbacks for that window.[*]The new window's display callback displays the animation.[*]The new window's idle callback drives the animation forward.[*]When the animation is finished, destroy it.[/list]

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1311541450' post='4839737']
All you need to do is create the second window as you need it, draw as usual, and destroy it when you're done. As I said in my first post, I'm not sure what specific part, or if it's the whole concept of having multiple window, you're having problem with.

From your vague description of your program, it sounds like you just need to do the following:
[list][*]When clicking the button, create a new window and register the callbacks for that window.[*]The new window's display callback displays the animation.[*]The new window's idle callback drives the animation forward.[*]When the animation is finished, destroy it.[/list]
[/quote]

thanks Brother Bob for your reply.......i am still having issues creating the second window.....the first window creates alright but when i click the button for the animation window to be created nothing happens..
any idea why this might be happening??

[code]


void main(int argc, char** argv) {
atexit(OnShutdown);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(width, height);
//glutCreateWindow("GLUT Picking Demo [using selection buffer]");

// Make Main outer window
window1 = glutCreateWindow("Satellite Assembly");
glutDisplayFunc(OnRender);
glutReshapeFunc(OnReshape);
glutIdleFunc(OnIdle);


if(animate){
// Create First subwindow
window2 = glutCreateWindow("Animation");
glutDisplayFunc(display2);
glutReshapeFunc(animation_Reshape);
glutIdleFunc(OnIdle1);

}


glutKeyboardFunc(OnKey);
glutSpecialFunc (keyboard_s);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutPassiveMotionFunc(MousePassiveMotion);

InitGL();

glutMainLoop();
}



static void TheButtonCallback8()
{
animate=true;
loadMast = true;
loadReflector = true;
loadReceiver = true;
loadConnector = true;
loadStand = true;
printf("Run Animation");
}

[/code]

thanks alot for your help

Share this post


Link to post
Share on other sites
Think about your code; under what conditions will the second window be created?

I will be created when [i]main [/i]executes and [i]animate[/i] is true. The variable [i]animate[/i] is not true when you start the program, so it won't be created when the program starts. Is the function [i]main [/i]executed again, at any time, after [i]animate[/i] is set to true? If not, then that code is never going to be reached again. But if it is called again, so the second window can be created, you have other problems as well; you will completely reinitialize everything, including creating another primary window. Not to mention that in a well-formed program, you are not allowed to call [i]main[/i] yourself.

You need to create the window in some piece of code that is actually executed when you press your button. If it's not executed, well, then nothing happens. Take a look at this quick example.
[source]
#include <GL/glut.h>

int primaryWindow = 0;
int secondaryWindow = 0;
GLfloat angle = 0;

void idleFunc();

void redisplaySecondaryFunc()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -5);
glRotatef(angle, 0, 1, 0);
glutWireTeapot(2);
glutSwapBuffers();
}

void reshapeSecondaryFunc(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1, 10);
glMatrixMode(GL_MODELVIEW);
}

void redisplayFunc()
{
glClearColor(0.75, 0.75, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}

void reshapeFunc(int w, int h)
{
glViewport(0, 0, w, h);
}

void mouseFunc(int button, int state, int, int)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && secondaryWindow == 0)
{
secondaryWindow = glutCreateWindow("animation");
glutDisplayFunc(redisplaySecondaryFunc);
glutReshapeFunc(reshapeSecondaryFunc);
glutIdleFunc(idleFunc);

angle = 0;
}
}


void idleFunc()
{
if(secondaryWindow != 0)
{
angle += 1;
if(angle < 360)
{
glutPostWindowRedisplay(secondaryWindow);
}
else
{
glutIdleFunc(0);
glutDestroyWindow(secondaryWindow);

secondaryWindow = 0;
angle = 0;
}
}
}

void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize(400,400);
primaryWindow = glutCreateWindow(argv[0]);
glutDisplayFunc(redisplayFunc);
glutMouseFunc(mouseFunc);
glutReshapeFunc(reshapeFunc);
glutMainLoop();
}
[/source]
It creates secondary window with some animation when you click inside the primary window. Keep in mind though that the idle callback is global and not tied to any particular window.

Share this post


Link to post
Share on other sites
solved the errors but its having serious effects on my animations ....cant explain why.....models don't display properly(look like shadows)...some timing orientations were also affected......any ideas on how i could solve this problem...


[code]


static void TheButtonCallback8()
{

if(window2==0){
window2 = glutCreateWindow("animation");
glutDisplayFunc(display2); //animation display
glutReshapeFunc(OnReshape);
glutIdleFunc(onIdle1);
}


printf("Run Animation");
}

void OnIdle1()
{
if(window2 != 0)
{
rY += 0.05;

glutPostWindowRedisplay(window2);
}
else
{
glutIdleFunc(0);
glutDestroyWindow(window2);
glutPostWindowRedisplay(window1);

window2 = 0;
rY = 0;
}

}

void main(int argc, char** argv) {
atexit(OnShutdown);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(width, height);
//glutCreateWindow("GLUT Picking Demo [using selection buffer]");

// Make Main outer window
window1 = glutCreateWindow("Satellite Assembly");
glutDisplayFunc(OnRender);
glutReshapeFunc(OnReshape);
glutIdleFunc(onIdle);

glutKeyboardFunc(OnKey);
glutSpecialFunc (keyboard_s);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutPassiveMotionFunc(MousePassiveMotion);

InitGL();

glutMainLoop();
[/code]

Share this post


Link to post
Share on other sites
If you can't explain what the problem is with the animation, then we can't help you solve it. It you won't show the code for drawing the objects, then we can't help you solve why it "looks like shadows".

Share this post


Link to post
Share on other sites
Hidden
[quote name='Brother Bob' timestamp='1311723074' post='4840876']
If you can't explain what the problem is with the animation, then we can't help you solve it. It you won't show the code for drawing the objects, then we can't help you solve why it "looks like shadows".
[/quote]

my apologies for not making my question's clear enough

if i call the display for the animation directly from the main it works fine(i.e if its just using one window i am using).....but when i try to make it two windows....it does'nt display properly.....feel like i'm missing something

[code]

void main(int argc, char** argv) {
atexit(OnShutdown);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(width, height);

// Make Main outer window
window1 = glutCreateWindow("Satellite Assembly");
glutDisplayFunc(OnRender);
//glutDisplayFunc(display2); //display for animation............uncommenting this line and commenting the line above...work fine for the animation
glutReshapeFunc(OnReshape);
glutIdleFunc(onIdle);

glutKeyboardFunc(OnKey);
glutSpecialFunc (keyboard_s);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutPassiveMotionFunc(MousePassiveMotion);

InitGL();

glutMainLoop();
}
[/code]

but if i try to run the animation from the button callback...... the models don't display properly
[code]

static void TheButtonCallback8()
{

loadMast = true;
loadReflector = true;
loadReceiver = true;
loadConnector = true;
loadStand = true;

if(window2==0){

window2 = glutCreateWindow("animation");
glutDisplayFunc(display2);
glutReshapeFunc(OnReshape);
glutIdleFunc(OnIdle1);

}


printf("Run Animation");
}



void display2()
{


float current = (float) glutGet(GLUT_ELAPSED_TIME);
++totalFrames;
if((current-startTime)>1000)
{
float elapsedTime = (current-startTime);
fps = (totalFrames/ elapsedTime)*1000 ;
char info[MAX_PATH]={0};
sprintf_s(info, "FPS: %3.2f - Move Plane: %s - Press '1' for XY plane, '2' for XZ plane",fps,( move_plane==0)?"XY Plane":"XZ Plane");
glutSetWindowTitle(info);
startTime = current;
totalFrames=0;
}

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();


/*
* Set perspective viewing transformation
*/

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.f, 100.0f);
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,10000.0f);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


//glMatrixMode(GL_MODELVIEW);
//setup the view transformation

glTranslatef(0,0,dist);
glRotatef(rX,1,0,0);
glRotatef(rY,0,1,0);


glGetDoublev(GL_MODELVIEW_MATRIX, MV);
viewDir.x = (float)-MV[2];
viewDir.y = (float)-MV[6];
viewDir.z = (float)-MV[10];
Right = glm::cross(viewDir, Up);

//draw grid
DrawGrid();

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);


glMatrixMode(GL_MODELVIEW);

glPushMatrix();
Animation1();
glPopMatrix();

glDisable(GL_LIGHTING);
glutSwapBuffers();

}



void Animation1()
{
if (flag1){
// Tell the timer a new frame has been started:
g_Timer.StartNewFrame();

// Start and End time of the teapot animation:
float StartTime = 2.0f;
float EndTime = 5.0f;

// Time since last frame, and total rendering time:

float FrameTime = g_Timer.GetFrameTime();
static float TotalTime = 0.0f;
TotalTime += FrameTime;

// Start and end position of the first model, as well as it's current position:
// StartPoint = {1, 0, -2};
// EndPoint = {4, 5, 0};
glm::vec3 StartPoint=glm::vec3(-15,3.4,10);
glm::vec3 EndPoint=glm::vec3(7, 5, -1.9);

static glm::vec3 CurrentPos = StartPoint;

// If we're in the animation interval, animate:
if ((TotalTime > StartTime) && (TotalTime < EndTime))
{
CurrentPos.x += (EndPoint.x - StartPoint.x) * (FrameTime / (EndTime - StartTime));
CurrentPos.y += (EndPoint.y - StartPoint.y) * (FrameTime / (EndTime - StartTime));
CurrentPos.z += (EndPoint.z - StartPoint.z) * (FrameTime / (EndTime - StartTime));
}

glPushMatrix();
// Translate and draw:
glTranslatef(CurrentPos.x, CurrentPos.y, CurrentPos.z);
glScalef( .3, .3, .3 );
glColor3f(1,0,0);
drawMast();
glPopMatrix();


connectMast=true;

if(TotalTime > 5){
//flag1=false;
flag2=true;
}

}//end flag1

if (flag2){
connectMast=false;
connectReflector=true;
// Tell the timer a new frame has been started:
g_Timer.StartNewFrame();

// Start and End time of the 2nd model animation:

float StartTime1 = 1.0f;
float EndTime1 = 2.0f;

// Time since last frame, and total rendering time:
float FrameTime1 = g_Timer.GetFrameTime();
static float TotalTime1 = 0.0f;
TotalTime1 += FrameTime1;

// Start and end position of the teapot, as well as it's current position:
// StartPoint = {1, 0, -2};
// EndPoint = {4, 5, 0};
glm::vec3 StartPoint1=glm::vec3(-15,3.4,-10);
glm::vec3 EndPoint1=glm::vec3(7, 9, -2);

static glm::vec3 CurrentPos1 = StartPoint1;

// If we're in the animation interval, animate:
if ((TotalTime1 > StartTime1) && (TotalTime1 < EndTime1))
{
CurrentPos1.x += (EndPoint1.x - StartPoint1.x) * (FrameTime1 / (EndTime1 - StartTime1));
CurrentPos1.y += (EndPoint1.y - StartPoint1.y) * (FrameTime1 / (EndTime1 - StartTime1));
CurrentPos1.z += (EndPoint1.z - StartPoint1.z) * (FrameTime1 / (EndTime1 - StartTime1));
}
glPushMatrix();
// Translate and draw:
glTranslatef(CurrentPos1.x, CurrentPos1.y, CurrentPos1.z);
glScalef( .3, .3, .3 );
//glRotatef(90,0,1,0);
drawReflector();
glPopMatrix();

if(TotalTime1 > 3){
flag2=false;
flag1=false;
}
}//end flag2 and animation
}
[/code]

thanks for your help

Share this post


Link to post
fixed the model dispalying like shadows by basically just coping the part of the main to the call back.....dont know if its right but it seemed to work

the only problem i now is that....the first model in the animation seem to go further than its end position in the animation....dont know the cause of that


[code]

static void TheButtonCallback8()
{
animate=true;
loadMast = true;
loadReflector = true;
loadReceiver = true;
loadConnector = true;
loadStand = true;

if(window2==0){

window2 = glutCreateWindow("animation");
glutDisplayFunc(display2);
glutReshapeFunc(OnReshape);
glutIdleFunc(OnIdle1);

glutKeyboardFunc(OnKey);
glutSpecialFunc (keyboard_s);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutPassiveMotionFunc(MousePassiveMotion);
InitGL();
glutMainLoop();

}


printf("Run Animation");
}
[/code]

Share this post


Link to post
Share on other sites
hi, i am still having problems with multiple windows.....when i destroy the second window by pressing the esc key it closes but then i cant use the keyboard function on the main window....please could you give me an idea how to fix that.......
i also noticed that when i click the button again to create the window after i have destroyed it ...it does not create

thank you..

[code]

void OnKeyAnimation(unsigned char key, int x, int y){

if (key == 27){
bool loadMast= false;
bool loadReflector= false;
bool loadReceiver= false;
bool loadConnector= false;
bool loadStand= false;
glutDestroyWindow(window2);


//exit(0);
}
}

static void TheButtonCallback8()
{

loadMast = true;
loadReflector = true;
loadReceiver = true;
loadConnector = true;
loadStand = true;

animate=true;

if(window2==0){

window2 = glutCreateWindow("animation");
glutDisplayFunc(display2);
glutReshapeFunc(animation_Reshape);
glutIdleFunc(OnIdle1);

glutKeyboardFunc(OnKeyAnimation);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
InitGL();
glutMainLoop();


}


printf("Run Animation");
}
[/code]

Share this post


Link to post
Share on other sites
I cannot say much about your problems since the description is quite vague and you're only showing small pieces of code. But whatever you do, you absolutely don't want to call glutMainLoop again after creating the window.

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1311884102' post='4841818']
I cannot say much about your problems since the description is quite vague and you're only showing small pieces of code. But whatever you do, you absolutely don't want to call glutMainLoop again after creating the window.
[/quote]


its a very long code....i omitted ones i thought were not necessary.... but if you still want me to put them ...just let me know...........
i still could not find ways of working around the problem...since its imperative for me to create the window only when the button is clicked....the keyboard response has become very slow that i have to wait over 20secs for it to respond......this only happens after the second window has been created an destroyed....on first run(ie when its just the first window running) the keyboard works just fine.

Problem Description
what i want to achieve is to run my program which then creates the first window....then click a run anmation button which has the buttoncallback8 to start the animation.....if i then press esc to close the second window i want to be able to continue using both mouse and key function on window1.....but so far all i can do is create and destroy the second window(the animation also runs when the button is clicked which is fine)...but after destroying the second window i cant use the first effectively as the keyboard response slows down tremendously ...i have to wait over 20sec......i think the problem might be from my approach but i cant find an alternative method that will give the results i want...........i hope my description was better

thanks for your help.&nbsp;

Share this post


Link to post
Share on other sites
There are some, perhaps vital, callbacks that aren't shown, and the callback TheButtonCallback where you create the secondary window is not referenced anywhere. You're destroying the window at different places, and after destroying the window at the different places you set the window id to zero, one, and leave it uninitialized, respectively, and your logic on some places depends on the value of window2 to identify a window or not. You have at least two different idle functions, and you also clear the idle function at some place. It is, as you perhaps can tell, quite difficult to follow your program.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this