Alex Encore
Alex Encore

Reputation: 309

OpenGL - draw line with points

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

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:

enter image description here

My code so far for the EventListener where I create my line is the following:

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.

Upvotes: 1

Views: 17699

Answers (2)

genpfault
genpfault

Reputation: 52123

Try using 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.

Upvotes: 1

S.P.
S.P.

Reputation: 3054

Thats how you do it in 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

Edited play around with your +=0.01 and you will get the results BETTER WAY

glBegin(GL_LINES);
   glVertex2i(0,0);
   glVertex2i(100,100);
glEnd();

Upvotes: 2

Related Questions