Hi
I have some problems with learning basic rotating, I have made a method
for moving a quad and rendering the quad, but when i rotate it always rotates the world, so it doesnt rotate around its own axis.
i want to rotate any object around its own axis without placing it exactly at 0,0
Is this the way its supposed to be? if i load a model then how can i know how to place it at exactly centered at 0,0? adn what if the model moves?
i really need help with this, please help me, i have looked in the opengl red book, and other tutorials + documents and forums and examples but i just cant get this rotating to work.
Here is the code so you can see what i do wrong..
What im trying to do in my render method is, calling firkant() that
moves and rotates and draws a quad on screen. This quad should rotate around its own z axis, but it wont do that unless i draw the quad exactly centered on 0,0
When the quad is drawn im drawing another quad that should not move...
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.openal.AL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.Window;
/**
* $Id: Game.java,v 1.3 2004/05/05 14:35:05 cix_foo Exp $
*
* This is a <em>very basic</em> skeleton to init a game and run it.
*
* @author $Author: cix_foo $
* @version $Revision: 1.3 $
*/
public class Game {
/** Game title */
public static final String GAME_TITLE = "My Game";
/** Desired frame time */
private static final int FRAMERATE = 60;
/** Exit the game */
private static boolean finished;
private static boolean leftpressed =false;
private static boolean rightpressed =false;
private static float rotated = 0.0f;
private static float rtri = 0;
private static float rquad = 0;
private static int firkantz = 0;
/** My texture */
//private static int texture;
/**
* No constructor needed - this class is static
*/
private Game() {}
/**
* Application init
* @param args Commandline args
*/
public static void main(String[] args) {
try {
init();
run();
} catch (Exception e) {
e.printStackTrace(System.err);
Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
} finally {
cleanup();
}
}
/**
* Initialise the game
* @throws Exception if init fails
*/
private static void init() throws Exception {
// Create a fullscreen window with 1:1 orthographic 2D projection, and with
// mouse, keyboard, and gamepad inputs.
Window.create(GAME_TITLE);
// Enable vsync if we can
Window.setVSyncEnabled(true);
// Start up the sound system
AL.create();
// Enable texture mapping
//GL11.glEnable(GL11.GL_TEXTURE_2D);
// TODO: Load in your textures etc here
//texture = loadTexture("data/nehe.png");
}
/**
* Texture loading directly from LWJGL examples
*/
private final static int loadTexture(String path) {
Image image = (new javax.swing.ImageIcon(path)).getImage();
// Exctract The Image
BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D) tex.getGraphics();
g.drawImage(image, null, null);
g.dispose();
// Flip Image
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
tex = op.filter(tex, null);
// Put Image In Memory
ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
scratch.clear();
scratch.put(data);
scratch.rewind();
// Create A IntBuffer For Image Address In Memory
IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glGenTextures(buf); // Create Texture In OpenGL
GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
// Typical Texture Generation Using Data From The Image
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
// Generate The Texture
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
return buf.get(0); // Return Image Address In Memory
}
/**
* Runs the game (the "main loop")
*/
private static void run() {
while (!finished) {
// Always call Window.update(), all the time
Window.update();
if (Window.isCloseRequested()) {
// Check for O/S close requests
finished = true;
} else if (Window.isActive()) {
// The window is in the foreground, so we should play the game
logic();
render();
org.lwjgl.Display.sync(FRAMERATE);
} else {
// The window is not in the foreground, so we can allow other stuff to run and
// infrequently update
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
logic();
if (Window.isVisible() || Window.isDirty()) {
// Only bother rendering if the window is visible or dirty
render();
}
}
}
}
/**
* Do any game-specific cleanup
*/
private static void cleanup() {
// TODO: save anything you want to disk here
// Stop the sound
AL.destroy();
// Close the window
Window.destroy();
}
/**
* Do all calculations, handle input, etc.
*/
private static void logic() {
// Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
finished = true;
}
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
leftpressed=true;
}
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
rightpressed=true;
}
// TODO: all your game logic goes here.
if(rotated==360){
rotated=0;
}
else{
rotated++;firkantz++;
}
}
public static void firkant(){
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glPushMatrix();
GL11.glLoadIdentity();
movefirkant();
renderfirkant();
GL11.glPopMatrix();
}
public static void movefirkant(){
GL11.glTranslatef(400.0f,400.0f,0.0f);
GL11.glRotatef(firkantz,0.0f,0.0f,0.2f);
}
public static void renderfirkant(){
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0.0f, 0.0f, 0.0f);
GL11.glVertex3f(40.0f, 0.0f, 0.0f);
GL11.glVertex3f(40.0f, 40.0f, 0.0f);
GL11.glVertex3f(0.0f, 40.0f, 0.0f);
GL11.glEnd();
}
/**
* Render the current frame
*/
private static void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
firkant();
GL11.glLoadIdentity();
GL11.glTranslatef(400.0f,400.0f,1.0f);
GL11.glRotatef(rtri,0.0f,0.0f,1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(-20.0f, -20.0f, 0.0f);
GL11.glVertex3f(20.0f, -20.0f, 0.0f);
GL11.glVertex3f(20.0f, 20.0f, 0.0f);
GL11.glVertex3f(-20.0f, 20.0f, 0.0f);
GL11.glEnd();
}
}
one of your problems (if not your only problem :wink: ) is that you need to rotate the poly before you translate it in order to create the effect you are going for.
just change:
GL11.glTranslatef(400.0f,400.0f,0.0f);
GL11.glRotatef(firkantz,0.0f,0.0f,0.2f);
in movefirkant() to:
GL11.glRotatef(firkantz,0.0f,0.0f,0.2f);
GL11.glTranslatef(400.0f,400.0f,0.0f);
that will help alot :lol: