我一直试图加载一个obj。将文件存入我的LWJGL应用程序。我只有裸露的骨头才能出现。我的对象。正在加载和显示但是它的扭曲/扭曲。我已经尝试过几种方式但仍然没有运气。这是我得到的图像。
Main.java
package Model;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
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.util.vector.Vector3f;
public class Main {
private Model m;
private Vector3f location, rotation;
public static void main(String[] args) {
new Main().run();
}
private void run() {
try {
Display.setDisplayMode(new DisplayMode(1200, 800));
Display.setTitle("3D Game");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
location = new Vector3f(0.0f, 0.0f, 0.0f);
rotation = new Vector3f(0.0f, 0.0f, 0.0f);
try {
m = Model.getModel("res/monkey.obj");
} catch (Exception e){
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30f, (float) (1200/800), 0.3f, 100);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
while(!Display.isCloseRequested()){
input();
render();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
private void input() {
boolean up = Keyboard.isKeyDown(Keyboard.KEY_W);
boolean down = Keyboard.isKeyDown(Keyboard.KEY_S);
boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);
boolean flyUp = Keyboard.isKeyDown(Keyboard.KEY_E);
boolean flyDown = Keyboard.isKeyDown(Keyboard.KEY_Q);
boolean speedUp = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
boolean slowDown = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
float walkspeed = 0.15f;
float mx = Mouse.getDX();
float my = Mouse.getDY();
mx *= 0.15f;
my *= 0.15f;
if (Mouse.isButtonDown(0)){
rotation.y += mx;
if (rotation.y > 360){
rotation.y -= 360;
}
rotation.y += my;
if (rotation.x > 85){
rotation.x = 85;
}
if (rotation.x < -85){
rotation.x = -85;
}
}
if(speedUp && !slowDown){
walkspeed = 0.25f;
}
if(slowDown && !speedUp){
walkspeed = 0.15f;
}
if(up && !down){
float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)));
float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
location.z += cz;
location.x -= cx;
}
if(down && !up){
float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)));
float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
location.z -= cz;
location.x += cx;
}
if(right && !left){
float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)) + 90);
float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
location.z += cz;
location.x -= cx;
}
if(left && !right){
float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)) + 90);
float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
location.z -= cz;
location.x += cx;
}
if(flyUp && !flyDown){
location.y -= walkspeed;
}
if(flyDown && !flyUp){
location.y += walkspeed;
}
}
private void render() {
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(rotation.x, 1f, 0f, 0f);
glRotatef(rotation.y, 0f, 1f, 0f);
glRotatef(rotation.z, 0f, 0f, 1f);
glTranslatef(location.x, location.y, location.z);
m.render();
glPopMatrix();
}
}
Model.java
package Model;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.util.vector.Vector3f;
import static org.lwjgl.opengl.GL11.*;
public class Model {
public List<Vector3f> verts;
public List<Vector3f> norms;
public List<Face> faces;
public static Model getModel(String s) throws IOException{
return new Model(s);
}
private Model(String path) throws IOException{
verts = new ArrayList<Vector3f>();
norms = new ArrayList<Vector3f>();
faces = new ArrayList<Face>();
new ModelLoader(this, path);
}
public void render(){
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_TRIANGLES);
for (Face f : faces){
Vector3f v1 = verts.get((int) f.verts.x -1);
Vector3f n1 = verts.get((int) f.norms.x -1);
Vector3f v2 = verts.get((int) f.verts.y -1);
Vector3f n2 = verts.get((int) f.norms.y -1);
Vector3f v3 = verts.get((int) f.verts.z -1);
Vector3f n3 = verts.get((int) f.norms.z -1);
glNormal3f(n1.x, n1.y, n1.z);
glVertex3f(v1.x, v1.y, v1.z);
glNormal3f(n2.x, n2.y, n2.z);
glVertex3f(v2.x, v2.y, v2.z);
glNormal3f(n3.x, n3.y, n3.z);
glVertex3f(v2.x, v3.y, v3.z);
}
glEnd();
}
}
ModelLoader.java
package Model;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.lwjgl.util.vector.Vector3f;
public class ModelLoader {
public ModelLoader(Model m, String path) throws IOException {
BufferedReader read = new BufferedReader(new FileReader(new File(path)));
String line;
while((line = read.readLine()) != null) {
if (line.startsWith("v ")){
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
Vector3f v = new Vector3f(x, y, z);
m.verts.add(v);
} else if (line.startsWith("vn ")) {
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
Vector3f v = new Vector3f(x, y, z);
m.norms.add(v);
} else if (line.startsWith("f ")) {
float x1 = Float.valueOf(line.split(" ")[1].split("/")[0]);
float y1 = Float.valueOf(line.split(" ")[2].split("/")[0]);
float z1 = Float.valueOf(line.split(" ")[3].split("/")[0]);
float x2 = Float.valueOf(line.split(" ")[1].split("/")[2]);
float y2 = Float.valueOf(line.split(" ")[2].split("/")[2]);
float z2 = Float.valueOf(line.split(" ")[3].split("/")[2]);
Face f = new Face(new Vector3f(x2, y2, z2), new Vector3f(x1, y1, z1));
m.faces.add(f);
}
}
read.close();
}
}
Face.java
package Model;
import org.lwjgl.util.vector.Vector3f;
public class Face {
public Vector3f norms, verts;
public Face(Vector3f n, Vector3f v){
this.norms = n;
this.verts = v;
}
}
答案 0 :(得分:0)
你其实非常接近。如果你看一下猴头的图像,我相信,它从大量的三角形中抽出来。渲染了一半的三角形。这使我相信某些面的索引的顺序存在问题。 其次我有另一个想法。由于这是从搅拌机中导出的,我建议您确保在导出选项中选择了三角测量导出猴头。这将确保创建三角形而不是四边形。可能您只从面线中提取前三个索引(&#34; f&#34;),这将导致这些三角形。
对于第一个可能的修复,我建议尝试禁用面部剔除以查看它是否已修复。如果这确实修复了模型,则问题在于面的索引顺序。 其次,我刚刚注意到在渲染的最后一个glVertex3f中,你为顶点x参考写了v2而不是v3。这很可能就是问题所在。 第三,如果这些不起作用,那么再次尝试从搅拌机中导出,确保对模型进行三角测量。