Dabar Parihar
Dabar Parihar

Reputation: 77

Scaling line in OpenGL

I have two lines in VC++

glBegin(GL_LINES);
//1st line
glVertex2d(20, 20);
glVertex2d(20, 80);
//2nd line to be scaled
glScaled(2,2,2); 
glVertex2d(50, 20);
glVertex2d(50, 80);
glEnd();

I want to double the length of second line by using glScaled(2,2,2).How can I do it using openGL in VC++.

Upvotes: 0

Views: 675

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32637

You shouldn't change matrices between glBegin() and glEnd(). This block is just supposed to specify vertex data.

glBegin(GL_LINES);
//1st line
glVertex2d(20, 20);
glVertex2d(20, 80);
glEnd();

glPushMatrix();
glScaled(2,2,2); 
glBegin(GL_LINES);    
//2nd line to be scaled
glVertex2d(50, 20);
glVertex2d(50, 80);
glEnd();
glPopMatrix(); //used to restore the unscaled matrix

Note that the entire line is scaled (both start and end point). If you just want to scale with the start point being the origin, you can adapt the transform as follows:

glTranslated(50, 20, 0);    
glScaled(2,2,2); 
glTranslated(-50, -20, 0); 

Or simply:

glTranslated(50, 20, 0);    
glScaled(2,2,2); 
//...
glVertex2d(0, 0);
glVertex2d(0, 60);

Upvotes: 3

Related Questions