Jump to content
  • Advertisement
Sign in to follow this  
kirk86

OpenGL How can i move a fractal when mouse is moved. Anybody plz

This topic is 2590 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 everybody, i have created a fractal and iam trying to create a movement effects by taking the mouse x,y positions and feeding it to gluOrtho2D so when you move your mouse with the left btn down and drag it onto the window then the fractal should follow that movement. Here is what i have done so far. Thank you all in advance. Any help or suggestions would be more that appreciated. I am a newbie with openGL so you have to bare with me. Thanks again.








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




int rotate = 0, old_x = 0, old_y = 0, anglex = 0, angley = 0;
bool btnDown
= false;




void myinit(void)
{
/* attributes */
glClearColor(0.0,
0.0, 0.0, 1.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /*
draw in red */




/* set up viewing */ /* 500 x 500 window with origin lower left
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,
500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}




void myReshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w,
(GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity
();
gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);




}




void display( void )
{
/* define a point data type
*/
typedef GLfloat point[2];




point vertices[6] = { {200.0, 30.0}, {100.0, 100.0}, {100.0, 250.0},
{200.0, 310.0}, {300.0, 250.0}, {300.0, 100.0} };
int j,
k;

point p = { 200.0, 100.0 };




glClear(GL_COLOR_BUFFER_BIT); /*clear the window
*/




/* compute and plots 50000 new points */
for(k = 0; k <
50000; k++)

{
j = rand() % 6; /* pick a
vertex at random */




/* Compute point 1/3 between selected vertex and old point
*/
p[0] = ( p[0] + vertices[j][0] ) / 3.0;

p[1] = ( p[1] +
vertices[j][1] ) / 3.0;




/* plot new point
*/

glBegin(GL_POINTS);
glVertex2fv(p);

glEnd();
}
//glFlush();
/* clear buffers */
glutSwapBuffers();

//glutPostRedisplay();
}




void drawFractal( int plotNumbers )
{
typedef GLfloat
point[2];




point vertices[6] = { {200.0, 30.0}, {100.0, 100.0}, {100.0, 250.0},
{200.0, 310.0}, {300.0, 250.0}, {300.0, 100.0} };
int j,
k;

point p = { 200.0, 100.0 };




glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,
500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glColor3ub(
(char) rand()%256, (char) rand()%256, (char) rand()%256 );




for(k = 0; k < plotNumbers; k++)

{
j = rand() % 6;




p[0] = ( p[0] + vertices[j][0] ) / 3.0;

p[1] = ( p[1] +
vertices[j][1] ) / 3.0;





glBegin(GL_POINTS);
glVertex2fv(p);

glEnd();
}
glutSwapBuffers();

glutPostRedisplay();
}




void mouse(int btn, int state, int x, int
y)
{
switch(btn)
{
case
GLUT_LEFT_BUTTON:
if (state ==
GLUT_DOWN)
{
old_x =
x;
old_y = y;
rotate =
1;
btnDown = true;
}

break;
}
}




void motion(int x, int y)
{
//calculate rotation angles due to
mouse action
if(btnDown){
anglex = (anglex + (x -
old_x));
angley = (angley + (y - old_y));
old_x
= x;
old_y =
y;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,
(GLdouble) anglex, 0.0, (GLdouble)
angley);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();

}
}




void menu(int id)
{
switch(id)
{
case
0:
drawFractal(100000);
break;
case
1:
drawFractal(200000);
break;
case
2:
drawFractal(400000);
break;
case
3:
exit(0);
break;
}
}




int main(int argc, char** argv)
{
glutInit(&argc,
argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB |
GLUT_DEPTH); /* default, not needed */

glutInitWindowSize(500, 500); /* 500 x 500 pixel window */

glutInitWindowPosition(0, 0); /* place window top left on display
*/
glutCreateWindow("Fractal"); /* window title
*/

glutReshapeFunc(myReshape);
glutDisplayFunc(display); /* display
callback invoked when window opened
*/
glutMouseFunc(mouse);
glutMotionFunc(motion);

myinit(); /* set attributes */




glutCreateMenu(menu);
glutAddMenuEntry("100.000
??????", 0);
glutAddMenuEntry("200.000 ??????",
1);
glutAddMenuEntry("400.000 ??????",
2);
glutAddMenuEntry("???????????",
3);
glutAttachMenu(GLUT_RIGHT_BUTTON);




glutMainLoop(); /* enter event loop */




return 0;
}


Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!