We will see a simple example of a program that lets you draw lines using several variations of the methods glVertex of OpenGL obviously using the Java language and libraries JOGL. We have explained earlier how draw a point using JOGL libraries , well, the same code as "used to draw lines.
The code will modify the contents inside the methods:
- init ( GLAutoDrawable drawable)
- reshape ( GLAutoDrawable drawable, int x, int and int width, int height)
- display ( GLAutoDrawable drawable)
init ( GLAutoDrawable drawable)
In the code we use to draw point also print the mark of your video card and operating system screen, this time simply initializes the background color to black:
public void init ( GLAutoDrawable drawable) {
gl.glClearColor (0.0f, 0.0f , 0.0f, 0.0f);}
reshape ( GLAutoDrawable drawable, int x, int and int width, int height)
In this method which we will define a "space work "that includes the following ranges:
- x = [-5, 5]
- y = [-5, 5]
- z = [-5, 5]
public void reshape ( GLAutoDrawable drawable, int x, int and int width, int height) {
gl.glMatrixMode (GL.GL_PROJECTION) gl.glLoadIdentity ();
gl.glOrtho (-5.0f, 5.0f,-5.0f, 5.0f,-5.0f, 5.0f);
canvas.repaint ();}
remember the way we define our "workspace"
- glOrtho method (double left, double right, double bottom, double top, double near, double far) is used to define the orthographic projection.
- Therefore, this method will create a "workspace" similar to a bucket where the bottom left corner front is defined by coordinates (left, bottom, far) and the right rear corner is defined by coordinates (right, top, near) .
display ( GLAutoDrawable drawable)
modify Here code so that we can use different methods to draw lines and to form a polygon. First we will start clearing the buffers used by your video card to draw and define the color with which we draw our lines. In this case I have decided to use the color CYAN, however, we can use any other color if it is different from that defined in the background.
gl.glClear (GL.GL_COLOR_BUFFER_BIT)
gl.glColor3f (0.0f, 1.0f, 1.0f);
JOGL Now we will tell what the charts are drawn using the methods glBegin (GLenum mode) and glEnd () . Recall that in these methods will all graphics to be displayed in our view GLCanvas . Use the method glVertex2d (double x, double y) to tell JOGL begins and ends where the vertices of the lines you draw.
In this example, we will do is draw a square. To achieve our mission will draw just 4 vertices:
- (-4, -4)
- (4, -4)
- (4, 4)
- (-4, 4) We help
Within methods glBegin () and glEnd () declare our vertices:
gl.glBegin (GL.GL_LINE_LOOP)
/ / Initialize the first vertex
/ / x (first vertex)
point [0] [0] = -4.0;
/ / y (first vertex)
point [0] [1] = -4.0;
/ / Initialize the second vertex
/ / x (second point)
point [1] [0] = 4.0;
/ / y (second point)
point [1] [1] = -4.0;
/ / Initialize the third vertex
/ / x (third point)
point [2] [0] = 4.0;
/ / y-coordinate (third point)
point [2] [1] = 4.0;
/ / Initialize the fourth vertex
/ / Coordinates x (fourth vertex)
point [3] [0] = -4.0;
/ / y-coordinate (fourth vertex)
point [3] [1] = 4.0;
/ / Draw ALL
vertices gl.glVertex2d (point [0] [0], item [0] [1]);
gl.glVertex2d (point [1] [0], item [1] [1]) ;
gl.glVertex2d (point [2] [0], item [2] [1]);
gl.glVertex2d (point [3] [0], item [3] [1]);
gl.glEnd ();
gl.glFlush ();
When you compile and run the modified code shows a result as follows:
The Mode parameter of the method glBegin (GLenum mode) will modify that to show different ways to draw lines with JOGL. In the previous example was used GL_LINE_LOOP , thus connecting all the vertices with line segments as follows:
- Draw a line segment of the first vertex to the second.
- Draw a line segment of the second vertex to the third.
- . . .
- Draw a line segment of the last vertex to the first.
gl.glBegin (GL.GL_LINE_LOOP)
by:
gl.glBegin (GL.GL_LINES)
obtain a result like this:
GL_LINE_STRIP parameter makes points defined within methods glBegin ( ) and glEnd () generate a sequence of line segments where the endpoint of a line segment will be the starting point of the next line segment , similar to what happens with GL_LINE_LOOP with the difference that the last vertex does not generate a line segment with the first vertex . If we change the method code:
gl.glBegin (GL.GL_LINE_LOOP)
by:
gl.glBegin (GL.GL_LINE_STRIP)
get a result like the following:
You can change the line thickness using the method:
gl.glLineWidth ( float width);
For example if we modify the method code:
gl.glBegin (GL.GL_LINE_LOOP)
by:
gl.glLineWidth (5);
gl.glBegin (Gal. GL_LINE_LOOP )
obtain a result like this:
What happens if you simply draw the vertices ? Modica method code:
gl.glBegin (GL.GL_LINE_LOOP)
by:
/ / points of 12 pixels so you can see
gl.glPointSize (12);
gl.glBegin (GL.GL_POINTS)
get a result like following:
If you wish to download the source code POSE to click on the following link: Download
dibujaLineas.java source
0 comments:
Post a Comment