使用 LWJGL 3 显示 3D 三角形
Displaying 3D triangle using LWJGL 3
我已经成功地使用 LWJGL 3 在 OpenGL 4.2 window 中显示二维三角形。这是我使用的代码:
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryUtil;
public class TestClass
{
private static long window;
private static int WIDTH = 1280;
private static int HEIGHT = 720;
public static void main (String[] args)
{
if (glfwInit() == GL_FALSE)
{
System.out.println ("GLFW initialization failed.");
System.exit (1);
}
glfwWindowHint (GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow (WIDTH, HEIGHT, "GLFW Window", MemoryUtil.NULL, MemoryUtil.NULL);
glfwMakeContextCurrent (window);
glfwSwapInterval (1);
GL.createCapabilities();
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (0, 12, 12, 0, 1, -1);
glClearColor (0, 0.7f, 1, 0);
glMatrixMode (GL_MODELVIEW);
glfwShowWindow (window);
while (glfwWindowShouldClose (window) == GL_FALSE)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin (GL_TRIANGLES);
glColor3f (1, 0, 0.7f);
glVertex3f (6, 4, 0); // Vertex one
glColor3f (1, 0, 0.7f);
glVertex3f (4, 8, 0); // Vertex two
glColor3f (1, 0, 0.7f);
glVertex3f (8, 8, 0); // Vertex three
glEnd();
glfwSwapBuffers (window);
glfwPollEvents();
}
glfwDestroyWindow (window);
glfwTerminate();
}
}
如果我将我的三个顶点中的任何一个的 Z 值设置为大于 1
或小于 -1
的值,三角形将在该顶点周围部分消失。当我将 Z 值设置为 1
和 -1
之间的某个值时,我看不出这与让值等于 0
之间没有区别。我有点卡在这里。有人可以解释一下如何让这个三角形存在于不完全平行于视角的平面上吗?
干杯,
星云
发生这种情况是因为您没有使用透视图,而是使用正交视图。当您调用 glOrtho()
时,您要求将用于投影的矩阵设置为正交矩阵,该矩阵不考虑深度,也不进行透视计算。这就是您看不到三角形变化的原因。至于当值超过(-1, 1)范围时顶点消失,那是因为你将glOrtho()
调用的最后两个参数分别设置为1和-1。这告诉 openGL 丢弃超出此范围的顶点(因为规范化设备坐标和矩阵数学协同工作的方式,如果您继续使用 openGL 并使用更高版本,您可能会了解这一点)。
现在,在 LWJGL 3 中,glu(实用程序包)已被删除。这使得为固定管道初始化透视矩阵变得有点困难。 here 是一个很好的 post 如何做到这一点,但由于它是为 c++ 编写的,我将为您提供 java 等价物。
// Replaces gluPerspective. Sets the frustum to perspective mode.
// fov - Field of vision in degrees in the y direction
// aspect - Aspect ratio of the viewport
// zNear - The near clipping distance
// zFar - The far clipping distance
private static void perspectiveGL(float fov, float aspect, float zNear, float zFar) {
float fH = (float) Math.tan(fov / 360 * Math.PI) * zNear;
float fW = fH * aspect;
glFrustum( -fW, fW, -fH, fH, zNear, zFar );
}
glFrustum()
调用是此函数的重要部分,它创建了一个透视矩阵,但是,它的参数对用户来说不是很友好。
希望对您有所帮助!
我已经成功地使用 LWJGL 3 在 OpenGL 4.2 window 中显示二维三角形。这是我使用的代码:
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryUtil;
public class TestClass
{
private static long window;
private static int WIDTH = 1280;
private static int HEIGHT = 720;
public static void main (String[] args)
{
if (glfwInit() == GL_FALSE)
{
System.out.println ("GLFW initialization failed.");
System.exit (1);
}
glfwWindowHint (GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow (WIDTH, HEIGHT, "GLFW Window", MemoryUtil.NULL, MemoryUtil.NULL);
glfwMakeContextCurrent (window);
glfwSwapInterval (1);
GL.createCapabilities();
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (0, 12, 12, 0, 1, -1);
glClearColor (0, 0.7f, 1, 0);
glMatrixMode (GL_MODELVIEW);
glfwShowWindow (window);
while (glfwWindowShouldClose (window) == GL_FALSE)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin (GL_TRIANGLES);
glColor3f (1, 0, 0.7f);
glVertex3f (6, 4, 0); // Vertex one
glColor3f (1, 0, 0.7f);
glVertex3f (4, 8, 0); // Vertex two
glColor3f (1, 0, 0.7f);
glVertex3f (8, 8, 0); // Vertex three
glEnd();
glfwSwapBuffers (window);
glfwPollEvents();
}
glfwDestroyWindow (window);
glfwTerminate();
}
}
如果我将我的三个顶点中的任何一个的 Z 值设置为大于 1
或小于 -1
的值,三角形将在该顶点周围部分消失。当我将 Z 值设置为 1
和 -1
之间的某个值时,我看不出这与让值等于 0
之间没有区别。我有点卡在这里。有人可以解释一下如何让这个三角形存在于不完全平行于视角的平面上吗?
干杯, 星云
发生这种情况是因为您没有使用透视图,而是使用正交视图。当您调用 glOrtho()
时,您要求将用于投影的矩阵设置为正交矩阵,该矩阵不考虑深度,也不进行透视计算。这就是您看不到三角形变化的原因。至于当值超过(-1, 1)范围时顶点消失,那是因为你将glOrtho()
调用的最后两个参数分别设置为1和-1。这告诉 openGL 丢弃超出此范围的顶点(因为规范化设备坐标和矩阵数学协同工作的方式,如果您继续使用 openGL 并使用更高版本,您可能会了解这一点)。
现在,在 LWJGL 3 中,glu(实用程序包)已被删除。这使得为固定管道初始化透视矩阵变得有点困难。 here 是一个很好的 post 如何做到这一点,但由于它是为 c++ 编写的,我将为您提供 java 等价物。
// Replaces gluPerspective. Sets the frustum to perspective mode.
// fov - Field of vision in degrees in the y direction
// aspect - Aspect ratio of the viewport
// zNear - The near clipping distance
// zFar - The far clipping distance
private static void perspectiveGL(float fov, float aspect, float zNear, float zFar) {
float fH = (float) Math.tan(fov / 360 * Math.PI) * zNear;
float fW = fH * aspect;
glFrustum( -fW, fW, -fH, fH, zNear, zFar );
}
glFrustum()
调用是此函数的重要部分,它创建了一个透视矩阵,但是,它的参数对用户来说不是很友好。
希望对您有所帮助!