[GtkGLExt] Problem with glUnProject



hello everybody 

I am doing application in which I need raycast, and I'm using glUnProject, what is strange is that when only use glut it works perfectly, but when I use gtk does not work, the values that I return to x and y are very small between 0 and 0.5 . 

The code is just the same as in gtk, I does not know what happening, any suggestions?


////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
Source for GLUT
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <GL/glut.h>

#include <stdlib.h>

#include <stdio.h>


void Display();
void Reshape(int w,int h);
void Mouse(int button,int state,int x,int y);
GLdouble ox=0.0,oy=0.0,oz=0.0;

double xp0[3],xp1[3];

int main(int argc,char **argv) {
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
	glutInitWindowSize(800,600);
	glutCreateWindow("dfshkfjdfk");
	glClearColor(1,1,1,1);
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutMouseFunc(Mouse);

	glutMainLoop();
	return 0;
}


void Display() {
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt( 0,0,10,
		0,0,0,
		0,1,0);

	glColor3f(0,0,0);
	glPushMatrix();
	{
	glTranslated(ox,oy,oz);
	glutSolidSphere(0.5,15,15);
	}
	glPopMatrix();

	glPushMatrix();
	{
		glBegin(GL_LINES);
		glColor3f(1,0,0);
		glVertex3f(0,0,0);
		glVertex3f(10,0,0);

		glColor3f(0,1,0);
		glVertex3f(0,0,0);
		glVertex3f(0,10,0);

		glColor3f(0,0,1);
		glVertex3f(0,0,0);
		glVertex3f(0,0,10);

		glVertex3f(xp0[0],xp0[1],xp0[2]);
		glVertex3f(xp1[0],xp1[1],xp1[2]);

		glEnd();
	}
	glPopMatrix();

	glFlush();
}

void Reshape(int w,int h) {
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0,(float)w/(float)h,5.0,30.0);
	glMatrixMode(GL_MODELVIEW);
}

void Mouse(int button,int state,int x,int y) {
	GLint viewport[4];
	GLdouble modelview[16],projection[16];
	GLfloat wx=x,wy,wz;

	printf("\nx=%i y=%i",x,y);

	if(state!=GLUT_DOWN)
		return;
	if(button==GLUT_RIGHT_BUTTON) exit(0);

	glGetIntegerv(GL_VIEWPORT,viewport);
	y=viewport[3]-y;
	wy=y;
	glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
	glGetDoublev(GL_PROJECTION_MATRIX,projection);
	//glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
	gluUnProject(wx,wy,0,modelview,projection,viewport,&ox,&oy,&oz);
	
	

	xp0[0]=ox;
	xp0[1]=oy;
	xp0[2]=oz;


	glutPostRedisplay();

	
	printf("\nx=%3.3f y=%3.3f z=%3.3f",xp0[0],xp0[1],xp0[2]);


}



////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
End Source for GLUT
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
Source for GTK
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

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

#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <gdk/gdkgl.h>

#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif

#include <GL/gl.h>
#include <GL/glu.h>
#include <conio.h>

#include <GL/glut.h>

void DrawScene ();

int destroyProgram(GtkWindow *, gpointer );
GdkGLConfig *configure_gl (void);
GtkWidget *intDrawArea(GdkGLConfig *);

gboolean configure_event (GtkWidget *,GdkEventConfigure *,gpointer );
gboolean expose_event (GtkWidget *,GdkEventExpose *,gpointer );
void init(GtkWidget *,gpointer );
void inicializar();
void drawWorld(void);

gboolean picking(GtkWidget *widget,GdkEventButton *event, gpointer data);

GLdouble ox=0.0,oy=0.0,oz=0.0;

void initNewton();
void DrawScene ();

float x,y;



int main(int argc, char *argv[]){

	GtkWidget *window;
	GtkWidget *drawing_area;
	GdkGLConfig *glconfig;

	//inicializamos GTK
	gtk_init(&argc, &argv);

	//Inicializamos los atributos de opengl
	gtk_gl_init (&argc, &argv);

	//Creamos la ventana de la aplicacion tamaño y titulo
	window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW (window),"Draw Robot.rbt");
	gtk_window_set_default_size (GTK_WINDOW (window),800,600);
	gtk_container_set_reallocate_redraws (GTK_CONTAINER (window), TRUE);
	g_signal_connect (G_OBJECT (window), "delete_event",G_CALLBACK (destroyProgram), NULL);


	//Area de dibujo
	glconfig=configure_gl();
	drawing_area =intDrawArea(glconfig);
	gtk_container_add (GTK_CONTAINER (window), drawing_area);
	gtk_widget_show (drawing_area);


	gtk_widget_show_all(window);
	gtk_main();
	return 0;
}


int destroyProgram(GtkWindow *widget, gpointer data){

	gtk_main_quit();


	return 0;
}


GtkWidget *intDrawArea(GdkGLConfig *glconfig){
	GtkWidget *drawing_area;
	drawing_area=gtk_drawing_area_new ();

	gtk_widget_add_events (drawing_area,
		GDK_LEAVE_NOTIFY_MASK |
		GDK_ENTER_NOTIFY_MASK |
		GDK_KEY_PRESS_MASK |
		GDK_KEY_RELEASE_MASK |
		GDK_POINTER_MOTION_MASK |
		GDK_POINTER_MOTION_HINT_MASK |
		GDK_BUTTON1_MOTION_MASK | 
		GDK_BUTTON2_MOTION_MASK | 
		GDK_BUTTON_PRESS_MASK);

	//gtk_widget_set_usize(drawing_area,50,50);
	gtk_widget_set_size_request (drawing_area,200,200);

	//Para que la area de dibujo pueda utilizar opengl
	gtk_widget_set_gl_capability (drawing_area,glconfig,NULL,TRUE,GDK_GL_RGBA_TYPE);

	//Evento que se lanza cuando se necesita redibujar
	g_signal_connect (G_OBJECT (drawing_area), "expose_event",G_CALLBACK (expose_event), NULL);

	//Evento que s elanza cuando una ventana cambia de tamaño
	g_signal_connect (G_OBJECT (drawing_area), "configure_event",G_CALLBACK (configure_event), NULL);


	//g_signal_connect (G_OBJECT (drawing_area), "motion_notify_event",G_CALLBACK (picking), NULL);
	g_signal_connect (G_OBJECT (drawing_area), "button_press_event",G_CALLBACK (picking), NULL);

	//INicializa las texturas
	g_signal_connect_after (G_OBJECT (drawing_area), "realize", G_CALLBACK (init), NULL);


	return drawing_area;
}



void init(GtkWidget *widget,gpointer data){

	GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

	//OpenGL BEGIN
	if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
		return;



	inicializar();


	gdk_gl_drawable_gl_end (gldrawable);
	/*** OpenGL END ***/
}


void inicializar() {

	glShadeModel(GL_SMOOTH);       // Esto hace que al dibujar las sombran en los objetos, se vean mas parejas haciendo que se vean mejor.
	glClearColor(1.0, 1.0, 1.0, 1.0);       // Pondremos aca la funcion glclearcolor
	// Las 3 lineas siguientes hacen que el depth buffer ordene los objetos que deben ser puestos primero en panatalla y los que deben ser dibujados despues dependiendo de cuan cerca estan de la camara.
	glClearDepth(1.0);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);      // Esto hace que opengl calcule las perspectivas de la mejor forma, quita un poco de rendimiento, pero hace que las perspectivas se vean un poco mejor.
	glEnable(GL_TEXTURE_2D);


}


gboolean configure_event (GtkWidget *widget,GdkEventConfigure *event,gpointer data){
	gboolean flag;
	int width;
	int height;

	//gtk_widget_get_gl_context regresa un GdkGLContext con el apropiado GdkGLDrawable para el widget
	GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
	//gtk_widget_get_gl_drawable regresa el GdkGLDrawable del widget
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

	//Iniciamos openGL para modificarlo
	flag=gdk_gl_drawable_gl_begin (gldrawable, glcontext);
	if (flag){
		//Definimos el viewport
		width=widget->allocation.width;
		height=widget->allocation.height;

		glEnable (GL_DEPTH_TEST);

		glViewport (0, 0,width,height);

		glMatrixMode(GL_PROJECTION);										//Selecciona la matriz de projecciones
		glLoadIdentity();													//se carga la matriz identidad

		gluPerspective(45.0f, (GLfloat) width/ (GLfloat)height, 1.0f,1000.0f); //define el viewport

		glMatrixMode(GL_MODELVIEW);											//Selecionanamos la matriz dde model

		glLoadIdentity();

		//Observador, punto que vemos, hacia arriba
		gluLookAt(0, 0, 20.0,
			0, 0, 0.0,
			0.0, 1.0, 0.0);

		//Cerramos openGL una vez que lo modificamos
		gdk_gl_drawable_gl_end (gldrawable);
	}

	return TRUE;
}



gboolean expose_event (GtkWidget *widget,GdkEventExpose *event,gpointer data){

	//gtk_widget_get_gl_context regresa un GdkGLContext con el apropiado GdkGLDrawable para el widget
	GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
	//gtk_widget_get_gl_drawable regresa el GdkGLDrawable del widget
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

	//Verificamos que podamos iniciar openGL para poder modificarlo
	if (gdk_gl_drawable_gl_begin (gldrawable, glcontext)){

		//Dibujamos el mundo
		glClearColor(1,1,1,0);
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


		DrawScene ();


		//verifica si el gldewable tiene doble buffer
		if (gdk_gl_drawable_is_double_buffered (gldrawable))
			gdk_gl_drawable_swap_buffers (gldrawable);			//intercambia los buffers
		else
			glFlush ();											//si no solo dibujamos 

		//Cierra openGL
		gdk_gl_drawable_gl_end (gldrawable);
	}

	return TRUE;
}


GdkGLConfig *configure_gl (void){
	GdkGLConfig *glconfig;

	//confuguracion de openGL,Modo se seleccionado y doble buffer
	glconfig = gdk_gl_config_new_by_mode (static_cast<GdkGLConfigMode>(GDK_GL_MODE_RGB|GDK_GL_MODE_DOUBLE|GDK_GL_MODE_DEPTH));

	//Verificamos que se pudo configurar openGL
	if (glconfig == NULL){
		g_print ("*** Cannot find the double-buffered visual.\n");
		g_print ("*** Trying single-buffered visual.\n");

		//Try single-buffered visual
		glconfig = gdk_gl_config_new_by_mode (static_cast<GdkGLConfigMode>(GDK_GL_MODE_RGB|GDK_GL_MODE_DEPTH));

		if (glconfig == NULL){
			printf("*** No appropriate OpenGL-capable visual found.\n");
			return false;
		}
	}

	return glconfig;
}


gboolean picking(GtkWidget *widget,GdkEventButton *event, gpointer data){
	GdkModifierType state;

	GLint viewport[4];
	GLdouble modelview[16],projection[16];
	GLfloat wx=x,wy,wz;
	
	GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

	if (gdk_gl_drawable_gl_begin (gldrawable, glcontext)){

		x=event->x;
		y=event->y;
		printf("\nx= %f y= %f",x,y);

		glGetIntegerv(GL_VIEWPORT,viewport);
		y=viewport[3]-y;
		wy=y;
		glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
		glGetDoublev(GL_PROJECTION_MATRIX,projection);
		gluUnProject(wx,wy,0,modelview,projection,viewport,&ox,&oy,&oz);

		gdk_gl_drawable_gl_end (gldrawable);
	}


	printf("\nw = %i h=%i",viewport[2],viewport[3]);
	printf("\nx= %f y=%f z=%f",ox,oy);

	expose_event(widget,NULL,NULL);

	return false;
}

void DrawScene (){

	glPushMatrix();
	glBegin(GL_LINES);
	glColor3f(1,0,0);
	glVertex3f(0,0,0);
	glVertex3f(10,0,0);
	glColor3f(0,1,0);
	glVertex3f(0,0,0);
	glVertex3f(0,10,0);
	glColor3f(0,0,1);
	glVertex3f(0,0,0);
	glVertex3f(0,0,10);
	glEnd();
	glPopMatrix();

	//Renderizamos el objeto
	glColor3f(0,0,0);
	glPushMatrix();
	glTranslated(ox,oy,0);
	gdk_gl_draw_sphere(true,0.5,10,10);;
	glPopMatrix();

}


////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
End Source for GTK
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////




      ¡Todo sobre Amor y Sexo!
La guía completa para tu vida en Mujer de Hoy.                       
http://mx.mujer.yahoo.com/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]