Java OpenGL - 用点画线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9691670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenGL - draw line with points
提问by Alex Encore
The exercise I have is the following: In display() add a method drawLine. Probably, you will need something like drawLine(GL gl, int x1, int y1, int x2, int y2) now using the equation of a line, plot the individual points that make up the line from x1,y1 to x2,y2 in JOGL using (for instance) gl.glPointSize(1.0f); gl.glBegin(GL.GL_POINTS); If you have coloured the points white, and used the following projections
我的练习如下: 在 display() 中添加一个方法 drawLine。可能,你现在需要像 drawLine(GL gl, int x1, int y1, int x2, int y2) 这样的东西,现在使用直线方程,绘制构成从 x1,y1 到 x2,y2 的直线的各个点JOGL 使用(例如)gl.glPointSize(1.0f); gl.glBegin(GL.GL_POINTS); 如果您将点涂成白色,并使用以下投影
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
then with a value of a line from points (0,0) to (100,100) yours will look something like the following picture:
然后使用从点 (0,0) 到 (100,100) 的线值,您的值将类似于下图:
My code so far for the EventListener where I create my line is the following:
到目前为止,我在其中创建行的 EventListener 的代码如下:
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
public class ThirdGLEventListener implements GLEventListener {
/**
* Interface to the GLU library.
*/
private GLU glu;
/**
* Take care of initialization here.
*/
public void init(GLAutoDrawable gld) {
GL gl = gld.getGL();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
/**
* Take care of drawing here.
*/
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
drawLine(gl, 0, 0, 100, 100);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
public void displayChanged(GLAutoDrawable drawable,
boolean modeChanged, boolean deviceChanged) {
}
private void drawLine(GL gl, int x1, int y1, int x2, int y2) {
gl.glPointSize(1.0f);
gl.glBegin(GL.GL_POINTS);
// This is where the whole problem lies.
gl.glEnd();//end drawing of points
}
}
}
My problem is that I don't quite know how to complete the code inside the method for the line equation. I would appreciate it if you could help me with that bit. I think the rest of it is just right, I just need to find how to implement the line equation inside the drawLine() method.
我的问题是我不太知道如何完成线方程方法中的代码。如果你能帮我解决这个问题,我将不胜感激。我觉得剩下的就对了,我只需要在 drawLine() 方法中找到如何实现线方程即可。
采纳答案by genpfault
Try using GL_LINES
:
尝试使用GL_LINES
:
private void drawLine(GL gl, int x1, int y1, int x2, int y2) {
gl.glPointSize(1.0f);
gl.glBegin(GL.GL_POINTS);
int samples = 100;
float dx = (x2 - x1) / (float)samples;
float dy = (y2 - y1) / (float)samples;
for( int i = 0; i < samples; i++ )
{
gl.glVertex2f( i * dx, i * dy );
}
gl.glEnd();//end drawing of points
}
Adjust samples
to taste.
samples
根据口味调整。
回答by S.P.
Thats how you do it in OpenGL.
这就是你在 OpenGL 中的做法。
glBegin(GL_POINTS);
for(float i = 0; i <= 100; ){
glVertex2f(i,i);
i+=0.01;
}
glEnd();
Also, a suggestion you want to have your Orthographic projections in GL_PROJECTION matrix and your modelview like the above code in GL_MODELVIEW
此外,建议您希望在 GL_PROJECTION 矩阵和模型视图中进行正交投影,例如 GL_MODELVIEW 中的上述代码
Edited play around with your +=0.01
and you will get the results BETTER WAY
编辑后玩你的+=0.01
,你会得到更好的结果
glBegin(GL_LINES);
glVertex2i(0,0);
glVertex2i(100,100);
glEnd();