I am trying to add a texture to a cube that is made of triangles but it is not working. The cube displays but when I uncomment the line gl.glEnable(GL2.GL_TEXTURE_2D); nothing shows up in the frame. Here is the source code:
import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GL2ES1; import com.jogamp.opengl.GL3; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLException; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.Animator; import com.jogamp.opengl.util.awt.ImageUtil; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureIO; import com.jogamp.opengl.util.texture.awt.AWTTextureIO; /** * Simple Implements GLEventListener so that I can modify * what happens when application runs * * */ public class Simple extends GLCanvas implements GLEventListener { private static final long serialVersionUID = 1L; private double eyeX = 5; private double eyeY = 5; private double eyeZ = 5; private float o = 0.0001f; // this is the title of the frame static final String TITLE = "Simple"; // these are the dimensions of the frame static final int CANVAS_WIDTH = 500; static final int CANVAS_HEIGHT = 500; int texture; Texture t; public static void main (String [] args) { // this class extends the GLCanvas class // so that I have something to reference // when I want to draw GLCanvas canvas = new Simple(); canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); // create a new frame to hold the canvas JFrame frame = new JFrame(); // add the canvas to the frame frame.getContentPane().add(canvas); // set the title of the frame frame.setTitle(TITLE); // this is not required but is convenient. When you // click the 'x' in the top left of the screen, the // application will close frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // pack the frame frame.pack(); // then make it displayable to the user frame.setVisible(true); // Create an animator object and then add it to // the canvas and start the animation Animator animator = new Animator(canvas); animator.start(); } // constructor for the class // you need to add a GLEventListener // to it so that you can draw with the gl methods public Simple () { this.addGLEventListener(this); } @Override public void init(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glEnable(GL2.GL_DEPTH_TEST); //gl.glEnable(GL2.GL_TEXTURE_2D); try { File im = new File("calvin-and-hobbes.png"); t = TextureIO.newTexture(im, true); texture = t.getTextureObject(gl); gl.glBindTexture(GL2.GL_TEXTURE_2D, texture); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_CLAMP_TO_EDGE); } catch(IOException e) { e.printStackTrace(); } } @Override public void dispose(GLAutoDrawable drawable) {} @Override public void display(GLAutoDrawable drawable) { // create objects used for drawing final GL2 gl = drawable.getGL().getGL2(); GLU glu = new GLU(); transformEye(); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(40.0, 1.0, 1.0, 10000.0); //gl.glMatrixMode(GL2.GL_TEXTURE); //gl.glLoadIdentity(); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); //gl.glEnable(GL2.GL_TEXTURE_2D); //t.bind(gl); //t.enable(gl); //gl.glBindTexture(GL2.GL_TEXTURE_2D, texture); drawTriangles(gl); //gl.glFlush(); // increment the angle so that the viewer rotates //o += 0.0001f; } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {} /** * This method rotates the viewer */ private void transformEye() { // use o for theta which is the amount of degrees eyeX = (5 * Math.cos(o)) + (5 * Math.sin(o)); eyeZ = (-5 * Math.sin(o)) + (5 * Math.cos(o)); } /** * This method draws all the triangles as the viewer rotates * * @param gl */ private void drawTriangles(GL2 gl) { // gl.glColor3f(1.0f, 1.0f, 1.0f); // gl.glColor3f(1.0f, 0.0f, 0.0f); // Triangle 0 gl.glBegin(GL2.GL_TRIANGLES); gl.glTexCoord2f(0f, 0f); gl.glVertex3f(0.5f, 0.5f, 0.5f); gl.glTexCoord2f(0f, 1f); gl.glVertex3f(0.5f, -0.5f, 0.5f); gl.glTexCoord2f(1f, 0f); gl.glVertex3f(-0.5f, 0.5f, 0.5f); gl.glEnd(); // Triangle 1 gl.glBegin(GL2.GL_TRIANGLES); // gl.glTexCoord3f(-0.5f, -0.5f, 0.5f); gl.glVertex3f(-0.5f, -0.5f, 0.5f); // gl.glTexCoord3f(0.5f, -0.5f, 0.5f); gl.glVertex3f(0.5f, -0.5f, 0.5f); // gl.glTexCoord3f(-0.5f, 0.5f, 0.5f); gl.glVertex3f(-0.5f, 0.5f, 0.5f); gl.glEnd(); // gl.glColor3f(0.0f, 0.5f, 0.0f); // Triangle 2 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(0.5f, 0.5f, 0.5f); gl.glVertex3f(0.5f, 0.5f, -0.5f); gl.glVertex3f(-0.5f, 0.5f, 0.5f); gl.glEnd(); // Triangle 3 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, 0.5f, -0.5f); gl.glVertex3f(0.5f, 0.5f, -0.5f); gl.glVertex3f(-0.5f, 0.5f, 0.5f); gl.glEnd(); // gl.glColor3f(0.0f, 0.0f, 0.5f); // Triangle 4 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(0.5f, -0.5f, 0.5f); gl.glVertex3f(0.5f, 0.5f, 0.5f); gl.glVertex3f(0.5f, 0.5f, -0.5f); gl.glEnd(); // Triangle 5 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(0.5f, -0.5f, 0.5f); gl.glVertex3f(0.5f, -0.5f, -0.5f); gl.glVertex3f(0.5f, 0.5f, -0.5f); gl.glEnd(); // gl.glColor3f(0.5f, 0.0f, 0.5f); // Triangle 6 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, -0.5f, 0.5f); gl.glVertex3f(-0.5f, -0.5f, -0.5f); gl.glVertex3f(0.5f, -0.5f, -0.5f); gl.glEnd(); // Triangle 7 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, -0.5f, 0.5f); gl.glVertex3f(0.5f, -0.5f, 0.5f); gl.glVertex3f(0.5f, -0.5f, -0.5f); gl.glEnd(); // gl.glColor3f(0.0f, 0.2f, 0.613f); // Triangle 8 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, -0.5f, 0.5f); gl.glVertex3f(-0.5f, -0.5f, -0.5f); gl.glVertex3f(-0.5f, 0.5f, -0.5f); gl.glEnd(); // Triangle 9 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, 0.5f, 0.5f); gl.glVertex3f(-0.5f, -0.5f, 0.5f); gl.glVertex3f(-0.5f, 0.5f, -0.5f); gl.glEnd(); // gl.glColor3f(0.6134f, 0.4513f, 0.1423f); // Triangle 10 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, -0.5f, -0.5f); gl.glVertex3f(0.5f, -0.5f, -0.5f); gl.glVertex3f(0.5f, 0.5f, -0.5f); gl.glEnd(); // Triangle 11 gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3f(-0.5f, -0.5f, -0.5f); gl.glVertex3f(-0.5f, 0.5f, -0.5f); gl.glVertex3f(0.5f, 0.5f, -0.5f); gl.glEnd(); } public static Texture loadTexture(String file) throws GLException, IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(ImageIO.read(new File(file)), "png", os); InputStream fis = new ByteArrayInputStream(os.toByteArray()); return TextureIO.newTexture(fis, true, TextureIO.PNG); } } |
This post was updated on .
Thanks I'll look into that. So my code displays a black screen because it's deprecated? Specifically, I'm interested in why my screen displays a cube when the line gl.glEnable(GL2.GL_TEXTURE_2D); is commented out but shows a black screen when it is uncommented.
|
No, but it's no worth to look into that, because you can't really transfer the knowledge when you will learn current OpenGL. You should put your vertices and texture coordinates into a buffer, transfer it on the gpu. Express transformations through matrices and put them into buffer or uniform variable. And then tell OpenGL how to fetch the data from the buffer and use the matrices to multiply your vertices in the shaders. |
Administrator
|
This post was updated on .
In reply to this post by John
Your texture coordinates were wrong. Each vertex of each triangle should have some texture coordinates. Please look at the rudimentary example in the wiki and in jogl-demos.
Have you looked at this simple example? https://github.com/JogAmp/jogl-demos/blob/master/src/redbook/src/glredbook11/texbind.java
Julien Gouesse | Personal blog | Website
|
Free forum by Nabble | Edit this page |