Friday, July 31, 2009

Fruit For Man To Masterbate

simple program to draw a point with Program

Now vemermos a simple program to draw a point using JOGL and also show in the command line version of your operating system and brand of video card you have installed on our computer. All this will do using a JFrame centered on the screen .



To begin, we must import the library:

javax.media.opengl import. * ;

which contains classes and interfaces required to call methods in Java OpenGL among which are:
  • GL Interface. This interface give us access to OpenGL functions.
  • GLEventListener Interface. This interface declares events which are used by client code to handle OpenGL rendering through GLAutoDrawable. Everything is handled by GLAutoDrawable be displayed through an instantiated object of class GLCanvas. Class
  • GLCanvas . This class will provide support for rendering OpenGL graphics, the object instances of this class must be placed inside a JPanel to be displayed.
Our main class, as well as extending the JFrame class, must implement GLEventListener interface, described above, so that the source code will be as follows:

import javax.swing. JFrame ;
import javax.swing. JPanel ;
import java.awt. Container ;
import java.awt. BorderLayout ;
import java.awt. Toolkit;
import java.awt. Dimension ;

/ / OpenGL import library import
javax.media.opengl. * ;

public class puntoJOGL extends JFrame implements GLEventListener
JPanel {
panelDibujo;
Container container;
Toolkit kit;
dimensionPantalla Dimension;
int height;
int width;

/ / GL interface will provide access to OpenGL functions
static GL gl;

/ * GLCanvas class provides support for rendering
* OpenGL graphics for the moment only the use
* to show the four main methods of GLEventListener * / static GLCanvas
canvas;

/ / Constructor public
puntoJOGL () {

super ( "Punto drawn with JOGL" )
Toolkit.getDefaultToolkit kit = (); kit.getScreenSize
dimensionPantalla = ();
height = (int ) dimensionPantalla.getHeight ();
width = (int ) dimensionPantalla.getWidth ();

/ / Create class object GLCanvas
canvas = new GLCanvas ();

/ * Add the event listener for OpenGL rendering,
* this automatically call init () and render the graphics
* whose code is written inside the method display () * /
canvas.addGLEventListener ( this )

/ * Initialize the interface GL which
use * to call OpenGL functions * / gl =
canvas.getGL ();

panelDibujo = new JPanel (new BorderLayout ()) ;

/ * add the object in the center GLCanvas
JPanel * for graphics rendering in the
GLCanvas * object can be visualized. * /
panelDibujo.add (canvas, BorderLayout. CENTER);

container = getContentPane ();
container.add (panelDibujo, BorderLayout.CENTER);
this.setSize (width / 2, height / 2);
this.setLocation (width / 4, height / 4);
this.setResizable (true);
this.setVisible (true); this
. setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);


} / / Below are the methods used by GLEventListener

/ * As explained, this method is that
* initialize the OpenGL graphics GLCanvas used,
* to call OpenGL functions use the object gl
* created earlier. * /
public void init ( GLAutoDrawable drawable)

{/ / write the screen version of the OS
System.out.println (gl.glGetString (GL.GL_VERSION));
/ / write on the screen of our brand video card
System.out.println (gl.glGetString (GL.GL_VENDOR));
/ * The bottom of the graphics displayed on the object
* GLCanvas will be black. As you can see, we
* using the method of OpenGL glClearColor (float, float, float, float)
* by GL interface. * /
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f);

} public void
reshape ( GLAutoDrawable drawable, int x, int and int width, int height) {

/ * This method, as explained, is used for the
* user can modify the "viewport"
graphics * properly. In this case, declare that our space
* work is the same size as the JFrame. This is done so that
* the point you draw appears exactly in the center of the JFrame .* /
gl.glMatrixMode (GL.GL_PROJECTION)
gl.glLoadIdentity ();

/ * Define the point of view (viewport) of our object GLCanvas
* which, as noted above, will be the same size as the
* JFrame. Thus, if there is a change in the size of
* JFrame, the workspace will adjust the size of it to
* that point to continue showing in the center. * /
gl.glOrtho (0, width, 0, height, -1.0, 1.0);

/ * If the JFrame is sized, are redrawn graphics. * /
canvas.repaint ();}


public void display ( GLAutoDrawable drawable)

{/ * This method is used to create all the graphics that
* will be drawn within the object GLCanvas. First call the
* OpenGL method glClear (GLBitField mask) which will cleanse
* all buffers for drawing. * /
gl.glClear (GL.GL_COLOR_BUFFER_BIT)

/ * Now call the method glColor3f (float, float, float)
* which will define the color of the graphics that
* drawn. In this case, draw a blue dot. * /
gl.glColor3f (0.0f, 0.0f, 1.0f);

/ * define the size of the point drawn using
glPointSize * method () in OpenGL, in this case will 10 pixels. * /
gl.glPointSize (10);

/ * This is nearly identical to a statement
* in C or C + + using OpenGL. Indicated that begin
* Drawing on the method glBegin (GLenum mode) and finalize
* with the method glEnd (). In both methods will
* ALL graphics drawings. * /
gl.glBegin (GL.GL_POINTS)
/ * For now, only draw a vertex exactly
* GLCanvas middle of our order. Because the object
* GLCanvas is in the center of the JPanel and it is
* In the center of the JFrame, the
* dot appears right in the middle of the latter. * /
gl.glVertex2i (width / 2, height / 2);
gl.glEnd ();

/ * Indicates draw immediately after using the method
* glFlush (); * /
gl.glFlush ();

} public void
displayChanged ( GLAutoDrawable drawable, boolean
ModeChanged, boolean deviceChanged)

{/ * Method for handling events in exchange for display,
* it will not use it now. * /}


/ / Stop the methods used by GLEventListener

/ / main method to start executing our program
public static void main (String [] args) {

/ * Simply create a class object
* puntoJOGL, then the program will run
* constructor code. * /
puntoJOGL new ();
}}


When you compile and run the code above, we show a centered JFrame with a dot drawn in the middle like the figure below:


While in the command line shown us some data as follows:


If you wish to download the source code you can click on the link below:

puntoJOGL.java Download source

OpenGL methods used.

glClearColor (float, float, float)

In the method init () , we can change the background graphics using the method OpenGL, glClearColor (float, float, float, float ) named by GL interface implemented in the constructor. The method glClearColor (float, float, float, float) handles colors using the RGBA model with a color depth interval between 0 and 255. Because managed float data type, we must use the formula:

(r/255, g/255, b/255, alpha)

being r, g, b desired color intensity and alpha transparency thereof, the combination of all of us will result in the requested color. To be more clear, here are some examples:
  1. Color YELLOW . To create this color combination is needed (255, 255, 0, 0), therefore, in OpenGL is (1.0f, 1.0f, 0.0f, 0.0f). CYAN Color
  2. . To create this color combination is needed (0, 255, 255, 0), therefore, in OpenGL is (0.0f, 1.0f, 1.0f, 0.0f). MAGENTA Color
  3. . To create this color combination is needed (255, 0, 255), 0, therefore, in OpenGL is (1.0f, 0.0f, 1.0f, 0.0f). ORANGE Color
  4. . To create this color combination is needed (255, 128, 0, 0), therefore, in OpenGL is (1.0f, 0.5f, 0.0f, 0.0f). PURPLE Color
  5. . To create this color combination is needed (128, 0, 255, 0), therefore, in OpenGL is (0.5f, 0.0f, 1.0f, 0.0f). Color
  6. ROSA. To create this color combination is needed (255, 0, 128, 0), therefore, in OpenGL is (1.0f, 0.0f, 0.5f, 0.0f).
can be tested by changing the values \u200b\u200bin the method glClearColor (float, float, float, float) within the method init () .

For more information visit the glClearColor method documentation () .

glColor3f (float, float, float)

This method is used to define the color of the graphics are drawn, the colors created using the RGB model . So to change the colors used a similar method with glClearColor (float, float, float, float) , only this time we will not use the variable alpha . Other variants glColor method * () that depend on the type of data they handle eg
  1. glColor3i void (int, int, int)
  2. void glColor3f (float, float, float) - Seen in the sample program. Void
  3. glColor3d (double, double, double) void
  4. glColor3fv (float [], int alpha) - This method, like glClearColor , use color transparency alpha defined.
can be tested by changing the values \u200b\u200bin the method glColor3f (float, float, float, float) within the method display () . For

information visit the glColor method documentation * () .

glBegin (GL.GL_POINTS) and glEnd ()

These methods especidican the beginning and end of the drawn object , in this case is a point. The general form of these methods is as follows:

glBegin (GLenum mode)
/ / Draw object
glEnd () Where

mode includes parameters such as:
  • GL_POINTS - Used in this program .
  • GL_LINES - Used to draw lines, there are other alternatives.
  • GL_TRIANGLES - Used to draw triangles, there are other alternatives.
  • GL_QUADS - Used to draw squares, exitsten other variants.
  • GL_POLYGON - Used to draw polygons.
Later we will see some concrete examples, if you want more information check the documentation for the method glBegin () .

glPointSize (int pixels)

This method is used to define the size of the point to draw, in this example program was 10 pixels.

glMatrixMode (GLenum mode);
gl.glLoadIdentity ();
gl.glOrtho (double left, double right, double bottom, double top, double near, double far)

These methods are necessary to define the orthographic projection is ie in tems easier, defines our workspace. In the case of our program define a workspace width x height x 2 , in this case the 2 is not seen as we are working in two dimensions only. To have a better idea, consider the workspace as a "hub" where:
  • The front lower left corner is defined by coordinates (Left, bottom, far)
  • The right rear corner is defined by coordinates (right, top, near)




For more information, visit the documentation glOrtho () .

glVertex2i (int x, int y)


This method draw a vertex located at coordinates (x, y) in the sample program are located exactly at the top half of our JFrame using the height and width variables . Because the method defined gl.glOrtho a workspace (width, height, 2) then, calling the method glVertex2i (width / 2, height / 2) we are putting our corner right in the middle of our area of \u200b\u200bwork, which, being situated in the middle of JFrame shows the point exactly between latter. The method glVertex * () contains many variants, among which are:
  • glVertex2i (int x, int y) - Used in the example program.
  • glVertex2f (float x, float y)
  • glVertex2i (int x, int y, int z) - Used to draw 3-D graphics.
For more information, visit the documentation glVertex () .

Finally, they can play with the code and make changes to see the results. Here are some variations by modifying the code:

glClearColor (1.0f, 1.0f, 1.0f, 0.0f)
glColor3f (1.0f, 0.0f, 0.0f)


glClearColor (0.5f, 0.0f, 1.0f, 0.0f)
glColor3f (0.0f, 0.0f, 0.0f)


glClearColor (1.0f, 0.0f, 0.0f, 0.0f)
glColor3f (1.0f, 1.0f, 0.0f)

FINAL NOTE: Remember that
call OpenGL functions must do so through JOGL GL interface.

0 comments:

Post a Comment