问题
I want to draw a line in opengl.
glBegin(GL_LINES);
glVertex2f(.25,0.25);
glVertex2f(.75,.75);
glEnd();
this code draw line but if i want to draw a line from coordinate(10,10) to coordinate(20,20) what will i do ?
what it means (.25,.25) and (.75, .75) ?
回答1:
(.25, .25) and (.75,.75) are line's start and end point.
To draw a line from (10,10) to (20,20):
glBegin(GL_LINES);
glVertex2f(10, 10);
glVertex2f(20, 20);
glEnd();
回答2:
Each point value in glVertex2f
is between -1 and 1, bottom left is (-1, -1), top right is (1,1) and center is (0, 0).
To map an absolute point to normalized space:
Divide
x
through by the window width,w
, to get the point in the range from 0 to 1.Multiply it by 2 to get the range from 0 to 2.
Subtract 1 to get the desired -1 to 1 range.
Repeat for
y
value and windows height ,h
.
For example:
double x1 = 10;
double y1 = 10;
double x2 = 20;
double y2 = 20;
x1 = 2*x1 / w - 1;
y1 = 2*y1 / h - 1;
x2 = 2*x2 / w - 1;
y2 = 2*y2 / h - 1;
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
来源:https://stackoverflow.com/questions/14486291/how-to-draw-line-in-opengl