PDA

View Full Version : 2D Lines & Resizing !!!



Vasilis
10-24-2000, 02:30 PM
How can you choose the colour of the lines if you join two points with coordinates (x1,y1) and (x2,y2) ??

Also how can you keep the aspect ratio if you have a square or a shape and u resize the drawing window?

Will the mouse pointer oblige to the aspect ratio as well?

Can you please tell mewhich functions to use or show me a code example?

Thanks for your time and help...
Vasilis

Antonio
10-24-2000, 11:55 PM
The line color is the same as the other graphical primitives color. Just use glColor3f(r,g,b);

As for reshape, here goes the source code I use in my tutorial:

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
glMatrixMode(GL_MODELVIEW);


}

As for the mouse pointer, I don't have a clue about what you mean, could you expand a little?

Antonio www.fatech.com/tech (http://www.fatech.com/tech)

Vasilis
10-29-2000, 03:28 PM
Right...let me show u my code..

=========================================

#include <windows.h> /* Windows Library */
#include <gl\gl.h> /* OpenGL Library */
#include <gl\glu.h> /* OpenGL Utility Library */
#include <gl\glut.h> /* Glut Library */
#include <math.h> /* Maths Library */

int down = 0;
int true = 1;
int false = 0;
int brush = 5;
int mx,my = 0;


void line(int x1, int y1, int x2, int y2){
glBegin(GL_LINES);
glColor3f(0.0,0.0,0.0);
glLineWidth(5.0); //set the line width
glVertex2f(x1,y1); //draw point A
glVertex2f(x2,y2); //draw point B
glEnd();
}


void square(int x, int y, int z, float c1, float c2, float c3){ //x and y are top left co-ordinates z is the size and c is the color
glBegin(GL_POLYGON);
glColor3f(c1,c2,c3);
glVertex3f(x,y,0);
glVertex3f(x+z,y,0);
glVertex3f(x+z,y-z,0);
glVertex3f(x,y-z,0);
glEnd();
}


void whitesquare(int x, int y, int z, float c1, float c2, float c3){ //x and y are top left co-ordinates z is the size and c is the color of the perimeter
glBegin(GL_LINE_LOOP);
glColor3f(c1,c2,c3);
glVertex3f(x,y,0);
glVertex3f(x+z,y,0);
glVertex3f(x+z,y-z,0);
glVertex3f(x,y-z,0);
glEnd();
}


void plussquare(int x, int y, int z, float c1, float c2, float c3){ //x and y are top left co-ordinates z is the size and c is the color of the perimeter
glBegin(GL_LINE_LOOP);
glColor3f(c1,c2,c3);
glVertex3f(x,y,0);
glVertex3f(x+z,y,0);
glVertex3f(x+z,y-z,0);
glVertex3f(x,y-z,0);
glEnd();
line(x+(z*3/10),y-(z/2),x+(z*8/10),y-(z/2)); //this draws the plus sign within an empty square
line(x+5,y-(z*8/10),x+5,y-(z*2/10));
}


void minussquare(int x, int y, int z, float c1, float c2, float c3){ //x and y are top left co-ordinates z is the size and c is the color of the perimeter
glBegin(GL_LINE_LOOP);
glColor3f(c1,c2,c3);
glVertex3f(x,y,0);
glVertex3f(x+z,y,0);
glVertex3f(x+z,y-z,0);
glVertex3f(x,y-z,0);
glEnd();
line(x+(z*3/10),y-(z/2),x+(z*8/10),y-(z/2)); //this draws the minus sign within an empty square
}


void circle(int xcentre, int ycentre, float size){
float radius = size;
float angle, angle_increment;
float next_x, next_y;
int i;

angle_increment=(3.14159*2/100);
angle = 0;

glBegin(GL_POLYGON);
for(i=0;i<=100;i++){
angle = angle + angle_increment;
next_x = radius * cos(angle);
next_y = radius * sin(angle);
next_x = next_x + xcentre;
next_y = next_y + ycentre;
glVertex2f(next_x,next_y);
}
glEnd();
}


void mouse(int button, int state, int x, int y){
if(button!=GLUT_LEFT_BUTTON &#0124; &#0124; state==GLUT_UP)
down=false;
else
down=true;

if(x <= 20 && x >= 10 && (399-y) <= 370 && (400-y) >= 360){
glColor3f(1.0,1.0,1.0);
square(10,10,10,1.0,1.0,1.0);
}else if(x <= 20 && x >= 10 && (399-y) <= 350 && (399-y) >= 340){
glColor3f(1.0,0.0,0.0);
square(10,10,10,1.0,0.0,0.0);
}else if(x <= 20 && x >= 10 && (399-y) <= 330 && (399-y) >= 320){
glColor3f(0.0,1.0,0.0);
square(10,10,10,0.0,1.0,0.0);
}else if(x <= 20 && x >= 10 && (400-y) <= 310 && (399-y) >= 300){
glColor3f(0.0,0.0,1.0);
square(10,10,10,0.0,0.0,1.0);
}else if(x <= 20 && x >= 10 && (400-y) <= 290 && (399-y) >= 280){
brush = brush + 1;
}else if(x <= 20 && x >= 10 && (400-y) <= 270 && (399-y) >= 260){
if(brush>1)
brush = brush - 1;
}else circle(x,399-y,brush);
}


void motion(int x, int y){
if(down==true){
if(x <= 20 && x >= 10 && (399-y) <= 370 && (400-y) >= 360){
glColor3f(1.0,1.0,1.0);
square(10,10,10,1.0,1.0,1.0);
}else if(x <= 20 && x >= 10 && (399-y) <= 350 && (399-y) >= 340){
glColor3f(1.0,0.0,0.0);
square(10,10,10,1.0,0.0,0.0);
}else if(x <= 20 && x >= 10 && (399-y) <= 330 && (399-y) >= 320){
glColor3f(0.0,1.0,0.0);
square(10,10,10,0.0,1.0,0.0);
}else if(x <= 20 && x >= 10 && (400-y) <= 310 && (399-y) >= 300){
glColor3f(0.0,0.0,1.0);
square(10,10,10,0.0,0.0,1.0);
}else if(x <= 20 && x >= 10 && (400-y) <= 290 && (399-y) >= 280){
brush = brush + 1;
}else if(x <= 20 && x >= 10 && (400-y) <= 270 && (399-y) >= 260){
if(brush>1)
brush = brush - 1;
}else circle(x,399-y,brush);
mx=x;
my=y;
}
}


void keyboard(unsigned char key, int x, int y){ //change the size of the brush, clear the display from all drawings and exit the program
if(key=='c')
display();
else if(key==27)
exit(0);
}


void display(void){
glClear(GL_COLOR_BUFFER_BIT);
line(25,0,25,399);
whitesquare(10,370,10,0.0,0.0,0.0);
square(10,350,10,1.0,0.0,0.0);
square(10,330,10,0.0,1.0,0.0);
square(10,310,10,0.0,0.0,1.0);
plussquare(10,290,10,0.0,0.0,0.0);
minussquare(10,270,10,0.0,0.0,0.0);
square(10,10,10,0.0,0.0,1.0); //shows the selected colour at the time
glFlush();
}

//-------------------------------------------------------------------------------------------------------------------------------------

void init(void){
glClearColor(0.5,0.5,0.5,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,399.0,0.0,399.0,-100.0,100.0);
}


int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(200,200);
glutCreateWindow("Vas' Paint Program");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0 ;
}

=========================================

This is a painting program, one of my first ones, and you can choose some colours to paint with, as well as adjust the size of the brush..

My problem is that when I resize the window, the brush is not where the actual mouse pointer is. I did try to implement the resize function as seen in your tutorial, but I had no luck.

Also, can anyone suggest how to prevent painting over the left hand side of the black line??
The way I tried to calculate it, was to say if the x(where the mouse points) + the radius of the brush is greater than the x co-ordinate of the black line do nothing, else draw....but it still won't work...ay other way?

Thanks for your help and time...and patience..

Vasilis