gpt4 book ai didi

java - OBJ 文件的 OpenGL 线框渲染

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:12 30 4
gpt4 key购买 nike

我正在编写一个程序,只需要读取 .obj 文件并以线框模式渲染它。我已经阅读了我想要渲染的 .obj 文件(正确 - 我相信)。但我遇到了一些问题...它应该是线框的,但相反...(如下图)

my output

这是代码:

    public void render(GL gl){
float xMiddle = (m.getXVertexMax() + m.getXVertexMin())/2;
float yMiddle = (m.getYVertexMax() + m.getYVertexMin())/2;
float zMiddle = (m.getZVertexMax() + m.getZVertexMin())/2;

gl.glScalef(1/(m.getXVertexMax() - m.getXVertexMin()), 1, 1);
gl.glScalef(1, 1/(m.getYVertexMax() - m.getYVertexMin()), 1);
gl.glScalef(1, 1, 1/(m.getZVertexMax() - m.getZVertexMin()));

gl.glBegin(GL.GL_TRIANGLES);
{
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINES);
for(int i = 0; i < m.faces.size(); i++ ){
Vector3f n1 = m.normals.get(m.faces.get(i).getNormalIndices()[0] - 1);
Vector3f v1 = m.vertices.get(m.faces.get(i).getVertexIndices()[0] - 1);
gl.glVertex3f(v1.x - xMiddle, v1.y - yMiddle, v1.z - zMiddle);
gl.glNormal3f(n1.x, n1.y, n1.z);


Vector3f n2 = m.normals.get(m.faces.get(i).getNormalIndices()[1] - 1);
Vector3f v2 = m.vertices.get(m.faces.get(i).getVertexIndices()[1] - 1);
gl.glVertex3f(v2.x - xMiddle, v2.y - yMiddle, v2.z - zMiddle);
gl.glNormal3f(n2.x, n2.y, n2.z);




Vector3f n3 = m.normals.get(m.faces.get(i).getNormalIndices()[2] - 1);
Vector3f v3 = m.vertices.get(m.faces.get(i).getVertexIndices()[2] - 1);
gl.glVertex3f(v3.x - xMiddle, v3.y - yMiddle, v3.z - zMiddle);
gl.glNormal3f(n3.x, n3.y, n3.z);

}
}
gl.glEnd();
}

注意:代码中的Vector3f,是我制作的一个数据结构。

我已经尝试了我能找到的所有方法,但它仍然不会将图像渲染为线框! :-/

有人可以帮忙吗?

最佳答案

gl.glBegin(GL.GL_TRIANGLES);
{
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINES);
...

很少 GL 命令在glBegin()/glEnd() 内有效。 block :

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

glEnable()glPolygonMode() 不在该列表中。

将它们移到 glBegin() block 之外。

<小时/>
    gl.glVertex3f(v1.x - xMiddle, v1.y - yMiddle, v1.z - zMiddle);
gl.glNormal3f(n1.x, n1.y, n1.z);

方向错误。你想要正常,然后顶点:

    gl.glNormal3f(n1.x, n1.y, n1.z);
gl.glVertex3f(v1.x - xMiddle, v1.y - yMiddle, v1.z - zMiddle);

glNormal() 仅设置当前法线,glVertex() 才是实际将其发送到管道中的内容。

关于java - OBJ 文件的 OpenGL 线框渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20221514/

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