Hello Guest

Rotate a textured quad at its center in glorth2d

  • 6 Replies
  • 22354 Views
Rotate a textured quad at its center in glorth2d
« on: September 28, 2004, 16:20:18 »
Yes, I am an LWJGL newbie.
My render code is as follows and I am using the Game.java princec posted on JGO as a skeleton game class

Code: [Select]
  GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
  // TODO: all your rendering goes here
 
  // begin quad

  GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[0]);
  GL11.glBegin(GL11.GL_QUADS);

GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2i((int)(400 - 50), (int)(300 + 50));
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2i((int)(400 - 50), (int)(300 - 50));
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2i((int)(400 + 50), (int)(300 - 50));
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2i((int)(400 + 50), (int)(300 + 50));

 // End quad drawing.
GL11.glEnd();

This puts a square in the center of my 800 * 600 screen with my textue on it.  (attempting a 2d game)  Now I want to spin/rotate the quad by its center, thus my ship can rotate.
I used
Code: [Select]
GL11.glRotatef(0.1f, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis
but this rotates is about the 0,0 location of the screen.  I feel like I am missing a translate somewhere?
S

Rotate a textured quad at its center in glorth2d
« Reply #1 on: September 28, 2004, 17:05:02 »
Instead of using a quad, use triangles (or a triangle strip).  Draw 4 triangles that when put together have one common point, also form a quad.  This will draw your quad with its own centre in the centre, then a rotate will now give you what you want.
Code: [Select]

     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
     // TODO: all your rendering goes here
     
     // begin quad
   
     GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[0]);
   GL11.glBegin(GL11.GL_TRIANGLES);

      GL11.glTexCoord2f(1.0f, 0.0f);
      GL11.glVertex2i(-200, -200);
      GL11.glTexCoord2f(1.0f, 1.0f);
      GL11.glVertex2i(200, -200);
      GL11.glTexCoord2f(0.5f, 0.5f);
      GL11.glVertex2i(0, 0);

      GL11.glTexCoord2f(1.0f, 1.0f);
      GL11.glVertex2i(200, -200);
      GL11.glTexCoord2f(0.0f, 1.0f);
      GL11.glVertex2i(200, 200);
      GL11.glTexCoord2f(0.5f, 0.5f);
      GL11.glVertex2i(0, 0);

      GL11.glTexCoord2f(0.0f, 1.0f);
      GL11.glVertex2i(200, 200);
      GL11.glTexCoord2f(0.0f, 0.0f);
      GL11.glVertex2i(-200, 200);
      GL11.glTexCoord2f(0.5f, 0.5f);
      GL11.glVertex2i(0, 0);

      GL11.glTexCoord2f(0.0f, 0.0f);
      GL11.glVertex2i(-200, 200);
      GL11.glTexCoord2f(1.0f, 0.0f);
      GL11.glVertex2i(-200, -200);
      GL11.glTexCoord2f(0.5f, 0.5f);
      GL11.glVertex2i(0, 0);

     // End quad drawing.
   GL11.glEnd();


It is better to use a triangle strip, but this illustrates the point.  You can see there are 4 triangles drawn and they all have 1 common point.
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)

Thanks
« Reply #2 on: September 28, 2004, 20:05:33 »
Now I have a rotating ship.  It is in the upper left corner, now to translate to where I want it?
S

*

Offline princec

  • *****
  • 1933
    • Puppygames
Rotate a textured quad at its center in glorth2d
« Reply #3 on: September 28, 2004, 21:17:13 »
Code: [Select]

GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0.0f);
drawShip();
GL11.glPopMatrix();


Cas :)

dag nabit, I am a clubie
« Reply #4 on: September 29, 2004, 12:52:07 »
this code starts ship in center and rotates around the 0,0 point (top left corner)
Code: [Select]
private static void render() {
  GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
  // TODO: all your rendering goes here
 
  // rotate at center?
GL11.glRotatef(0.1f, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis

  GL11.glPushMatrix();
  GL11.glTranslatef(400,300, 0.0f);

// begin quad with texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[0]);
    GL11.glBegin(GL11.GL_TRIANGLES);

               // draw triangles from above goes here

     // End quad drawing.
   GL11.glEnd();
   
   GL11.glPopMatrix();


and this code again puts ship in center, but no rotation

Code: [Select]
private static void render() {
  GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
  // TODO: all your rendering goes here

  GL11.glPushMatrix();
  GL11.glTranslatef(400,300, 0.0f);

  // rotate at center?
GL11.glRotatef(0.1f, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis

// begin quad with texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[0]);
    GL11.glBegin(GL11.GL_TRIANGLES);

               // draw triangles from above goes here
 
     // End quad drawing.
   GL11.glEnd();
   
   GL11.glPopMatrix();


phiddlesticks!  I know I can get this... :oops:
S

Rotate a textured quad at its center in glorth2d
« Reply #5 on: September 30, 2004, 02:27:56 »
Here is a modified NeHe tutorial that should explain it.  You can cut and paste it and it should work as is.  Make sure you link in the LWJGL library.
Code: [Select]
/*
 *      This Code Was Created By Jeff Molofee and GB Schmick 2000
 *      A HUGE Thanks To Fredric Echols For Cleaning Up
 *      And Optimizing The Base Code, Making It More Flexible!
 *      If You've Found This Code Useful, Please Let Me Know.
 *      Visit Our Sites At www.tiptup.com and nehe.gamedev.net
 */

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;
import org.lwjgl.input.Keyboard;

public class Test {
    private boolean done = false;
    private boolean fullscreen = false;
    private final String windowTitle = "Centre of rotation";
    private boolean f1 = false; // F1 key pressed
    private DisplayMode displayMode;
   
    private float rotate = 0.0f;
    private float rotateVelocity = 0.0f;
    private float transx = 0.0f;
    private float transy = 0.0f;

    public static void main(String args[]) {
        boolean fullscreen = false;
        if(args.length>0) {
            if(args[0].equalsIgnoreCase("fullscreen")) {
                fullscreen = true;
            }
        }

        Test l23 = new Test();
        l23.run(fullscreen);
    }
    public void run(boolean fullscreen) {
        this.fullscreen = fullscreen;
        try {
            init();

            while (!done) {
                render();
                mainloop();
                Display.update();
            }
            cleanup();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
    private void mainloop() {
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {       // Exit if Escape is pressed
            done = true;
        }
        if(Display.isCloseRequested()) {                     // Exit if window is closed
            done = true;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) {    // Is F1 Being Pressed?
            f1 = true;                                      // Tell Program F1 Is Being Held
            switchMode();                                   // Toggle Fullscreen / Windowed Mode
        }
        if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) {          // Is F1 Being Pressed?
            f1 = false;
        }

        if(Keyboard.isKeyDown(Keyboard.KEY_PRIOR)) {
            rotateVelocity += 0.001f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_NEXT)) {
            rotateVelocity -= 0.001f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
            transx -= 0.002f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
            transx += 0.002f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
            transy -= 0.002f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
            transy += 0.002f;
        }
    }

    private void switchMode() {
        fullscreen = !fullscreen;
        try {
            Display.setFullscreen(fullscreen);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void createWindow() throws Exception {
        if (fullscreen) {
            Display.setFullscreen(true);
        }
        else {
            Display.setFullscreen(false);
        }
        DisplayMode d[] = Display.getAvailableDisplayModes();
        for (int i = 0; i < d.length; i++) {
            if (d[i].getWidth() == 640
                && d[i].getHeight() == 480
                && d[i].getBitsPerPixel() == 32) {
                displayMode = d[i];
                break;
            }
        }
        Display.setDisplayMode(displayMode);
        Display.setTitle(windowTitle);
        Display.create();
    }
    private void init() throws Exception {
        createWindow();

        initGL();
    }


    private void initGL() {                                         // All Setup For OpenGL Goes Here
        int width = 640;
        int height = 480;
        GL11.glEnable(GL11.GL_TEXTURE_2D);                            // Enable Texture Mapping
        GL11.glShadeModel(GL11.GL_SMOOTH);                            // Enable Smooth Shading
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
        GL11.glClearDepth(1.0f);                                 // Depth Buffer Setup
        GL11.glEnable(GL11.GL_DEPTH_TEST);                            // Enables Depth Testing
        GL11.glDepthFunc(GL11.GL_LEQUAL);                             // The Type Of Depth Testing To Do
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);  // Really Nice Perspective Calculations

        GL11.glViewport(0,0,width,height);                           // Reset The Current Viewport

        GL11.glMatrixMode(GL11.GL_PROJECTION);                            // Select The Projection Matrix
        GL11.glLoadIdentity();                                       // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(45.0f,
                (float) displayMode.getWidth() / (float) displayMode.getHeight(),
                0.1f,100.0f);

        GL11.glMatrixMode(GL11.GL_MODELVIEW);                             // Select The Modelview Matrix
        GL11.glLoadIdentity();                                       // Reset The Modelview Matrix
    }

    private void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glTranslatef(transx, transy, -6.0f);
        GL11.glRotatef(rotate, 0.0f, 0.0f, 1.0f);
        GL11.glBegin(GL11.GL_TRIANGLES);

            GL11.glColor3f(1.0f, 0.0f, 0.0f);
            GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
            GL11.glColor3f(0.0f, 1.0f, 0.0f);
            GL11.glVertex3f( 1.0f, 1.0f, 0.0f);
            GL11.glColor3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f( 0.0f, 0.0f, 0.0f);

            GL11.glColor3f(0.0f, 1.0f, 0.0f);
            GL11.glVertex3f( 1.0f, 1.0f, 0.0f);
            GL11.glColor3f(0.0f, 0.0f, 1.0f);
            GL11.glVertex3f( 1.0f,-1.0f, 0.0f);
            GL11.glColor3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f( 0.0f, 0.0f, 0.0f);
       
            GL11.glColor3f(0.0f, 0.0f, 1.0f);
            GL11.glVertex3f( 1.0f,-1.0f, 0.0f);
            GL11.glColor3f(1.0f, 0.0f, 1.0f);
            GL11.glVertex3f(-1.0f,-1.0f, 0.0f);
            GL11.glColor3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f( 0.0f, 0.0f, 0.0f);
           
            GL11.glColor3f(1.0f, 0.0f, 1.0f);
            GL11.glVertex3f(-1.0f,-1.0f, 0.0f);
            GL11.glColor3f(1.0f, 0.0f, 0.0f);
            GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
            GL11.glColor3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f( 0.0f, 0.0f, 0.0f);

        GL11.glEnd();
       
        rotate += rotateVelocity;
    }

    private void cleanup() {
        Display.destroy();
    }
}
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)

Tanks
« Reply #6 on: September 30, 2004, 14:18:12 »
All is well, I have a centered, rotating ship.  Off to the next hurdle!

Thanks to all!

 :D
S