你好,三角形
现在我们想绘制一个五×N彩的三角形。
在本节中,我们将首次也是最后一次使用固定管线来绘制。如果想了解更多固定管线的使用示例,请参考Minecraft2D.
下一节,我们将会介绍着色器——一个自定义的管线
首先,我们定位到这个位置:
public class HelloWorld {
// ..
private void loop() {
// ..
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
// ..
}
// ..
}
然后,把glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
改为glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
,这样就不会亮瞎眼了。
接着在glClear
后添加下面几条语句:
public class HelloWorld {
// ..
private void loop() {
// ..
while (!glfwWindowShouldClose(window)) {
// ..
glBegin(GL_TRIANGLES); // 开始绘制三角形
// 注意:需要逆时针绘制
glColor3f(1, 0, 0); // 红
glVertex2f(0, 1); // 中上
glColor3f(0, 1, 0); // 绿
glVertex2f(-1, -1); // 左下
glColor3f(0, 0, 1); // 蓝
glVertex2f(1, -1); // 右下
glEnd(); // 绘制结束
// ..
}
// ..
}
// ..
}
启动后,会得到一个鲜艳的三角形。
glColor4f()
用于设置颜色, 以OpenGL的RGBA模式
glVertex3f()
设置顶点位置, 以x, y, z的格式。
也许你会疑惑:
这不是2d游戏吗?z轴有什么用
答案简单至极:将背景的z轴变深,以使其他物体覆盖于背景上。
记得开深度测试
失败了?还不快抛issue!