gpt4 book ai didi

java - OpenGL 中的立方体渲染怪癖

转载 作者:行者123 更新时间:2023-12-02 07:25:14 24 4
gpt4 key购买 nike

我正在尝试使立方体能够正确渲染,但立方体的顶部和底部拒绝正常工作。

代码在这里:

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;
}

}


}

基本上,它会渲染立方体的底部,也就是顶部应该在的位置,但只有当你在它下面以及当你在它里面时,它才会正确渲染

最佳答案

这可能是深度测试的问题。您无法在任何地方启用深度双重检查!试试这个:

GL11.glEnable(GL11.GL_DEPTH_TEST);

这对我有用。如果没有这个,事情看起来真的很奇怪,请告诉我这是否有帮助!

关于java - OpenGL 中的立方体渲染怪癖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com