Sign in to follow this  

Rotating Image Along Y axis in JOGL so that it is horizontal (Visible side faces upward)

This topic is 2323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am new to JOGL and have tried to display an image in JOGL from an example. [url="http://wiki.tankaar.com/index.php?title=Displaying_an_Image_in_JOGL_%28Part_1%29"]http://wiki.tankaar.com/index.php?title=Displaying_an_Image_in_JOGL_%28Part_1%29[/url]

However , this example displays the image in 2D, with the right side facing the user(i-e leaf is visible to user), **I wanted to change the code so that i can rotate the image along Y-axis, so that leaf is facing upwards and is not visible to user, and if i rotate it 30 degrees,

[indent] (gl.glRotated(30, 0, 1, 0);)

[/indent] some part of image should be visible to user.** ,Below is the changing that i have done in the code, but it does not show the image, can any one tell me what am i doing wrong here.. Any Help will be GREATLY Appreciated, Thanks










import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
//import net.java.games.jogl.GL;
//import net.java.games.jogl.GLCanvas;
//import net.java.games.jogl.GLDrawable;
//import net.java.games.jogl.GLEventListener;

public class Image3DGraphics {
public Image3DGraphics() {
JFrame frame = new JFrame("2D Graphics");
frame.setSize(800,900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener((GLEventListener) new JOGLListener());
frame.add(canvas);
frame.setVisible(true);
}
public static void main(String args[]){
new Image3DGraphics();
}

private class JOGLListener implements GLEventListener {


public void display(GLAutoDrawable drawable) {
System.out.println("DISPLAY CALLED");
GL gl = drawable.getGL();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, 300, 300, 0, 0, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glDisable(GL.GL_DEPTH_TEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable (GL.GL_BLEND);
BufferedImage bufferedImage = null;
int w = 10;
int h = 10;
int d=10;
try {
bufferedImage = ImageIO.read(Image3DGraphics.class.getResource("leaf.png"));
w = bufferedImage.getWidth();
h = bufferedImage.getHeight();
d = bufferedImage.getWidth();
} catch (IOException e) {
e.printStackTrace();
}
WritableRaster raster =
Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE,
w,
h,
4,
null);
ComponentColorModel colorModel=
new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
BufferedImage dukeImg =
new BufferedImage (colorModel,
raster,
false,
null);

Graphics2D g = dukeImg.createGraphics();
g.drawImage(bufferedImage, null, null);
DataBufferByte dukeBuf =
(DataBufferByte)raster.getDataBuffer();
byte[] dukeRGBA = dukeBuf.getData();
ByteBuffer bb = ByteBuffer.wrap(dukeRGBA);
bb.position(0);
bb.mark();
gl.glBindTexture(GL.GL_TEXTURE_3D, 13);
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glTexImage3D (GL.GL_TEXTURE_3D, 1, GL.GL_RGBA, w, h, d,1, GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE, bb);

int left = 100;
int top = 100;
gl.glEnable(GL.GL_TEXTURE_3D);
gl.glBindTexture (GL.GL_TEXTURE_3D, 13);


// gl.glPushMatrix();
// gl.glTranslated(100, 100, 0);
gl.glRotated(30, 0, 1, 0);
// gl.glTranslated(-100, -100, 0);
gl.glBegin (GL.GL_POLYGON);
gl.glTexCoord3d (0, 0,0);
gl.glVertex3d (left,top,0);
gl.glTexCoord3d(1,0,0);
gl.glVertex3d (left + w, top,0);
gl.glTexCoord3d(1,1,+1.0);
gl.glVertex3d (left + w, top + h,+1.0);
gl.glTexCoord3d(0,1,+1.0);
gl.glVertex3d (left, top + h,+1.0);
// gl.glTexCoord2d (0, 0);
// gl.glVertex2d (left,top);
// gl.glTexCoord2d(1,0);
// gl.glVertex2d (left + w, top);
// gl.glTexCoord2d(1,1);
// gl.glVertex2d (left + w, top + h);
// gl.glTexCoord2d(0,1);
// gl.glVertex2d (left, top + h);
gl.glEnd ();

gl.glFlush();


}


public void displayChanged(GLAutoDrawable arg0, boolean arg1,
boolean arg2) {
System.out.println("DISPLAY CHANGED CALLED");
}


public void init(GLAutoDrawable drawable) {
System.out.println("INIT CALLED");
}


public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
System.out.println("RESHAPE CALLED");

}

public void init(GLDrawable gld) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void display(GLDrawable gld) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void reshape(GLDrawable gld, int i, int i1, int i2, int i3) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void displayChanged(GLDrawable gld, boolean bln, boolean bln1) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}



Share this post


Link to post
Share on other sites
Sign in to follow this