JOGL transparent background - java

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);

Related

Blurry text on GLCanvas

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.

Texture not displaying properly OpenGL

I'm trying to load a simple background texture for the menu of a game I'm creating, but the texture isn't displayed properly.
For example, if I use this texture: http://s15.postimage.org/mqvuhq463/Super_Mario_Galazy_Background.png the entire background is blue and if I use this texture: http://www.highresolutiontextures.com/wp-content/uploads/2010/11/food-textures-01-bowtie-pasta-texture.jpg the entire background is a shade of orange.
This is the code I've written:
public class Menu extends Painter
{
public Menu(GLCanvas canvas, Animator animator, GameWindow window)
{
super(canvas, animator, window);
}
#Override
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
drawBackground(gl);
gl.glFlush();
}
private void drawBackground(GL gl)
{
Texture background = textureLoader.getTexture("background");
background.enable();
background.bind();
gl.glBegin(GL.GL_QUADS);
gl.glVertex2f(0f, 0f);
gl.glVertex2f(0f, window.getHeight());
gl.glVertex2f(window.getWidth(), window.getHeight());
gl.glVertex2f(window.getWidth(), 0);
gl.glEnd();
background.disable();
}
#Override
public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
textureLoader = new TextureLoader("menu textures/", gl);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
// set ortho to the size of the background
gl.glOrtho(0, 800, 800, 0, -1, 1);
gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
gl.glClearColor(0f, 0f, 0f, 0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
}
}
I haven't got the slightest clue how this is even possible, so any help would be greatly appreciated.
You don't supply texture coordinates. Without texture coordinates all vertices receive the default texture coordinate, which is (0,0,0,0), which is the bottom left corner. If you look at your texture images the colors you see, are the colors of the bottom left pixel of each picture.
Solution: Supply texture coordinates. Texture coordinates are in the range [0; 1].

rotating an object using gl.glrotatef

I am trying to get an object to rotate using opengl and the command gl.glroate but nothing seems to be happening, can anyone show me where i am going wrong ? below is the code
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
public class Cube2 implements GLEventListener {
private float Rotate = 0.0f;
private static final GLU glu = new GLU ();
/**
* #param args
*/
public static void main(String[] args){
JFrame frame = new JFrame("A simple JOGL demo");
GLCanvas canvas = new GLCanvas ();
canvas.addGLEventListener(new Cube2());
frame.add(canvas);
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Animator animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
}
#Override
public void display (GLAutoDrawable glDrawable){
final GL gl = glDrawable.getGL();
//gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -7.0f);
gl.glRotatef(Rotate++, 1.0f, 0.0f, 0.0f);
gl.glRotatef(Rotate++, 0.0f, 1.0f, 0.0f);
gl.glRotatef(Rotate++, 0.0f, 0.0f, 1.0f);
gl.glBegin(GL.GL_TRIANGLES);
//Counter Clockwise Front Face 1
gl.glColor3f(0.0f, 1.0f, 0.0f); //Green
gl.glVertex3f (1.0f, 1.0f, 0.0f); //Top
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // left to right
gl.glVertex3f (-1.0f, -1.0f, 0.0f); //left
gl.glVertex3f (-1.0f, -1.0f, -0.0f); //left
gl.glVertex3f(1.0f, -1.0f, -0.0f); //left to right
gl.glVertex3f (1.0f, 1.0f, -0.0f); //left
// Right Face 2
gl.glColor3f(1.0f, 0.0f, 1.0f); //Pink
gl.glVertex3f (1.0f, 1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, -0.0f);
gl.glVertex3f (1.0f, 1.0f, 1.0f);
gl.glVertex3f (-1.0f, -1.0f, -0.0f);
gl.glVertex3f(-1.0f, 1.0f, 0.0f);
gl.glVertex3f (-1.0f, -1.0f, -1.0f);
gl.glEnd();
}
#Override
public void displayChanged (GLAutoDrawable arg0, boolean arg1, boolean arg2)
{
//TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable glDrawable)
{
final GL gl = glDrawable.getGL();
int width = 640;
int height = 480;
//Set the state of out new OpenGL context
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f,(float)(width)/(float)(height),1.0f,100.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glShadeModel(GL.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepth(1.0f); //Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); //The Type of Depth Testing To Do
gl.glEnable(GL.GL_CULL_FACE); //Start culling back faces
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); //Really Nice Perspective Calculations
}
#Override
public void reshape (GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4){
//TODO Auto-generated method stub
}
}
thank you in advance for your help
You should start by adding
animator.start();
in order to start the rendering.
You are using JOGL 1 which is no longer maintained. And it defenetly won't work. You should follow the tutorials at Jogamp.org.
You are using proprietary apis like import com.sun.opengl.util.Animator; to begin with. It is a bad choice if you want help from people who don't own the right to use that api. As I said, you can go to jogamp.org for a open source substitute of that package.

Translation position of an object only visible in the -1.0 to +1.0 range in Java3D

I have a tiny little program in Java which supposed to display 2 spheres. It works fine until I the translation of a sphere gets out of the range of -1.0 to 1.0, the object in this case will disappear. I have a SimpleUniverse setup and I tried several setBounds in several places, but I couldn't figure out, how is it disappear. The real strangeness, that this is position isn't dependent on the camera position, as I have an OrbitBehavior on the ViewPlatform transform at moving closer doesn't show the object.
This is the setup for the spheres:
public class CelestialBody extends TransformGroup {
CelestialBody(float posX, float posY, float posZ) {
// This is set for the dynamic behaviour.
setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3f(posX, posY, posZ));
setTransform(t3d);
setBounds(new BoundingSphere(new Point3d(0.0f, 0.0f, 0.0f), 10000.0f));
Sphere sphere = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 100, createAppearance());
addChild(sphere);
}
private Appearance createAppearance() {
Appearance appear = new Appearance();
Material mat = new Material();
mat.setShininess(100.0f);
appear.setMaterial(mat);
return appear;
}
}
This is the main program:
public class CelestialApp extends Frame {
public static void main(String[] args) {
// TODO Auto-generated method stub
new CelestialApp();
}
private SimpleUniverse universe = null;
public CelestialApp() {
// ...
BranchGroup scene = constructContentBranch();
scene.compile();
universe = new SimpleUniverse(defaultCanvas);
setupView();
universe.addBranchGraph(scene);
}
private void setupView() {
ViewingPlatform vp = universe.getViewingPlatform();
OrbitBehavior orbit = new OrbitBehavior();
orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0f, 0.0f, 0.0f), 10000.0f));
vp.setNominalViewingTransform();
vp.setViewPlatformBehavior(orbit);
}
private BranchGroup constructContentBranch() {
BranchGroup scene = new BranchGroup();
CelestialBody body1 = new CelestialBody(-2.0f, 0.0f, 0.0f);
scene.addChild(body1);
CelestialBody body2 = new CelestialBody(2.0f, 0.0f, -1.1f);
scene.addChild(body2);
PhysicalBehavior physics1 = new PhysicalBehavior(body1);
scene.addChild(physics1);
PhysicalBehavior physics2 = new PhysicalBehavior(body2, new Vector3d(0.0f, 0.0f, 0.0f));
scene.addChild(physics2);
setupLights(scene);
return scene;
}
private void setupLights(BranchGroup scene) {
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
lightA.setColor(new Color3f(0.3f, 0.3f, 0.3f));
scene.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f dir = new Vector3f(-0.3f, -1.0f, -0.5f);
dir.normalize();
lightD1.setDirection(dir);
lightD1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
scene.addChild(lightD1);
Background bg = new Background(0.0f, 0.0f, 0.0f);
bg.setApplicationBounds(new BoundingSphere());
scene.addChild(bg);
}
}
Bah, never mind. The light source had only a bounding sphere with radius 1.0f and the object was rendered but with black.

JOGL white texture?

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

Categories

Resources