Blurry text on GLCanvas - java

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.

Related

How to move camera eye position in JOGL?

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

JOGL, Animator, Scaling, Overlay

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) {
}
}

Can not draw anything after text rendering initialization code in lwjgl (Java)

Hello I have two seperate initialization codes to switch between rendering 2d shapes and (2d) text in lwjgl. If the initialization code for rendering text is executed, the 2d shapes will not be drawn. I tried everything, and I found the problem line: GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
After I have done something with the glBlendFunc, I can only render tekst, and switching to the initialization code for rendering 2d shapes won't work anymore.
Here are my 2 codes:
Simple 2d rendering:
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
Code for rendering text:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
// Problem line
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
I think the problem is just a wrong OpenGl state, but how can I put the states right?
It's hard to find out the issue without access to your whole code, thus I can just post some guesses:
Do you clear the colour and depth buffer (glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);)?
Do you enable GL_TEXTURE_2D when drawing textures and disable it otherwise?
I use the same blend function and can draw images just fine (it's the most used blend function for drawing images that have transparency).
PS: This are the only settings I use to draw any kind of 2D stuff (I'm using vertex array objects and shaders for rendering though):
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDisable(GL_DEPTH_TEST);
Here is more code
Renderer.java
package game;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.opengl.GL11.GL_CONSTANT_COLOR;
import static org.lwjgl.opengl.GL11.GL_ONE;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Renderer {
private static boolean in3d = false;
public static void initText2D() {
in3d = false;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void init2D() {
in3d = false;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL_TEXTURE_2D);
// Test
//GL11.glBlendFunc(GL_CONSTANT_COLOR, GL_ONE);
}
public static void init3D(float fov, float aspect, float near, float far) {
in3d = true;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
GL11.glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
public static void begin(int shape) {
GL11.glBegin(shape);
}
public static void end() {
GL11.glEnd();
}
public static void setVertex3(float x, float y, float z) {
if(!in3d) {
System.out.println("[WARNING] > Adding 3d vertex but there is no 3d context");
}
GL11.glVertex3f(x, y, z);
}
public static void setColor(float r, float g, float b) {
GL11.glColor3f(r, g, b);
}
public static void setVertex2(float x, float y) {
if(in3d) {
System.out.println("[WARNING] > Adding 2d vertex while in 3d context");
}
GL11.glVertex2f(x, y);
}
}
Button.java (example of how I draw a button in lwjgl)
package gui;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import game.Renderer;
import game.TextRenderer;
import org.lwjgl.opengl.GL11;
public class Button extends AbstractButton {
private String text;
public Button(int x, int y, int w, int h, String text) {
this.setX(x);
this.setY(y);
this.setWidth(w);
this.setHeight(h);
this.text = text;
paintElement();
}
public Button(String text) {
this.text = text;
paintElement();
}
public Button() {
paintElement();
}
public void paintElement() {
Renderer.init2D();
Renderer.begin(GL11.GL_QUADS);
Renderer.setColor(this.getColorR(), this.getColorG(), this.getColorB());
Renderer.setVertex2(this.getX(), this.getY());
Renderer.setVertex2(this.getX() + this.getWidth(), this.getY());
Renderer.setVertex2(this.getX() + this.getWidth(), this.getY() + this.getHeight());
Renderer.setVertex2(this.getX(), this.getY() + this.getHeight());
Renderer.end();
Renderer.initText2D();
TextRenderer.drawString(this.getX() + 10, this.getY() + 10, this.text);
}
}
Main.java
package game;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import java.awt.Font;
import gui.Button;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
public class Main {
private int gamestate;
private boolean closeRequested;
private static Camera cam;
private static User user;
public Main() {
createUser();
createDisplay();
createCamera();
gameLoop();
cleanUp();
}
private void createUser() {
}
private void createDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setResizable(true);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
private void createCamera() {
}
private void gameLoop() {
while(!closeRequested) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Renderer.init2D();
/*
// Test 2d line
Renderer.init2D();
Renderer.setColor(1.0f, 1.0f, 1.0f);
Renderer.begin(GL11.GL_LINE);
Renderer.setVertex2(0, 0);
Renderer.setVertex2(100, 100);
Renderer.end();
*/
// set the color of the quad (R,G,B,A)
glColor3f(0.7f, 0.5f, 1.0f);
// draw quad
glBegin(GL_QUADS);
glVertex2f(100,100);
glVertex2f(100+200,100);
glVertex2f(100+200,100+200);
glVertex2f(100,100+200);
glEnd();
checkInput();
Button b = new Button(100, 100, 100, 30, "Test");
Display.update();
Display.sync(10);
}
return;
}
private void checkInput() {
// Check keyboard, mouse and other input
if(Display.isCloseRequested()) {
closeRequested = true;
}
return;
}
private void cleanUp() {
}
public static void main(String[] args) {
new Main();
}
}
I got it working!
This is my new (working) code:
init2d
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
// solution
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
initText2d
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
I think the problem was that slick uses textures for text rendering, and I had to enable the textures or bind them to 0.
See this: http://lwjgl.org/forum/index.php?topic=4019.0

JOGL 2.0 doesn't support GLCanvas ,Texture ,Animator , but jogl 1.0 does?

I downloaded jogl 2.0 from here , the file jogl-all.jar , and ran a simple code example taken from here :
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import com.sun.opengl.util.Animator;
/**
* This is a simple example for the method
*
* glNormal();
*
*
* Keyboard commands:
*
* Key A) Increase the deltaZ value
*
* Key B) Decrease the deltaZ value
*
* Key C) Increase the deltaX value
*
* Key D) Decrease the deltaX value
*
* Key E) Increase the deltaY value
*
* Key F) Decrease the deltaY value
*
* #author Alessandro Martinelli
*/
public class Practice11IlluminateGeometry extends JFrame implements KeyListener{
private static float deltaZ=0;
private static float deltaX=0;
private static float deltaY=0;
public static void main(String[] args) {
Practice11IlluminateGeometry frame=new Practice11IlluminateGeometry();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Practice11IlluminateGeometry(){
setSize(600,600);
setTitle("Hello Universe");
GraphicListener listener=new GraphicListener();
GLCanvas canvas = new GLCanvas(new GLCapabilities());
canvas.addGLEventListener(listener);
getContentPane().add(canvas);
Animator animator = new Animator(canvas);
animator.start();
addKeyListener(this);
}
public class GraphicListener implements GLEventListener{
public void display(GLAutoDrawable arg0) {
GL gl=arg0.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glCullFace(GL.GL_FRONT);
gl.glEnable(GL.GL_CULL_FACE);
gl.glFrontFace(GL.GL_CW);
gl.glLoadIdentity();
gl.glTranslatef(deltaX, deltaY, deltaZ);
gl.glColor3f(1,1,1);
gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glNormal3f(0, 0, 1);
gl.glVertex3f(0,0,0);
gl.glVertex3f(0.3f,0,-0.4f);
gl.glVertex3f(0,0.3f,0);
gl.glVertex3f(0.3f,0.3f,0.4f);
gl.glVertex3f(0.3f,0.6f,0);
gl.glEnd();
}
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
}
public void init(GLAutoDrawable arg0) {
GL gl=arg0.getGL();
gl.glEnable(GL.GL_LIGHTING);
float ambient[]= {0.2f,0.2f,0.2f,1};
gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT , ambient,0);
gl.glEnable(GL.GL_LIGHT0);
float position[]= {-0.4f,0.5f,0.7f,1};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0);
float intensity[]= {1,1,1,1};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, intensity, 0);
gl.glEnable(GL.GL_LIGHT1);
float position2[]= {0,-0.8f,0.3f,1};
gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, position2, 0);
float intensity2[]= {1,0,0,0};
gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, intensity2, 0);
float specIntensity2[]= {1,1,1,1};
gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, specIntensity2, 0);
gl.glEnable(GL.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
float specColor[]= {1,1,1,1};
gl.glMaterialfv(GL.GL_FRONT_AND_BACK,GL.GL_SPECULAR, specColor,0);
gl.glMaterialf(GL.GL_FRONT_AND_BACK,GL.GL_SHININESS, 80);
}
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
}
}
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_A){
deltaZ+=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_B){
deltaZ-=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_C){
deltaX+=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_D){
deltaX-=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_E){
deltaY+=0.05;
}
if(arg0.getKeyCode()==KeyEvent.VK_F){
deltaY-=0.05;
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
static final long serialVersionUID=100;
}
and I got :
The import javax.media.opengl.GLCanvas cannot be resolved
The import com.sun.opengl cannot be resolved
and a few more (from other projects):
Texture cannot be resolved to a type
Animator cannot be resolved to a type
I guess that the jar doesn't support those types , why is that ? any way around this ?
This is quite weird , since version 2.0 suppose to support everything that version 1.0 supports , or am I wrong ?
Furthermore , this code works great with jogl 1.0 , but I must use version 2.0 .
Between jogl 1.1 and 2.0 the top level package name of the jogl project changed from javax.media.opengl to com.jogamp.opengl
For the two cases listed the Animator class can now be found under com.jogamp.opengl.util.Animator and Texture is now in the package com.jogamp.opengl.util.texture.Texture
The javadoc for jogl should help locate any other classes you are missing.
https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/
I used the above example as template and adapted it to Jogl 2.0.
Please see code below:
import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.Animator;
public class IlluminateGeometry extends Frame implements KeyListener {
private static float deltaZ = 0.0f;
private static float deltaX = 0.0f;
private static float deltaY = 0.0f;
static GL2 gl;
static GLCanvas canvas;
static GLCapabilities capabilities;
static GLProfile profile;
static Animator animator;
public IlluminateGeometry() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
dispose();
System.exit(0);
}
});
profile = GLProfile.getDefault();
capabilities = new GLCapabilities(profile);
canvas = new GLCanvas(capabilities);
animator = new Animator(canvas);
GraphicListener graphiclistener = new GraphicListener();
canvas.addGLEventListener(graphiclistener);
canvas.addKeyListener(this);
add(canvas, BorderLayout.CENTER);
animator.start();
}
public class GraphicListener implements GLEventListener {
public void display(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Enables clearing of the depth buffer
gl.glClearDepth(1.0f);
// Z-Buffer algorithm
gl.glEnable(GL2.GL_DEPTH_TEST);
// The type of depth test to do
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glCullFace(GL2.GL_FRONT);
gl.glEnable(GL2.GL_CULL_FACE);
gl.glFrontFace(GL2.GL_CW);
gl.glLoadIdentity();
gl.glTranslatef(deltaX, deltaY, deltaZ);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glBegin(GL2.GL_TRIANGLE_STRIP);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glVertex3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(0.3f, 0.0f, -0.4f);
gl.glVertex3f(0.0f, 0.3f, 0.0f);
gl.glVertex3f(0.3f, 0.3f, 0.4f);
gl.glVertex3f(0.3f, 0.6f, 0.0f);
gl.glEnd();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glEnable(GL2.GL_LIGHTING);
float lightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lightAmbient, 0);
gl.glEnable(GL2.GL_LIGHT0);
float lightPosition[] = { -0.4f, 0.5f, 0.7f, 1.0f };
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPosition, 0);
float lightIntensity[] = { 1.0f, 1.0f, 1.0f, 1.0f };
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, lightIntensity, 0);
gl.glEnable(GL2.GL_LIGHT1);
float lightPosition2[] = { 0.0f, -0.8f, 0.3f, 1.0f };
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPosition2, 0);
float lightIntensity2[] = { 1.0f, 0.0f, 0.0f, 0.0f };
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, lightIntensity2, 0);
float lightSpecularIntensity2[] = { 1.0f, 1.0f, 1.0f, 1.0f };
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightSpecularIntensity2, 0);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
float lightSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, lightSpecularColor, 0);
gl.glMaterialf(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 80);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
public void dispose(GLAutoDrawable drawable) {}
}
public void keyTyped(KeyEvent key) {}
public void keyPressed(KeyEvent key) {
switch (key.getKeyCode()) {
case KeyEvent.VK_A:
deltaZ += 0.05;
break;
case KeyEvent.VK_B:
deltaZ -= 0.05;
break;
case KeyEvent.VK_C:
deltaX += 0.05;
break;
case KeyEvent.VK_D:
deltaX -= 0.05;
break;
case KeyEvent.VK_E:
deltaY += 0.05;
break;
case KeyEvent.VK_F:
deltaY -= 0.05;
break;
default:
break;
}
}
public void keyReleased(KeyEvent key) {}
public static void main(String[] args) {
IlluminateGeometry frame = new IlluminateGeometry();
frame.setTitle("Illuminate Geometry");
frame.setSize(640, 480);
frame.setVisible(true);
}
}

Why won't my texture show up?

Okay, I'm using Java and JoGL. I'm trying to load and display a texture, but the texture isn't showing up at all. I'm not getting any errors at all, so I don't know what the problem could be.
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import javax.imageio.*;
import javax.swing.*;
public class chartest extends JFrame implements GLEventListener, KeyListener
{
private int texture;
public void display(GLAutoDrawable gLDrawable)
{
final GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
gl.glBegin(GL.GL_QUADS); // Draw A Quad
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-12.0f, -19.0f, -15.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 12.0f, -19.0f, -15.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 12.0f, 19.0f, -15.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-12.0f, 19.0f, -15.0f);
gl.glEnd(); // Done Drawing The Quad
gl.glFlush();
}
public void displayChanged(GLAutoDrawable g, boolean b, boolean b2){}
public void init(GLAutoDrawable gLDrawable)
{
final GL gl = gLDrawable.getGL();
gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_FLAT);
gl.glEnable(GL.GL_TEXTURE_2D);
texture = genTexture(gl);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
URL url = this.getClass().getResource("test.png");
try
{
BufferedImage img = ImageIO.read(url);
makeRGBTexture(gl, new GLU(), img, GL.GL_TEXTURE_2D, false);
}
catch(Exception e)
{
e.printStackTrace();
}
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
}
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
{
final GL gl = gLDrawable.getGL();
final GLU glu = new GLU();
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
chartest c = new chartest();
GLCanvas canvas = new GLCanvas(new GLCapabilities());
canvas.addGLEventListener(c);
c.add(canvas);
c.setSize(640, 480);
canvas.addKeyListener(c);
c.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
c.setVisible(true);
canvas.requestFocus();
}
//-----------------------
private void makeRGBTexture(GL gl, GLU glu, BufferedImage img, int target, boolean mipmapped)
{
ByteBuffer dest = null;
switch (img.getType())
{
case BufferedImage.TYPE_3BYTE_BGR:
case BufferedImage.TYPE_CUSTOM:
{
System.out.println("Custom");
byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
dest = ByteBuffer.allocateDirect(99999);
dest.order(ByteOrder.nativeOrder());
dest.put(data, 0, data.length);
break;
}
case BufferedImage.TYPE_INT_RGB:
{
int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
dest = ByteBuffer.allocateDirect(data.length * BufferUtil.SIZEOF_INT);
dest.order(ByteOrder.nativeOrder());
dest.asIntBuffer().put(data, 0, data.length);
break;
}
default:
throw new RuntimeException("Unsupported image type " + img.getType());
}
if (mipmapped)
{
glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
}
else
{
gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
}
}
private int genTexture(GL gl)
{
final int[] tmp = new int[1];
gl.glGenTextures(1, tmp, 0);
return tmp[0];
}
}
I don't see that you check for any OpenGL-errors. JOGL provides an easy way to do this. Just place glDrawable.setGL(new DebugGL(glDrawable.getGL())); at the beginning of your init method.
If that doesn't help I would check the image data and texture variable if they are non-zero.
I managed to get a texture loaded by using com.sun.opengl.util.texture.Texture and the tutorial I found here.

Categories

Resources