I am trying to load earth.png and place it over a triangle. The image is 256x256. I have followed an online tutorial and played around with this for hours, but the triangle still remains white. Can any one point me in the right direction.
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
public class test implements GLEventListener {
private Texture earthTexture;
public static void main(String[] args) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
GLCanvas canvas = new GLCanvas(caps);
final Frame frame = new Frame("AWT Window Test111");
frame.setSize(700, 700);
frame.add(canvas);
frame.setVisible(true);
// by default, an AWT Frame doesn't do anything when you click
// the close button; this bit of code will terminate the program when
// the window is asked to close
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
System.exit(0);
}
});
canvas.addGLEventListener(new test());
}
#Override
public void display(GLAutoDrawable arg0) {
update();
render(arg0);
}
private void update() {
// TODO Auto-generated method stub
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBegin(GL2.GL_TRIANGLES); // Begin drawing triangle sides
earthTexture.enable();
earthTexture.bind();
// gl.glColor3f( 1.0f, 0.0f, 0.0f); // Set colour to red
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f( 0.0f, 1.0f, 1.0f); // Top vertex
gl.glTexCoord2f(-1.0f, -2.0f);
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom left vertex
gl.glTexCoord2f(1.0f, -2.0f);
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom right vertex
gl.glEnd();
}
#Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable arg0) {
GL2 gl = arg0.getGL().getGL2();
// Load texture.
try {
InputStream stream = getClass().getResourceAsStream("earth.png");
TextureData data = TextureIO.newTextureData(gl.getGLProfile(), stream, 100, 200, false, "png");
earthTexture = TextureIO.newTexture(data);
}
catch (IOException exc) {
exc.printStackTrace();
System.exit(1);
}
}
#Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
You are binding your texture in between the glBegin/glEnd statements. It is required to do so before the glBegin. Texture switches in between begin/end pairs are likely to be ignored.
A couple of things I have noticed:
You need to explicitly enable texturing within OpenGL, by using something like:
gl.glEnable(GL.GL_TEXTURE_2D);
You also will need to specify coordinates for the textures (typically expressed as u,v coordinates), this needs to be done for every 3D point:
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f( 0.0f, 1.0f, 1.0f);
...
The excellent NeHe tutorials also have JOGL sample code these days, which will be worthwhile looking at in more depth:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=07
This article also has some good information on understanding texture coordiantes:
http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node52.html
Related
I have a simple custom Canvas
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.awt.TextRenderer;
import java.awt.*;
public class MyCanvas extends GLCanvas implements GLEventListener {
private GLU glu;
public MyCanvas() {
this.addGLEventListener(this);
}
TextRenderer renderer;
#Override
public void init(GLAutoDrawable drawable) {
renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 16));
GL2 gl = drawable.getGL().getGL2();
glu = new GLU();
gl.glClearColor(1f, 1f, 1f, 1f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glShadeModel(GL2.GL_SMOOTH);
}
#Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height == 0) height = 1;
float aspect = (float)width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, aspect, 0.1, 100.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
#Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -50f);
gl.glColor3f(0.5f, 0.5f, 0.5f);
gl.glBegin(GL2.GL_TRIANGLES);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 0.0f);
gl.glEnd();
String text = "Text";
renderer.setColor(Color.BLACK);
renderer.begin3DRendering();
gl.glPushMatrix();
gl.glTranslated(0, 0, 0);
renderer.draw(text, 0, 0);
renderer.flush();
gl.glPopMatrix();
renderer.end3DRendering();
}
#Override
public void dispose(GLAutoDrawable drawable) { }
}
And I have a simple JFrame for displaying this canvas.
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class App extends JFrame {
private static final int CANVAS_WIDTH = 640;
private static final int CANVAS_HEIGHT = 480;
private static final int FPS = 60;
public App() {
GLCanvas canvas = new MyCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
this.getContentPane().add(canvas);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
new Thread() {
#Override
public void run() {
if (animator.isStarted()) animator.stop();
System.exit(0);
}
}.start();
}
});
this.setTitle("JOGL (GLCanvas)");
this.pack();
this.setVisible(true);
animator.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(App::new);
}
}
When I run programm I see very blurry text.
And when I reduce text size, then text became is more and more blurry. What should I do to get a clear text?
The text is blurry, because the font size doesn't match the rendered size on screen and bi-linear interpolation is used to magnify the font/font-texture.
The problem might happen because you are mixing 2D and 3D text rendering functions. You should either use begin3DRendering, draw3D and end3DRendering, where you can control the actual size of the text with the scaling parameter, or you use beginRendering, draw and endRendering where a orthographic projection is used to map pixels from the font to the screen.
I'm trying to move the camera in jogl but my code doesn't seem to do anything. How do you move the camera with keyboard input? I'm drawing a rotating hexagon and I want to move the camera but I can't seem to get it working. This is my code:
import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.gl2.GLUT;
import javax.media.opengl.glu.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Rotation2 extends JFrame implements GLEventListener, KeyListener
{
GLCanvas canvas;
Animator an;
public Rotation2()
{
canvas=new GLCanvas();
an=new Animator(canvas);
add(canvas);
canvas.addGLEventListener(this);
canvas.addKeyListener(this);
setSize(600,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
an.start();
}
double eyeX = 0;
double eyeY = 0;
double eyeZ = 0;
public void init(GLAutoDrawable drawable)
{
GL2 gl=drawable.getGL().getGL2();
GLU glu = new GLU();
gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glOrtho(-300,300,-200,200,200,-200);
//gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_MODELVIEW);
//glu.gluLookAt(5, 5, 5,0, 0, 0, 1, 100, 0);
gl.glClearDepth(1.0f); // Set background depth to farthest
gl.glEnable(GL2.GL_DEPTH_TEST); // Enable depth testing for z-culling
gl.glDepthFunc(GL2.GL_LEQUAL); // Set the type of depth-test
}
double x=0;
public void display(GLAutoDrawable drawable)
{
GL2 gl=drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glLoadIdentity();
GLU glu=new GLU();
//gl.glPushMatrix();
gl.glScaled(2,2,2);
gl.glRotated(x,1,1,1);
x+=.05;
glu.gluLookAt(eyeX, eyeY, 2,0, 0, 0, 1, 100, 0);
gl.glColor3f(0,1f,0);
// front
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,50,25);
gl.glVertex3d(-25,50,25);
gl.glVertex3d(-50,0,25);
gl.glVertex3d(-25,-50,25);
gl.glVertex3d(25,-50,25);
gl.glVertex3d(50,0,25);
gl.glEnd();
//left
gl.glColor3f(1f,1f,0);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,50,-25);
gl.glVertex3d(-25,50,-25);
gl.glVertex3d(-50,0,-25);
gl.glVertex3d(-25,-50,-25);
gl.glVertex3d(25,-50,-25);
gl.glVertex3d(50,0,-25);
gl.glEnd();
gl.glColor3f(1f,0f,0);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,50,25);
gl.glVertex3d(25,50,-25);
gl.glVertex3d(-25,50,-25);
gl.glVertex3d(-25,50,25);
gl.glEnd();
gl.glColor3f(0f,0f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-25,50,25);
gl.glVertex3d(-25,50,-25);
gl.glVertex3d(-50,0,-25);
gl.glVertex3d(-50,0,25);
gl.glEnd();
gl.glColor3f(1f,1f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-50,0,25);
gl.glVertex3d(-50,0,-25);
gl.glVertex3d(-25,-50,-25);
gl.glVertex3d(-25,-50,25);
gl.glEnd();
gl.glColor3f(0,1f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-25,-50,25);
gl.glVertex3d(-25,-50,-25);
gl.glVertex3d(25,-50,-25);
gl.glVertex3d(25,-50,25);
gl.glEnd();
gl.glColor3f(1,0f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,-50,25);
gl.glVertex3d(25,-50,-25);
gl.glVertex3d(50,0,-25);
gl.glVertex3d(50,0,25);
gl.glEnd();
gl.glColor3f(.2f,1f,.2f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(50,0,25);
gl.glVertex3d(50,0,-25);
gl.glVertex3d(25,50,-25);
gl.glVertex3d(25,50,25);
gl.glEnd();
//gl.glPopMatrix();
}
public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height)
{}
public void dispose(GLAutoDrawable drawable)
{}
public static void main(String[] ar)
{
new Rotation2();
}
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_X) {
eyeX++;
}
else if (e.getKeyCode() == KeyEvent.VK_Y) {
eyeY++;
}
else if(e.getKeyCode() == KeyEvent.VK_Z) {
eyeZ++;
}
else {
}
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
that's deprecated opengl, forget it.
You are also using a very old jogl, update it, go on jogamp.org, under "Builds / Download" click on "zip", then download jogamp-all-platforms.7z extract it and set jogl as explained here
Take this Hello Triangle.
It uses already the keyListener, just copy the code you wrote for the keyTyped and paste it under keyPressed (you have to use the jogamp keyListener, what you use in your sample is instead the java awt one).
Let us know if you have any problem :)
I have two graphics objects. Each object is made up of tens of thousands of triangle primitives. For the sake of this example, let`s just consider the two objects as triangles - blue triangle and orange triangles.
I want to draw the blue triangle once in the background, and use the animator to over lay the several orange triangles over the blue. Think about the blue triangle as a canvas and the orange triangles are drawn over them in a pattern. Now the canvas and the orange triangles are rotated as a whole. Also, how can I scale the whole set up and rotate them around a different axes?
In this example, I want all the orange triangles to over lay on the blue.
Here is the SSCCE. If you see in the example, the blue triangles over lay on the orange. I want all the orange triangles to be on top of blue.
Also, how do I clear the canvas from an external event like a button click and completely start redrawing the scene?
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.Animator;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainWin implements GLEventListener {
private JFrame frame;
private int iter = 0;
private float rotAng = 0;
private GLProfile profile;
private GLCapabilities capabilities;
private GLCanvas canvas;
private Animator animator;
private int totIter = 15;
private float scaleFac = 1f;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWin window = new MainWin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWin() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnZoom = new JButton("Zoom");
btnZoom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
zoomObj();
}
private void zoomObj() {
canvas.invalidate();
scaleFac -= 0.5f;
}
});
frame.getContentPane().add(btnZoom, BorderLayout.NORTH);
// Initilaize graphics profile, capabilites, canvas and animator
profile = GLProfile.get(GLProfile.GL2);
capabilities = new GLCapabilities(profile);
capabilities.setNumSamples(2);
capabilities.setSampleBuffers(true);
canvas = new GLCanvas(capabilities);
canvas.addGLEventListener(this);
canvas.requestFocusInWindow();
animator = new Animator();
animator.add(canvas);
animator.start();
frame.getContentPane().add(canvas); // add to the frame
}
#Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glRotatef(15, 0, 1, 0); // rotate both blue and orange objects around Y axis
gl.glScalef(scaleFac, scaleFac, scaleFac);
//blue object
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
gl.glEnable(GL2.GL_MULTISAMPLE);
gl.glPushMatrix();
gl.glCallList(2);
gl.glPopMatrix();
gl.glDisable(GL2.GL_MULTISAMPLE);
// orange object
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
if (iter < totIter) {
iter++;
rotAng = 5;
gl.glRotatef((float) rotAng, 0, 0, 1); // rotate each iter by 5 aroung Z. Only the orange.
gl.glEnable(GL2.GL_MULTISAMPLE);
gl.glPushMatrix();
gl.glCallList(1);
gl.glPopMatrix();
gl.glDisable(GL2.GL_MULTISAMPLE);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (iter == totIter - 1) {
animator.stop();
}
gl.glFlush();
}
#Override
public void dispose(GLAutoDrawable drawable) {
}
#Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(.0f, .0f, .2f, 0.9f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
// buffers for multisampling
int buf[] = new int[1];
int sbuf[] = new int[1];
gl.glGetIntegerv(GL2.GL_SAMPLE_BUFFERS, buf, 0);
gl.glGetIntegerv(GL2.GL_SAMPLES, sbuf, 0);
initObj1(gl); // Orange
initObj2(gl); // Blue
}
/**
* Blue triangle. In actual code, this is a complicated 3D object made up of atleast a few tens of thousands of
* primitives in a mesh.
*
* #param gl
*/
private void initObj2(GL2 gl) {
gl.glNewList(2, GL2.GL_COMPILE);
gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_DIFFUSE);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glBegin(GL2.GL_TRIANGLES);
gl.glColor4f(.2f, .5f, 0.8f, 0.5f);
gl.glVertex3f(-0.5f, 0, 0);
gl.glVertex3f(0.5f, 0, 0);
gl.glVertex3f(0, 0.5f, 0);
gl.glEnd();
gl.glEndList();
}
/**
* Orange Triangle. In actual code, this is a complicated 3D object made up of atleast a few tens of thousands of
* primitives in a mesh.
*
* #param gl
*/
private void initObj1(GL2 gl) {
gl.glNewList(1, GL2.GL_COMPILE);
gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_DIFFUSE);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glBegin(GL2.GL_TRIANGLES);
gl.glColor4f(.95f, .45f, 0.15f, 0.5f);
gl.glVertex3f(-1, 0, 0);
gl.glVertex3f(1, 0, 0);
gl.glVertex3f(0, 1, 0);
gl.glEnd();
gl.glEndList();
}
#Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
}
}
I need to make my GLCanvas to have a transparent background. The effect that I want to receive is that there will be not black background but grey from JFrame background. I use
capabilities.setBackgroundOpaque(false);
and
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
but it does not working. I know that there was the same question (Jogl: How to make a transparent background on com.jogamp.opengl.swt.GLCanvas?) but I do not understand how That HUD works. Beside, I just simply dont understand why the metod capabilities.setBackgroundOpaque(false); does not work.
here is my code
public class JOGL implements GLEventListener {
private static final int FPS = 30;
#Override
public void display(GLAutoDrawable gLDrawable) {
GL2 gl = gLDrawable.getGL().getGL2();
// Clear the drawing area
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
// Move the "drawing cursor" around
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
// Drawing Using Triangles
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set the current drawing color to red
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set the current drawing color to green
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the current drawing color to blue
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
// Finished Drawing The Triangle
gl.glEnd();
gl.glFlush();
}
#Override
public void init(GLAutoDrawable glDrawable) {
GL2 gl = glDrawable.getGL().getGL2();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
#Override
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
}
#Override
public void dispose(GLAutoDrawable gLDrawable) {
}
public static void main(String[] args) {
GLCapabilities capabilities = new GLCapabilities(null);
capabilities.setBackgroundOpaque(false);
final GLCanvas canvas = new GLCanvas(capabilities);
final JFrame frame = new JFrame("JOGL - test obj loading");
frame.setLayout(null);
final FPSAnimator animator = new FPSAnimator(canvas, FPS);
canvas.addGLEventListener(new JOGL());
JPanel p = new JPanel();
frame.add(canvas);
frame.add(p);
p.setBackground(Color.red);
canvas.setBounds(0, 0, 1024, 768);
p.setBounds(0, 0, 100, 500);
frame.setSize(1040, 900);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
animator.stop();
frame.dispose();
System.exit(0);
}
});
frame.setVisible(true);
animator.start();
canvas.requestFocus();
}
}
Rather use a GLJPanel, put it into a JInternalFrame, call setOpaque(false) on both. I did it in an Eclipse RCP application based on JOGL in order to create a transparent "window" that I could move above the rest of the GUI.
P.S: There are already 2 WaveFront OBJ loaders in JOGL itself and mine (the most robust) is in JogAmp's Ardor3D Continuation.
I'm new to jogl and I have the similar problem when I'm using jogl, but I finally find one solution.
Use the following codes to create a GLJPanel object with transparent background.
//Indeed, use GLJPanel instead of GLCanvas
GLProfile glp = GLProfile.getDefault();
GLCapabilities glcap = new GLCapabilities(glp);
glcap.setAlphaBits(8);
GLJPanel pane = new GLJpanel(glcap);
pane.setOpaque(false);
I am new to using OpenGL and am experimenting with jogl. I am able to draw triangles without a problem however when I try and draw quads (used in many tutorials) eclipse keeping telling me GL.GL_QUADS cannot be resolved.
gl.glBegin(GL.GL_QUADS);
Not sure what I'm doing wrong.
Thanks,
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;
public class SimpleScene implements GLEventListener {
public static void main(String[] args) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
GLCanvas canvas = new GLCanvas(caps);
final Frame frame = new Frame("AWT Window Test");
frame.setSize(300, 300);
frame.add(canvas);
frame.setVisible(true);
// by default, an AWT Frame doesn't do anything when you click
// the close button; this bit of code will terminate the program when
// the window is asked to close
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
System.exit(0);
}
});
canvas.addGLEventListener(new SimpleScene());
FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
animator.start();
}
#Override
public void display(GLAutoDrawable arg0) {
update();
render(arg0);
}
private void update() {
// TODO Auto-generated method stub
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
// gl.glViewport(0, 0, 300, 300); //Possibly use to move around object
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glPushMatrix();
gl.glTranslatef(-1.5f,1.5f,0.0f); // Move left 1.5 units, up 1.5 units, and back 8 units
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(10, 0, 0);
// Begin drawing triangles
gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top vertex
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom left vertex
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom right vertex
gl.glEnd(); // Finish drawing triangles
gl.glPopMatrix();
}
#Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
The javax.media.opengl.GL interface contains a subset of OpenGL common to GL 3, GL 2, GL ES 2.0 and GL ES 1.x, and GL_QUADS is not part of this subset.
If you use the javax.media.opengl.GL2, you get GL2.GL_QUADS.
It's a static constant in the GL2 class. You need to call:
gl.glBegin(GL2.GL_QUADS);
Rather than:
gl.glBegin(GL.GL_QUADS);