PDA

View Full Version : Mouse does not update



mapers
12-04-2010, 12:42 AM
I'm trying to build a coordinate axis so that when you click on the screen and if you release the mouse generates a line in squares are supposed to pixels in zom ... but I upgraded to see if I can help thanks


//************************************************** *********************//
//************************************************** *********************//
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
#include <iostream.h>
#define ancho 600
#define alto 600
#define profundidad 500

int x_inicial;
int y_inicial;
int colores[600* 600];

//************************************************** *********************//
//************************************************** *********************//
void dibuja()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glBegin(GL_LINES);
glColor3f(1, 0, 0);
for(int i=0;i<=800;i=i+10)
{

glVertex2i(0,i);

glVertex2i(800,i );
}


glEnd();

//================================================== ========================
//================================================== ========================
glBegin(GL_LINES);
glColor3f(1, 0, 0);
for(int i=0;i<=800;i=i+10)
{

glVertex2i(i,800);

glVertex2i(i,0);
}


glEnd();

glFlush();
glPopMatrix(); // Recupera la matriz guardada en el stack
glutSwapBuffers();

}


//************************************************** *********************//
//************************************************** *********************//


void ControlRaton(int button, int state, int x, int y)

{

float t1=0,t2=0,b1=0,b2=0;
int b3=0,b4=0;



if(button==GLUT_LEFT_BUTTON &amp;&amp; state==GLUT_DOWN)
{

t1=x/10.0;
t2=y/10.0;
b1=round(t1)*10;
b2=round(t2)*10;
b3=b1;
b4=b2;
//================================================== ========================
//================================================== ========================
x_inicial=b3;
y_inicial=b4;

}

glutPostRedisplay();
}


//************************************************** *********************//
//************************************************** *********************//



void draw_pixel(int ix, int iy)
{
float t1=0,t2=0,b1=0,b2=0;
int b3=0,b4=0;

glColor3f (1.0, 1.0, 1.0);
glBegin(GL_QUADS);
t1=ix/10.0;
t2=iy/10.0;
b1=round(t1)*10;
b2=round(t2)*10;
b3=b1;
b4=b2;
glVertex2f(b3,b4);
glVertex2f(b3,b4-10);
glVertex2f(b3+10,b4-10);
glVertex2f(b3+10,b4);
glEnd();

}
//************************************************** *********************//
//************************************************** *********************//


void bres(

int x2,
int y2)
{

int x1=x_inicial;
int y1=y_inicial;


signed char ix;
signed char iy;

// if x1 == x2 or y1 == y2, then it does not matter what we set here
int delta_x = (x2 > x1?(ix = 1, x2 - x1):(ix = -1, x1 - x2)) << 1;
int delta_y = (y2 > y1?(iy = 1, y2 - y1):(iy = -1, y1 - y2)) << 1;



if (delta_x >= delta_y)
{
// error may go below zero
int error = delta_y - (delta_x >> 1);

while (x1 != x2)
{
if (error >= 0)
{
if (error || (ix > 0))
{
y1 += iy;
error -= delta_x;
}
// else do nothing
}
// else do nothing

x1 += ix;
error += delta_y;
draw_pixel(x1,y1);


}
}
else
{
// error may go below zero
int error = delta_x - (delta_y >> 1);

while (y1 != y2)
{
if (error >= 0)
{
if (error || (iy > 0))
{
x1 += ix;
error -= delta_y;
}
// else do nothing
}
// else do nothing

y1 += iy;
error += delta_x;
draw_pixel(x1,y1);



}
}



}
//************************************************** *********************//
//************************************************** *********************//

void passivemotion( int x, int y )
{

glDrawPixels(ancho, alto, GL_LUMINANCE, GL_FLOAT, colores);
float t1=0,t2=0,b1=0,b2=0;
int b3=0,b4=0;
t1=x/10.0;
t2=y/10.0;
b1=round(t1)*10;
b2=round(t2)*10;
b3=b1;
b4=b2;
//================================================== ========================
//================================================== ========================
bres( b3, b4);
glutPostRedisplay();
}


//************************************************** *********************//
//************************************************** *********************//

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

glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB);
glutInitWindowPosition(100, 0);
glutInitWindowSize(ancho, alto);
glutCreateWindow("Sistema de Coordenadas");
glOrtho(0, 600,600, 0, -profundidad, profundidad);
glClearColor(0, 0, 0, 0);
glutDisplayFunc(dibuja);
//================================================== ========================
glutMouseFunc(ControlRaton);
//================================================== ========================
glutMotionFunc(&amp;passivemotion);
//================================================== ========================

glutMainLoop();
return 0;
}


//************************************************** *********************//
//************************************************** *********************//

marshats
12-04-2010, 07:42 PM
requesting a new GL drawing update or having any GL draw commands with every mouse callback is not a well controlled approach ie in ControlRaton and passivemotion you should not call glutPostRedisplay or bres. Basically I recommend having a "display" callback that encapsulates only GL drawing commands and an idle callback that controls when to actually do glutPostRedisplay. Here's is a modified version of your code that explains this better


#include <windows.h>
#include <GL/glut.h>
#include <math.h>
#include <iostream.h>
#define ancho 600
#define alto 600
#define profundidad 500

void bres( int x2, int y2);
int x_inicial;
int y_inicial;
int colores[600* 600];

int b3=0,b4=0;

void idle()
{
static int b3_last = 0;
static int b4_last = 0;
if (b3_last!=b3 ||b4_last!=b4 ) {
b3_last=b3;
b4_last=b4;
glutPostRedisplay(); // only request redisplay if something changed
}
};
//************************************************** *********************//
//************************************************** *********************//
void dibuja()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glBegin(GL_LINES);
glColor3f(1, 0, 0);
for(int i=0;i<=800;i=i+10)
{

glVertex2i(0,i);

glVertex2i(800,i );
}

glEnd();

//================================================== ========================
//================================================== ========================
glBegin(GL_LINES);
glColor3f(1, 0, 0);
for(int i=0;i<=800;i=i+10)
{

glVertex2i(i,800);

glVertex2i(i,0);
}

glEnd();

glFlush();
glPopMatrix(); // Recupera la matriz guardada en el stack
bres( b3, b4);
glutSwapBuffers();

}


//************************************************** *********************//
//************************************************** *********************//

void ControlRaton(int button, int state, int x, int y)

{

float t1=0,t2=0,b1=0,b2=0;
int b3=0,b4=0;

if(button==GLUT_LEFT_BUTTON &amp;&amp; state==GLUT_DOWN)
{

t1=x/10.0;
t2=y/10.0;
b1=round(t1)*10;
b2=round(t2)*10;
b3=b1;
b4=b2;
//================================================== ========================
//================================================== ========================
x_inicial=b3;
y_inicial=b4;

}

//glutPostRedisplay();
}


//************************************************** *********************//
//************************************************** *********************//

void draw_pixel(int ix, int iy)
{
float t1=0,t2=0,b1=0,b2=0;
int b3=0,b4=0;

glColor3f (1.0, 1.0, 1.0);
glBegin(GL_QUADS);
t1=ix/10.0;
t2=iy/10.0;
b1=round(t1)*10;
b2=round(t2)*10;
b3=b1;
b4=b2;
glVertex2f(b3,b4);
glVertex2f(b3,b4-10);
glVertex2f(b3+10,b4-10);
glVertex2f(b3+10,b4);
glEnd();

}


//************************************************** *********************//
//************************************************** *********************//

void bres(

int x2,
int y2)
{

int x1=x_inicial;
int y1=y_inicial;

signed char ix;
signed char iy;

// if x1 == x2 or y1 == y2, then it does not matter what we set here
int delta_x = (x2 > x1?(ix = 1, x2 - x1):(ix = -1, x1 - x2)) << 1;
int delta_y = (y2 > y1?(iy = 1, y2 - y1):(iy = -1, y1 - y2)) << 1;

if (delta_x >= delta_y)
{
// error may go below zero
int error = delta_y - (delta_x >> 1);

while (x1 != x2)
{
if (error >= 0)
{
if (error || (ix > 0))
{
y1 += iy;
error -= delta_x;
}
// else do nothing
}
// else do nothing

x1 += ix;
error += delta_y;
draw_pixel(x1,y1);

}
}
else
{
// error may go below zero
int error = delta_x - (delta_y >> 1);

while (y1 != y2)
{
if (error >= 0)
{
if (error || (iy > 0))
{
x1 += ix;
error -= delta_y;
}
// else do nothing
}
// else do nothing

y1 += iy;
error += delta_x;
draw_pixel(x1,y1);

}
}

}


//************************************************** *********************//
//************************************************** *********************//

void passivemotion( int x, int y )
{

//glDrawPixels(ancho, alto, GL_LUMINANCE, GL_FLOAT, colores);
float t1=0,t2=0,b1=0,b2=0;
//int b3=0,b4=0;
t1=x/10.0;
t2=y/10.0;
b1=round(t1)*10;
b2=round(t2)*10;
b3=b1;
b4=b2;
//================================================== ========================
//================================================== ========================
//bres( b3, b4);
//glutPostRedisplay();
}


//************************************************** *********************//
//************************************************** *********************//

int main(int argc, char** argv)
{
glutInit(&amp;argc,argv);

glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB);
glutInitWindowPosition(100, 0);
glutInitWindowSize(ancho, alto);
glutCreateWindow("Sistema de Coordenadas");
glOrtho(0, 600,600, 0, -profundidad, profundidad);
glClearColor(0, 0, 0, 0);
glutDisplayFunc(dibuja);
glutIdleFunc(idle);
//================================================== ========================
glutMouseFunc(ControlRaton);
//================================================== ========================
glutMotionFunc(&amp;passivemotion);
//================================================== ========================

glutMainLoop();
return 0;
}