PDA

View Full Version : paint project plz help



maxvo
05-13-2009, 02:41 PM
#include<iostream.h>
#include<glut.h>

#include<math.h>

const float DEG2RAD = 3.14159/180;
static GLint posX = 0, posY = 0;
int n1=0,n2=0;
int n=0;


int rad1_get(int x)
{
int x1=x-n1;
return x1;
}
int rad2_get(int y)
{
int y1=y-n2;
return y1;
}
int C1(int x)
{
int x1=.5*n1+.5*x;
return x1;

}
int C2(int y)
{
int y1=.5*n2+.5*y;
return y1;
}
void circle(int x,int y)
{
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_LOOP);

for (int theta=0; theta<360; theta++)
{
float degInRad = theta*DEG2RAD;
glVertex3f(cos(degInRad)*rad1_get(x) +C1(y), sin(degInRad)*rad2_get(y)+C2(x),5);



}
glEnd();
//glFlush();

}
void copy_buffer()
{
static GLint viewport[4];
static GLfloat raster_pos[4];

glGetIntegerv(GL_VIEWPORT, viewport);

/* set source buffer */
glReadBuffer(GL_FRONT);

/* set projection matrix */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity() ;
gluOrtho2D(0, viewport[2], 0, viewport[3]);

/* set modelview matrix */
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

/* save old raster position */
glGetFloatv(GL_CURRENT_RASTER_POSITION, raster_pos);

/* set raster position */
glRasterPos4f(0.0, 0.0, 0.0, 1.0);

/* copy buffer */
glCopyPixels(0, 0, viewport[2], viewport[3], GL_COLOR);

/* restore old raster position */
glRasterPos4fv(raster_pos);

/* restore old matrices */
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

/* restore source buffer */
glReadBuffer(GL_BACK);
}

void squar(int x,int y)
{
int x1=x-n1;
int y1=y-n2;
glBegin(GL_QUADS);
glVertex2f(n1,n2);
glVertex2f(x,n2);
glVertex2f(x,y);
glVertex2f(n1,y);
glEnd();
glFlush();
}



void MenuRight(int val)
{
/* Push to RIGHT mouse button and Choose "Exit" */
/* exit */
exit(0);
}


void mousefun(int button, int state, int x, int y)
{






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

posX=x;
posY=y;
n1=x;
n2=y;
//glDrawBuffer(GL_FRONT);
cout<<"front"<<endl;



}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{

//glClear(GL_COLOR_BUFFER_BIT);
//glutSwapBuffers();
//squar(x,y);
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{



}
if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
{
glutSwapBuffers();


}





}
void clear(int x,int y)
{

glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(n1,n2);
glVertex2f(x,y);

glEnd();
glFlush();
cout<<"$"<<n1<<endl;

}
void Line(int x,int y)
{
glColor3f(1.0,1,1.0);
glBegin(GL_LINES);
glVertex2f(posX,posY);
glVertex2f(x,y);
glEnd();
glFlush();
}


void mouse_motion (int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
circle(x,y);
glEnd();
glFlush();
}

void mydisplay()
{
glOrtho(0, 300, 300, 0,-10,10);




glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glDrawBuffer(GL_FRONT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
;



glFlush();

// glutPostRedisplay();
//glutSwapBuffers();
}


int main ()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutCreateWindow("assignment 1");

glutDisplayFunc(mydisplay);
glutMotionFunc(mouse_motion);

glutMouseFunc(mousefun);
glutMainLoop();

return 1;
}

when i draw any circle the privous one is gone.......
how can i fix this?