I am trying to make it so that the cubes will render properly, but the tops and bottoms of the cubes are refusing to work right.
code here:
package com.blazingkin.threeDee.thisguy;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import java.util.Random;
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;
public class threeDMain {
public static void main(String args[]){
new threeDMain().start();
}
public void walkForward(float distance)
{
x -= distance * (float)Math.sin(Math.toRadians(pitch));
z += distance * (float)Math.cos(Math.toRadians(pitch));
}
public void walkBackwards(float distance)
{
x += distance * (float)Math.sin(Math.toRadians(pitch));
z -= distance * (float)Math.cos(Math.toRadians(pitch));
}
public void strafeLeft(float distance)
{
x -= distance * (float)Math.sin(Math.toRadians(pitch-90));
z += distance * (float)Math.cos(Math.toRadians(pitch-90));
}
//strafes the camera right relitive to its current rotation (yaw)
public void strafeRight(float distance)
{
x -= distance * (float)Math.sin(Math.toRadians(pitch+90));
z += distance * (float)Math.cos(Math.toRadians(pitch+90));
}
public void lookThrough()
{
//roatate the pitch around the X axis
GL11.glRotatef(yaw, 1.0f, 0.0f, 0.0f);
//roatate the yaw around the Y axis
GL11.glRotatef(pitch, 0.0f, 1.0f, 0.0f);
//translate to the position vector's location
GL11.glTranslatef(x, -y, z);
}
int screenX = 800;
int screenY = 600;
int displayType = 0;
long timePassed = 0L;
long lastTime;
float x, y, z = 0;
float pitch, yaw= 0;
boolean mouseLocked = true;
public void start(){
try {
DisplayMode d = new DisplayMode(screenX, screenY);
Display.setDisplayMode(d);
Display.setVSyncEnabled(true);
if (displayType == 1){
System.setProperty("org.lwjgl.opengl.Window.undecorated","true");
Display.setFullscreen(true);
}else{
System.setProperty("org.lwjgl.opengl.Window.undecorated","false");
}
if (displayType == 2){
Display.setFullscreen(true);
}
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
lastTime = System.currentTimeMillis();
// init OpenGL here
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
gluPerspective((float)90,(float)screenX/(float)screenY,0.001f,100);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
Point[] points = new Point[100];
Random r = new Random();
for (int i = 0; i < points.length; i++){
points[i] = new Point((r.nextFloat() - 0.5F) * 100,(r.nextFloat() - 0.5F) * 100, r.nextInt(200) - 200);
}
float speed = 0.2f;
while (!Display.isCloseRequested()) {
glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
if (Keyboard.isKeyDown(Keyboard.KEY_D)){
strafeRight(speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)){
strafeLeft(speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_W)){
walkForward(speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)){
walkBackwards(speed);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
y+=speed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
y-=speed;
}
while (Keyboard.next()){
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
mouseLocked =! mouseLocked;
}
}
if (Display.isActive()){
if (mouseLocked){
Mouse.setCursorPosition(screenX/2, screenY/2);
yaw -= Mouse.getDY();
pitch += Mouse.getDX();
}
}
yaw = yaw<-80?-80:yaw;
yaw = yaw>80?80:yaw;
pitch = pitch>360?pitch%360:pitch;
pitch = pitch<-360?(pitch%360)*-1:pitch;
GL11.glLoadIdentity();
lookThrough();
System.out.println(x+", "+y+", "+z);
for (Point p: points){
GL11.glColor3f(1, 0, 1);
glBegin(GL11.GL_POLYGON);
//top face
GL11.glVertex3f(p.x, p.y+2, p.z);
GL11.glVertex3f(p.x, p.y+2, p.z+2);
GL11.glVertex3f(p.x+2, p.y+2, p.z+2);
GL11.glVertex3f(p.x+2, p.y+2, p.z);
glEnd();
GL11.glColor3f(1,1,1);
glBegin(GL11.GL_POLYGON);
//bottom face
GL11.glVertex3f(p.x, p.y, p.z);
GL11.glVertex3f(p.x, p.y, p.z+2);
GL11.glVertex3f(p.x+2, p.y, p.z+2);
GL11.glVertex3f(p.x+2, p.y, p.z);
glEnd();
GL11.glColor3f(1, 0, 0);
glBegin(GL11.GL_POLYGON);
//back
GL11.glVertex3f(p.x, p.y, p.z);
GL11.glVertex3f(p.x+2, p.y, p.z);
GL11.glVertex3f(p.x+2, p.y+2, p.z);
GL11.glVertex3f(p.x, p.y+2, p.z);
glEnd();
GL11.glColor3f(0, 0, 1);
glBegin(GL11.GL_POLYGON);
//side 1 face (left)
GL11.glVertex3f(p.x, p.y, p.z);
GL11.glVertex3f(p.x, p.y, p.z+2);
GL11.glVertex3f(p.x, p.y+2, p.z+2);
GL11.glVertex3f(p.x, p.y+2, p.z);
glEnd();
GL11.glColor3f(1, 1, 0);
glBegin(GL11.GL_POLYGON);
//side 2 face (right)
GL11.glVertex3f(p.x+2, p.y, p.z);
GL11.glVertex3f(p.x+2, p.y, p.z+2);
GL11.glVertex3f(p.x+2, p.y+2, p.z+2);
GL11.glVertex3f(p.x+2, p.y+2, p.z);
glEnd();
GL11.glColor3f(0, 1, 1);
glBegin(GL11.GL_POLYGON);
//front
GL11.glVertex3f(p.x, p.y, p.z+2);
GL11.glVertex3f(p.x+2, p.y, p.z+2);
GL11.glVertex3f(p.x+2, p.y+2, p.z+2);
GL11.glVertex3f(p.x, p.y+2, p.z+2);
glEnd();
}
Display.update();
}
Display.destroy();
}
class Point{
float x,y,z;
public Point(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
}
}
Basically, it is rendering the bottom of the cube where the top should be, but only when you are underneath it and when you are inside it it all renders correctly
It could be a problem of depth testing. You don’t enable a depth double-check anywhere! Try this:
That worked for me. Without that, things looked REALLY odd, let me know if this helps!