Jump to content
  • Advertisement
Sign in to follow this  
subharb

mousefunction(mouse) doesnt Work!!

This topic is 4028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, again, let´s see if u can solve this problem i have.I want to draw a triangle when there´s a left click, but nothing happens, those lines are executed, but nothing happens, is it something with the buffers or the location of the camera?? Help!! #include <windows.h> #include <GL/glut.h> #include <stdio.h> // Para cada objeto que vamos a representar necesitamos una Matrix, que va a //va a reprensetar los puntos x,y,z que ocupa el objeto en el espacio float ang = 0.0f; int max = 64; void mouse(int btn, int state, int x, int y){ if((btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)){ //glTranslatef(0.0f,1.0f,0.0f); glBegin(GL_TRIANGLES); glColor3f(2.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); } } void draw(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //Carga la matriz identidad gluLookAt(0.0f, 0.0f, 4.0f, //eye 0.0f, 0.0f, 0.0f, //center 0.0f, 1.0f, 0.0f);//up*/ //glLoadIdentity(); //Carga la matriz identidad glColor3f(8.0f,0.0f,0.0f); glPushMatrix(); glTranslatef(-1.5f,0.0f,0.0f); glRotatef(ang,1.0f,0.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW ) // Move Left 1.5 Units And Into The Screen 6.0 glBegin(GL_TRIANGLES); glColor3f(0.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); glPopMatrix(); //glPushMatrix(); //glLoadIdentity(); //porque no funciona??? //glLoadMatrix(m); //glRotatef(-ang,1.0f,0.0f,0.0f); glPushMatrix(); glTranslatef(1.5f,0.0f,0.0f); glRotatef(ang,0.0f,1.0f,0.0f); //glPopMatrix(); // Move Right 3 Units glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); glPopMatrix(); glFlush(); // Asegura que los comandos anteriores se vana ejecutar ang+=0.2f; } void display(void) { int i; GLenum err; glClear(GL_ACCUM_BUFFER_BIT); //Vacia ese buffer, porque es posible que tenga residuos for (i=0; i<max; i++) { draw(); glAccum(GL_ACCUM, 1.f/max); } glAccum(GL_RETURN, 1.f); glutSwapBuffers(); } void reshape(int w, int h) { glMatrixMode(GL_PROJECTION); //Con esta función se elige en que matriz vamos a realizar los cambios glLoadIdentity(); gluPerspective(55.0f, (float)w/(float)h, 0.3f, 5.0f); glViewport(0,0,w,h); glMatrixMode(GL_MODELVIEW); } void idle(void) { glutPostRedisplay(); } void init(void) { glReadBuffer( GL_BACK); glDrawBuffer( GL_BACK); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearAccum(0.0f, 0.0f, 0.0f, 1.0f); glClearDepth(1.0f); } int main(int argc, char *argv[]) { // inizializzo la libreria glutInit(&argc, argv); // definisco le proprieta della finestra e del display glutInitWindowPosition(16, 16); glutInitWindowSize(512, 512); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_ACCUM); // creo la fienstra glutCreateWindow("OpenGL Test"); // registro le callback glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); init(); glutMouseFunc (mouse); //estoy probando con el ratón // loop infinito glutMainLoop(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
The triangle is being drawn, but then your draw function gets called and clears the display. You could try setting a flag when the user clicks (and save the click location), then check the flag in your draw function and draw the triangle to the saved location there. (Be sure to reset the flag after drawing.)

Share this post


Link to post
Share on other sites
I'm not sure, maybe you're using some weird implementation of OpenGL or something, but as far as I know, the color values in glColorXY function calls should be in the range of [0.0f; 1.0f]. Now, in your code I see a lot of calls, where you pass values like 2.0f, 8.0f etc... I don't know, maybe OpenGL gets confused what color to use, and it just uses the default color (which might be the same as the background color), so you don't really see anything on screen? Just guessing here...

Share this post


Link to post
Share on other sites
You're using double buffering, so you're drawing the triangle to the back buffer. Instead of drawing things immediately, save the coordinates to a triangle list or something and draw everything in the display callback. Or in this trivial case, as Dave Hunt suggested, use a flag to indicate if the mouse is pressed or not, and read it from the display function. But even if you draw it to the front buffer and make it show up, it will be erased as soon as GLUT issues a new callback to the display function because it doesn't know about the triangle in the mouse callback.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!