0
votes

So I want to rotate the object , I call glPushMatrix() , to save the matrix,glTranslatef because I want the object to have it's own axis so I'll move the object in the middle of the screen, then I do a rotate , call glPopMatrix() and nothing happens :D

package Tanc;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.*;
public class Tanc{

public Tanc(){
    try{
        Display.setDisplayMode(new DisplayMode(640,480));
        Display.setTitle("Tancc");
        Display.create();
    }catch(LWJGLException e){
        e.printStackTrace();
    }
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glOrtho(1, 1, 1, 1, 1, -1);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glMatrixMode(GL11.GL_PROJECTION);

    float y_angle = 0;

    while (!Display.isCloseRequested()) {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GL11.glPushMatrix(); 

/*     
        GL11.glRectd(0, 0, 0.2f, 0.3f);
       // GL11.glRotated(50, 1, 0, 0);
      //  GL11.glRotated(50, 0, 1, 0);
        GL11.glRotated(1, 0, 0, 1);
    //    GL11.glTranslatef(0, 0, 0);
        GL11.glTranslatef(-0.1f, -0.15f, 0);
     //   GL11.glTranslatef(-1f, -0.15f, 0);
        GL11.glPopMatrix();
*/
        GL11.glLoadIdentity();
        GL11.glPushMatrix();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(0f, 0f);
        GL11.glVertex2f(0.2f, 0f);
        GL11.glVertex2f(0.2f, 0.3f);
        GL11.glVertex2f(0f, 0.3f);
        GL11.glEnd();
        GL11.glTranslatef(-0.1f, -0.15f, 0);
        GL11.glRotatef(30, 1, 0, 1);
        GL11.glPopMatrix();
        Display.update();
        Display.sync(60);
    }       
    Display.destroy();
    System.exit(0);
}

public static void main(String[] args){
    new Tanc();
}

}

3

3 Answers

3
votes

This is because you're doing the same thing each frame.

You are: creating a triangle, translating it, and rotating it from 0 to 30 degrees. The next frame, everything resets, and it does the exact same thing, rotates from 0 to 30 degrees.

You need some kind of persistent data.

GL11.glRotatef(xRot, 1, 0, 1);
//...
xRot = xRot > 360 ? 0 : xRot + 10;
2
votes

Captain Skyhawk's answer is correct.

Additionally, do your transformations BEFORE drawing.

0
votes

I finally fixed it :) and it rotates only one object :D .

 package Tanc;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.*;
public class Tanc{
private void useless(){
    /*     
    GL11.glRectd(0, 0, 0.2f, 0.3f);
   // GL11.glRotated(50, 1, 0, 0);
  //  GL11.glRotated(50, 0, 1, 0);
    GL11.glRotated(1, 0, 0, 1);
//    GL11.glTranslatef(0, 0, 0);
    GL11.glTranslatef(-0.1f, -0.15f, 0);
 //   GL11.glTranslatef(-1f, -0.15f, 0);
    GL11.glPopMatrix();
*/

       //          GL11.glLoadIdentity();
         }
    public Tanc(){

    try{
        Display.setDisplayMode(new DisplayMode(640,480));
        Display.setTitle("Tancc");
        Display.create();
    }catch(LWJGLException e){
        e.printStackTrace();
    }
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glOrtho(1, 1, 1, 1, 1, -1);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glMatrixMode(GL11.GL_PROJECTION);

    float y_angle = 0;
    float xrot= Mouse.getDY();
    float yrot =Mouse.getDX();
    float a=0,b=0,c=0;
    while (!Display.isCloseRequested()) {  

        GL11.glPushMatrix();
        GL11.glRotatef(yrot, 0, 0, 1);
        GL11.glRotatef(xrot, 0, 0, 1);
        GL11.glTranslatef(a, b, 0);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);


        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(0f, 0f);
        GL11.glVertex2f(0.2f, 0f);
        GL11.glVertex2f(0.2f, 0.3f);
        GL11.glVertex2f(0f, 0.3f);   
        GL11.glEnd(); 



       if(Keyboard.isKeyDown(Keyboard.KEY_A)){
        xrot = xrot > 360 ? 0 : xrot + 5;
     //   a += 0.01f;
      //  b += 0.01f;
       }
    //   if(Keyboard.isKeyDown(Keyboard.KEY_D)){
           // if(xrot>360){
           //   xrot = 0;
            //}else {
                xrot += Mouse.getDY()*1f;
                yrot -= Mouse.getDX()*1f;
           // }
    //   }
        GL11.glPopMatrix();

        GL11.glBegin(GL11.GL_QUADS);


        GL11.glVertex2f(0.2f, 0.2f);
        GL11.glVertex2f(0.3f, 0.2f);
        GL11.glVertex2f(0.3f, 0.3f);
        GL11.glVertex2f(0.2f, 0.3f);   
        GL11.glEnd(); 

        Display.update();
        Display.sync(60);
    }       
    Display.destroy();
    System.exit(0);
}

public static void main(String[] args){
    new Tanc();
}

}