mwcurtis65
mwcurtis65

Reputation: 23

lwjgl generating 3D cube gives odd shape

So I've been staring at this code for what seems like an age and a half. I'm a newbie when it comes to lwjgl, but I've got some code running. The problem is that I want a cube, and it's coming back as really strange. Technically, the goal is just to be able to render random number of cubes, but I've cut it down to just one for because of the errors I was running into.

I've looked into VBO, but the rest of my group is all using GL11 - GL13 and seeing as this is the last part, we want to stick with it.

So, in summary, I'm trying to just render the current cube but in a way that later on I can add any number of cubes using the current set up. Any help would be great. I've looked into changing the x, y, and z.

Thank you all so much for any help! And hopefully this is on topic.

package citygeneratorv0;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW; 
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex3f;

public class CityGeneratorV0 {

private static final List<Box> city = new ArrayList<>(16);


static void generateCity(int numBuildings) {
     city.add(new Box());
  }
}

public static void main(String args[]) {
  try {
     Display.setDisplayMode(new DisplayMode(640, 480));
     Display.setTitle("Input Demo");
     Display.create();
  } catch (LWJGLException e) {
     e.printStackTrace();
     Display.destroy();
     System.exit(1);
  }

  int numBuildings = 1;
  generateCity(numBuildings);

  glMatrixMode(GL_PROJECTION);
  glOrtho(0, 640, 480, 0, 1, -1);
  glMatrixMode(GL_MODELVIEW);
  while (!Display.isCloseRequested()) {
     glClear(GL_COLOR_BUFFER_BIT);
     while (Keyboard.next()) {
        if (Keyboard.getEventKey() == Keyboard.KEY_C && Keyboard.getEventKeyState()) {
        }
     }
     if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
        Display.destroy();
        System.exit(0);
     }
     for (final Box box : city) {
        box.draw();
     }
     Display.update();
     Display.sync(5000);
  }
  System.out.print(city.size());
  Display.destroy();
}

private static class Box {
  private float rquad;                

  Boolean draw() {
     GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix
     GL11.glTranslatef(1.5f, 0.0f, -7.0f);             // Move Right 1.5 Units And Into The Screen 6.0
     GL11.glRotatef(rquad, 1.0f, 0.0f, 0.0f);               // Rotate The Quad On The X axis ( NEW )
     glColor3f(0.5f, 0.5f, 1.0f);                 // Set The Color To Blue One Time Only

     glBegin(GL11.GL_QUADS);                        // Draw A Quad
     {
        glColor3f(0.0f, 1.0f, 0.0f);             // Set The Color To Green
        glVertex3f(400, 200, -100);         // Top Right Of The Quad (Top)
        glVertex3f(300, 200, -100);         // Top Left Of The Quad (Top)
        glVertex3f(300, 200, -50);         // Bottom Left Of The Quad (Top)
        glVertex3f(400, 200, -50);         // Bottom Right Of The Quad (Top)

        glColor3f(1.0f, 0.5f, 0.0f);             // Set The Color To Orange
        glVertex3f(400, 100, -50);         // Top Right Of The Quad (Bottom)
        glVertex3f(300, 100, -50);         // Top Left Of The Quad (Bottom)
        glVertex3f(300, 100, -100);         // Bottom Left Of The Quad (Bottom)
        glVertex3f(400, 100, -100);         // Bottom Right Of The Quad (Bottom)

        glColor3f(1.0f, 0.0f, 0.0f);             // Set The Color To Red
        glVertex3f(400, 200, -50);         // Top Right Of The Quad (Front)
        glVertex3f(300, 200, -50);         // Top Left Of The Quad (Front)
        glVertex3f(300, 100, -50);         // Bottom Left Of The Quad (Front)
        glVertex3f(400, 100, -50);         // Bottom Right Of The Quad (Front)

        glColor3f(1.0f, 1.0f, 0.0f);             // Set The Color To Yellow
        glVertex3f(400, 100, -100);         // Bottom Left Of The Quad (Back)
        glVertex3f(300, 100, -100);         // Bottom Right Of The Quad (Back)
        glVertex3f(300, 200, -100);         // Top Right Of The Quad (Back)
        glVertex3f(400, 200, -100);         // Top Left Of The Quad (Back)

        glColor3f(0.0f, 0.0f, 1.0f);             // Set The Color To Blue
        glVertex3f(300, 200, -50);         // Top Right Of The Quad (Left)
        glVertex3f(300, 200, -100);         // Top Left Of The Quad (Left)
        glVertex3f(300, 100, -100);         // Bottom Left Of The Quad (Left)
        glVertex3f(300, 100, -50);         // Bottom Right Of The Quad (Left)

        glColor3f(1.0f, 0.0f, 1.0f);             // Set The Color To Violet
        glVertex3f(400, 200, -100);         // Top Right Of The Quad (Right)
        glVertex3f(400, 200, -50);         // Top Left Of The Quad (Right)
        glVertex3f(400, 100, -50);         // Bottom Left Of The Quad (Right)
        glVertex3f(400, 100, -100);         // Bottom Right Of The Quad (Right)
     }
     glEnd();                                       // Done Drawing The Quad

     rquad -= 0.15f;                                   
     return true;
     }
  }

}

Upvotes: 0

Views: 738

Answers (1)

amuliolis
amuliolis

Reputation: 23

TL;DR at bottom.

There are a few problems with this. When you call in glOrtho, you switched the third and fourth numbers, it should be what Dawnkeeper said, glOrtho(0, 640, 0, 480, 1, -1);. But even with that, there may be problems because the depths that you put into glOrtho are too small for the cube you're trying to render. Try using glOrtho(0, 640, 0, 480, 1000, -1000); so you can render the whole cube and not the sections that are within -1 and 1.

Another problem is that you're even using an orthographic view. If you're trying to achieve an isometric view then orthographic would be appropriate, but to see the cube as you would in real life, you should use a perspective view. I believe there are other ways to do this, but I do it by using the gluPerspective method, which comes in the lwjgl-util jar. The import is import org.lwjgl.util.glu.GLU; and with it you just replace the glOrtho method with GLU.gluPerspective(fovy, aspect, near, far); where fovy is the Field Of View, usually around 75, aspect, which is width/height, or in your case, 640/480, and near and far are kind of like the last two parameters in glOrtho, where near is usually 0.3f and far is the maximum distance you would be able to see anything, and could be any number you want. But with the perspective method you'll also have to move the cube away from the camera, or else it will be behind the camera. I recommend changing the parameters in glTranslatef() to (-400, 0, -400).

One more thing, you may want to enable depth testing by calling GL11.glEnable(GL11.GL_DEPTH_TEST); before you render your cube and change glClear(GL_COLOR_BUFFER_BIT); to glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER BIT);, or you can add glClear(GL_DEPTH_BUFFER_BIT); right under. What this does is just make sure you can't see the other side of the cube while it's rotated in a way that the sides rendered last are in front of the sides rendered first.

TL;DR, change the glOrtho method to glOrtho(0, 640, 0, 480, 1000, -1000); in order to be able to see the cube. The two paragraphs above are just suggestions.

I hope this answer solves your problem.

Upvotes: 1

Related Questions