PDA

View Full Version : Gtk and Glut animation



Mao
04-30-2003, 12:36 AM
Hi !
I have made a little glut animation, with a triangle which rotates. And i wanted to put it with a gtk menu.
The probleme is, there is no more rotation. It seems to be, that gtk stop the opengl (glut) loop.
How can I make this animation working ?
I make it with gluut because, later I wanted to make some more complicated thing with it, and use de glui too.
I need to make an animation (glut), with a menu(gtk). I don't very well gtk, and glut is for me the best to make a scene with interactions with the mouse and keyboard, use glui widgets, and etc...
Please help me.

satan
04-30-2003, 02:50 AM
You just should not mix glut with gtk. If you want to have gtk you should use a gtk widget for your rendering context.

Mao
04-30-2003, 03:35 AM
Originally posted by satan:
You just should not mix glut with gtk. If you want to have gtk you should use a gtk widget for your rendering context.

I don't understand. Is it , if I want to use gtk with glut this story of widget ?

Please, give a look at my source, I've tried to make it with 2 threads :

#include <stdio.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include <GL/glut.h>

float angle = 0.0;


//************ Glut Init ****************//
void Initialisation()
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glClearColor(0, 0, 0, 1);
}

void Reshape(int largeur, int hauteur)
{
if(hauteur == 0)
hauteur = 1;

float ratio = 1.0* largeur / hauteur;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, largeur, hauteur);
//gluOrtho2D(-1, 1, -1, 1);
gluPerspective(45,ratio,1,1000);

// Set the correct perspective.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
}

//******* Gl Paint *************//
void Dessin(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotatef(angle,0.0,1.0,0.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0,0,0);glVertex3f(-0.5,-0.5,0.0);
glColor3f(0,1.0,0);glVertex3f(0.5,0.0,0.0);
glColor3f(0,0,1.0);glVertex3f(0.0,0.5,0.0);
glEnd();
glPopMatrix();
angle++;

//glFinish();
glFlush();
glutSwapBuffers();
}

static void Touche(unsigned char key, int x, int y)
{
printf("Touche: %d\n",key);
switch (key)
{
case 27:
exit(0);
break;
}
}

//*************** Glut Function ***************//
void opengl_thread(void)
{
glutInitWindowSize(200,200 );
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("Fenetre OpenGL");
Initialisation();
glutReshapeFunc(Reshape);
glutDisplayFunc(Dessin);
glutKeyboardFunc(Touche);
Initialisation();
glutMainLoop();
}

void cb_ok()
{
printf("Ok Clicked...\n");
}

//********* GTK Interface *********//
int menuInterface(void)
{
GtkWidget *pwindow;

GtkWidget *pMenuBar;
GtkWidget *pMenu;
GtkWidget *pMenuItem;

GtkWidget *pvbox;
//GtkWidget *pBUT_Quit;
//GtkWidget *pBUT_Ok;

pwindow=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(pwindow),"Menu thread !");
gtk_window_set_default_size(GTK_WINDOW(pwindow),20 0,320);
g_signal_connect(G_OBJECT(pwindow),"destroy",G_CALLBACK(gtk_main_quit),NULL);

/* GTKvbox */
pvbox=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(pwindow),pvbox);

/* Menu */
pMenuBar=gtk_menu_bar_new();

pMenu=gtk_menu_new();

pMenuItem=gtk_menu_item_new_with_label("New !");
gtk_menu_shell_append(GTK_MENU_SHELL(pMenu),pMenuI tem);

pMenuItem=gtk_menu_item_new_with_label("Open !");
gtk_menu_shell_append(GTK_MENU_SHELL(pMenu),pMenuI tem);

pMenuItem=gtk_menu_item_new_with_label("Save !");
gtk_menu_shell_append(GTK_MENU_SHELL(pMenu),pMenuI tem);

pMenuItem=gtk_menu_item_new_with_label("Close !");
gtk_menu_shell_append(GTK_MENU_SHELL(pMenu),pMenuI tem);


pMenuItem=gtk_menu_item_new_with_label("Quit !");
g_signal_connect(G_OBJECT(pMenuItem),"activate",G_CALLBACK(gtk_main_quit),(GtkWidget*)pwindow);
gtk_menu_shell_append(GTK_MENU_SHELL(pMenu),pMenuI tem);

pMenuItem=gtk_menu_item_new_with_label("Fichier");

gtk_menu_item_set_submenu(GTK_MENU_ITEM(pMenuItem) ,pMenu);

gtk_menu_shell_append(GTK_MENU_SHELL(pMenuBar),pMe nuItem);

/* Menu to window */
gtk_box_pack_start(GTK_BOX(pvbox),pMenuBar,FALSE,F ALSE,0);

gtk_widget_show_all(pwindow);

return EXIT_SUCCESS;

}

int main(int argc, char **argv)
{
//threads Initialisation...
g_thread_init(NULL);

//Initialisation GTK, GLUT...
gtk_init (&argc, &argv);
glutInit(&argc, argv);

//GTK Window Creation ...
menuInterface();

//threads activation ...
gdk_threads_enter();

//GL and Gtk loops
g_thread_create((GThreadFunc)opengl_thread,NULL,FA LSE,NULL);
gtk_main ();

//quit...
gdk_threads_leave();
return 0;

}

Mao
04-30-2003, 04:18 AM
Probleme solved, I forgot the method glutPostRedisplay() at the end of my scene.
It works now !!!

nexusone
04-30-2003, 04:23 AM
Great if you got it to work, normaly I don't think you should mix the two together.
You should rewrite your openGL animation using only gtk with no glut functions.


Originally posted by Mao:
Hi !
I have made a little glut animation, with a triangle which rotates. And i wanted to put it with a gtk menu.
The probleme is, there is no more rotation. It seems to be, that gtk stop the opengl (glut) loop.
How can I make this animation working ?
I make it with gluut because, later I wanted to make some more complicated thing with it, and use de glui too.
I need to make an animation (glut), with a menu(gtk). I don't very well gtk, and glut is for me the best to make a scene with interactions with the mouse and keyboard, use glui widgets, and etc...
Please help me.



[This message has been edited by nexusone (edited 04-30-2003).]

Mao
04-30-2003, 04:45 AM
Originally posted by nexusone:
Great if you got it to work, normaly I don't think you should mix the two together.
You should rewrite your openGL animation using only gtk with no glut functions.


Why not ? i had the feeling that GTK is fully objectoriented, and it will be too bad that it doesn't works, just for de OS portability. GlutPostRedisplay was for me here a good help, why making a method to force a redisplay, when we don't need it ?
It is practical, when glut should work with other interface, isn't it ?

M@o