Why won't my texture show up? - java

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.

Related

How to draw a circle in OpenGL 1.0 in Android Studio that moves from the center to right and then to left

This is my code, before some changes it was drawing a blue circle on green background. I don't have any idea how to fix that problem. I was trying many things. The first problem is that it looks distorted, it it more oval that circle, first idea was to create seperate function which was called out in onSurfaceChanged
DrawCircle(gl, width, height) it helped me to get correct shape of circle, but I was not able to move it like in the pattern shown below.
package pl.varkame;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.os.SystemClock;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyRenderer implements GLSurfaceView.Renderer {
private float kat=0;
public FloatBuffer vertexBuffer;
private DrawCircle Circlek;
private int n_seg=720; // number of segments
float[] vertices = new float[n_seg*2+2];
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Circlek = new DrawCircle();
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio = (float) width/(float) height;
GLU.gluPerspective(gl,45.0f,(float)width/(float)height,-1.0f, -10.0f);
gl.glOrthof(-ratio, ratio, -1.0f, 1.0f, -1.0f, 1.0f);
gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
}
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(0.0f , 0.0f , 1.0f , 1.0f);
gl.glLoadIdentity();
Circlek.draw();
}
}
class DrawCircle{
public FloatBuffer vertexBuffer;
public int c=0;
private int n_seg=720; // number of segments
float[] vertices = new float[n_seg*2+2];
public float width = 100;
public float height= 50;
float ratio = width/height;
public void Circle(GL10 gl) {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2 , GL10.GL_FLOAT , 0 , vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN , 0 , vertices.length / 2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
public void draw(){
for (int i=0; i<n_seg;i++){
float radius=0.1f ;
double rad=(Math.PI*2*i/n_seg);
vertices[c]=(float)Math.cos(rad)*radius;
vertices[c+1]=(float) Math.sin(rad)*(radius/ratio);
c+=2;
}
}
}
I was trying to animate this movement with any success
This was last working code, circle was distorted in it
package pl.varkame;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyRenderer implements GLSurfaceView.Renderer {
private float kat=0;
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl,45.0f,(float)width/(float)height,-1.0f, -10.0f);
gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
}
#Override
public void onDrawFrame(GL10 gl) {
FloatBuffer vertexBuffer;
int c=0;
int n_seg=720; // number of segments
float[] vertices = new float[n_seg*2+2];
for (int i=0; i<n_seg;i++){
float radius=0.1f ; // 10% z szerokości ekranu
double rad=(Math.PI*2*i/n_seg);
vertices[c]=(float)Math.cos(rad)*radius;
vertices[c+1]=(float) Math.sin(rad)*radius;
c+=2;
}
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glLoadIdentity();
gl.glTranslatef(0.0f,0.0f,0.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,0,vertices.length/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}

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.

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

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

Textures in OpenGL ES 2.0 for Android

I'm new to OpenGL and I'm teaching myself by making a 2D game for Android with ES 2.0. I am starting off by creating a "Sprite" class that creates a plane and renders a texture to it. To practice, I have two Sprite objects that are drawn alternating in the same place. I got this much working fine and well with ES 1.0, but now that I've switched to 2.0, I am getting a black screen with no errors. I'm exhausted trying to figure out what I'm doing wrong, but I have a strong feeling it has to do with my shaders. I'm going to dump all the relevant code here and hopefully somebody can give me an answer or some advice as to what I'm doing wrong. And if it's not immediately apparent what I'm doing wrong, perhaps some advice on how to figure it out? Thanks in advance for looking through all the code I'm about to post.
The three classes I'm posting are:
GameRenderer - the renderer for my GLSurfaceView
Shader - creates a shader program object
Sprite - creates a square and draws a texture on it
Also, I'll post my vertex and fragment shader source.
Related classes I didn't think were relevant enough to post:
GameActivity
GameView - A GLSurfaceView
GameLoopThread - My main game loop
FPSCounter - outputs the average FPS to logcat every 100 frames.
GameRender class:
package com.detour.raw;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLU;
import android.opengl.Matrix;
import android.opengl.GLSurfaceView;
public class GameRenderer implements GLSurfaceView.Renderer{
private static final String LOG_TAG = GameRenderer.class.getSimpleName();
Context mContext;
Bitmap bitmap;
private float red = 0.0f;
private float green = 0.0f;
private float blue = 0.0f;
Shader shader;
FPSCounter fps;
Sprite sprite;
Sprite sprite2;
int x = 0;
private float[] mProjMatrix = new float[16];
private float[] mVMatrix = new float[16];
//int[] vertexShader;
//int[] fragmentShader;
//int program;
//String vShaderSource = "";
//String fShaderSource = "";
public GameRenderer(Context context){
mContext = context;
//create objects/sprites
sprite = new Sprite(mContext);
sprite2 = new Sprite(mContext);
fps = new FPSCounter();
}
#Override
public void onDrawFrame(GL10 gl) {
GLES20.glClearColor(red, green, blue, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
if(x>3){
x=0;
}
if(x%2==0){
sprite.draw(gl);
}else{
sprite2.draw(gl);
}
x++;
fps.calculate();
//fps.draw(gl);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float)(width/height);
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 0.5f, 10);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glClearDepthf(1.0f);
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
GLES20.glDepthMask(true);
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glCullFace(GLES20.GL_BACK);
GLES20.glClearColor(red, green, blue, 1.0f);
//load sprite/object textures (preferably loop through an array of all sprites).
sprite.loadGLTexture(gl, mContext, R.drawable.raw1);
sprite2.loadGLTexture(gl, mContext, R.drawable.raw2);
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5.0f, 0.0f, 0f, 0f, 0f, 0.0f, 0.0f);
System.gc();
}
}
Shader class:
package com.detour.raw;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;
public class Shader {
public static final String TAG = Shader.class.getSimpleName();
int program;
int vertexShader;
int fragmentShader;
String vShaderSource;
String fShaderSource;
public Shader(){
//blank constructor
//createProgram();
}
public Shader(String vs_source, String fs_source){
this.vShaderSource = vs_source;
this.fShaderSource = fs_source;
createProgram();
}
public Shader(int vs_source_id, int fs_source_id, Context context) {
StringBuffer vs = new StringBuffer();
StringBuffer fs = new StringBuffer();
try{
InputStream inputStream = context.getResources().openRawResource(vs_source_id);
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String read = in.readLine();
while (read != null) {
vs.append(read + "\n");
read = in.readLine();
}
vs.deleteCharAt(vs.length() - 1);
inputStream = context.getResources().openRawResource(fs_source_id);
in = new BufferedReader(new InputStreamReader(inputStream));
read = in.readLine();
while (read != null) {
fs.append(read + "\n");
read = in.readLine();
}
fs.deleteCharAt(fs.length() - 1);
}catch (Exception e){
Log.d("ERROR-readingShader", "Could not read shader: " + e.getLocalizedMessage());
}
this.vShaderSource = vs.toString();
this.fShaderSource = fs.toString();
createProgram();
}
private void createProgram(){
program = GLES20.glCreateProgram();
if(program!=0){
vertexShader = createShader(GLES20.GL_VERTEX_SHADER, vShaderSource);
fragmentShader = createShader(GLES20.GL_FRAGMENT_SHADER, fShaderSource);
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
GLES20.glLinkProgram(program);
}else{
Log.e(TAG, "Couldn't create program.");
}
}
private int createShader(int type, String source){
int shader = GLES20.glCreateShader(type);
if(shader!=0){
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
}
return shader;
}
public int getProgram(){
return program;
}
Sprite class:
package com.detour.raw;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
public class Sprite {
//public static final int FRAME_WIDTH = 64;
//public static final int FRAME_HEIGHT = 64;
private static final String LOG_TAG = Sprite.class.getSimpleName();
Context mContext;
Bitmap bitmap;
private int textureLoc;
private int vertexLoc;
private int[] textures = new int[1];
//private int[] pixels;
/*private float textureCoordinates[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f};*/
private float vertices[] = {
-1.0f, 1.0f,// 0.0f,
-1.0f, -1.0f,// 0.0f,
1.0f, -1.0f,// 0.0f,
1.0f, 1.0f// 0.0f
};
private short[] indices = {
0, 1, 2,
0, 2, 3};
private FloatBuffer vertexBuffer;
//private IntBuffer textureBuffer;
private ShortBuffer indexBuffer;
Shader shader;
int program;
String vShaderSource = "";
String fShaderSource = "";
public Sprite(Context context){
mContext = context;
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl) {
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_FLOAT, indexBuffer);
}
public void loadGLTexture(GL10 gl, Context context, int id){
shader = new Shader(R.raw.sprite_vs, R.raw.sprite_fs, mContext);
program = shader.getProgram();
GLES20.glUseProgram(program);
vertexLoc = GLES20.glGetAttribLocation(program, "a_position");
textureLoc = GLES20.glGetUniformLocation(program, "u_texture"); //texture
InputStream is = context.getResources().openRawResource(id);
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
}
}
//pixels = new int[(bitmap.getWidth()*bitmap.getHeight())];
//bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
/*ByteBuffer byteBuf = ByteBuffer.allocateDirect(pixels.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asIntBuffer();
textureBuffer.put(pixels);
textureBuffer.position(0);*/
GLES20.glDeleteTextures(1, textures, 0);
GLES20.glGenTextures(1, textures, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glUniform1i(textureLoc, 0);
GLES20.glEnableVertexAttribArray(vertexLoc);
GLES20.glVertexAttribPointer(vertexLoc, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
//GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, FRAME_WIDTH, FRAME_HEIGHT, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, byteBuf);//(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}
Vertex shader (sprite_vs.txt):
#version 110
attribute vec2 a_position;
varying vec2 v_texcoord;
void main()
{
gl_Position = vec4(a_position, 0.0, 1.0);
v_texcoord = a_position * vec2(0.5) + vec2(0.5);
}
Fragment (pixel) shader (sprite_fs.txt):
#version 110
uniform sampler2D u_texture;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = texture2D(u_texture, v_texcoord);
}
Thank you so much if you actually took the time to look through this! Hopefully someone else can use this as a resource for themselves in the future, also.
A few observations/questions:
I don't know how you changed the fragment shader, but the version that is currently posted needs a precision specifier. Just add:
precision mediump float;
to the top, and it should work. Now regarding the black screen here are some questions:
When you change the glClearColor to something not black and comment out all the draw commands, does it still look black? If so, then you have a bigger problem than textures.
Second, if you ignore the texture output and try drawing each sprite as just a flat colored rectangle with no texture data, what do you get? You should be able to see some colored rectangle on the screen.
Finally, you need to bind the texture before you call glDrawElements. (Though this shouldn't matter in this example since you haven't changed the state yet.)

Categories

Resources