I have spent about 2 Days trying to find out how to make 3d objects using lwjgl, and have found nothing. I can easily render 2d objects, and when I tried to render 3d objects, either the program crashed, or a black screen was opened, right now, I’m using Eclipse, and I’m running it in that environment (I don’t know if that’s the problem). If someone could point me towards a good tutorial, or just give me an explanation of what I’m doing wrong, that would be appreciated. Here’s my code:
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class Objects {
public static void main(String[] args) {
Objects objects = new Objects();
objects.start();
}
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
initGL();
while(!Display.isCloseRequested) {
Cube();
Display.update();
}
Display.destroy();
}
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
return;
}
public void cube() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
//front face
GL11.glColor3d(0.0, 1.0, 1.0);
GL11.glVertex3d(1.0, 1.0, -1.0);
GL11.glVertex3d(-1.0, 1.0, -1.0);
GL11.glVertex3d(-1.0, -1.0, -1.0);
GL11.glVertex3d(1.0, -1.0, -1.0);
//side face
GL11.glColor3d(1.0, 0.0, 0.0);
GL11.glVertex3d(1.0, 1.0, -1.0);
GL11.glVertex3d(1.0, 1.0, -3.0);
GL11.glVertex3d(1.0, -1.0, -3.0);
GL11.glVertex3d(1.0, -1.0, -1.0);
...(I kept going like this for the rest of the faces)
return;
}
***************EDIT***************
I have read both of your answers, and have made a java lwjgl code based on it, however when I tried to run it in the Eclipse environment, it gave me the error:
usage:
XPMFile <file>
java.lang.ArrayIndexOutOfBoundsException: 0
at org.lwjgl.util.XPMFile.main(XPMFile.java:260)
So, once again here’s my code:
package testing;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
public class CubeRender {
public static void main(String[] args) {
CubeRender cube = new CubeRender();
cube.start();
}
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
initGL();
while(!Display.isCloseRequested()) {
renderCube();
Display.update();
}
Display.destroy();
}
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45, 100, 1, 100);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public void renderCube() {
double x = 1;
double y = 1;
double z = -1;
//front face
GL11.glBegin(GL11.GL_QUADS);
GL11.glNormal3d(0, 0, 1);
GL11.glColor3d(0.0, 1.0, 1.0);
GL11.glVertex3d(x, y, z);
GL11.glVertex3d(x-2, y, z);
GL11.glVertex3d(x-2, y-2, z);
GL11.glVertex3d(x, y-2, z);
GL11.glEnd();
//right face
GL11.glBegin(GL11.GL_QUADS);
GL11.glNormal3d(-1, 0, 0);
GL11.glVertex3d(x, y, z);
GL11.glVertex3d(x, y, z-2);
GL11.glVertex3d(x, y-2, z-2);
GL11.glVertex3d(x, y-2, z);
GL11.glEnd();
return;
}
}
******EDIT 2******
if you have anything to contribute for the above answer please post answers here:
How to import lwjgl_util
Your using a 2d orthographic view see the following line “GL11.glOrtho(0, 800, 600, 0, 1, -1);” which clips everything to 1, -1 axis for the 3rd dimension.
What you really need to be looking at is a setting up a 3d view, likely using glFrustum or gluPerspective instead of glOrtho.
Although old now, you should have a quick read of the Nehe Tutorials at http://nehe.gamedev.net/ with some LWJGL ports of the first few found at http://ninjacave.com/nehetutorials
Do keep in mind that the Nehe tutorials use classic OpenGL is which pretty much depreciated for modern opengl, however it doesn’t hurt to know how classic opengl works. Its better you google for some newer OpenGL tutorials that use techniques such as shaders and VBO’s, I’d recommend that you get some books like the OpenGL Red Book, Orange Book or OpenGL Super Bible. Alternatively have a look at the links and resources page on the LWJGL Wiki, it should point you to a ton of opengl resources and tutorials.