为什么我的 LEGACY OPENGL 颜色是倒置的?
Why are my LEGACY OPENGL colors inverted?
几个月前我写了一个简单的代码来显示 3 个多边形。一切正常。然后颜色突然开始变化...
我在 VISUAL STUDIO 2019 年。
我试过:
- 正在修复 VS 可再发行组件。
- 制作新项目。
有线索吗?
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
//idk what is going on
glBegin(GL_POLYGON);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glColor3f(1.0f, 0.5f, 0.5f);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.7f, 0.7f);
glVertex2f(-0.7f, 0.7f);
glVertex2f(-0.7f, -0.7f);
glVertex2f(0.7f, -0.7f);
glColor3f(1.0f, 1.0f, 1.0f);
glEnd();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
必须设置颜色属性 (glColor
) before the vertex coordinate is specified by glVertex2f
:
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.5f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.7f, 0.7f);
glVertex2f(-0.7f, 0.7f);
glVertex2f(-0.7f, -0.7f);
glVertex2f(0.7f, -0.7f);
glEnd();
注意,glColor
设置当前颜色,当调用glVertex
时,当前颜色、法线和纹理坐标与顶点相关联。
几个月前我写了一个简单的代码来显示 3 个多边形。一切正常。然后颜色突然开始变化...
我在 VISUAL STUDIO 2019 年。
我试过:
- 正在修复 VS 可再发行组件。
- 制作新项目。
有线索吗?
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
//idk what is going on
glBegin(GL_POLYGON);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glColor3f(1.0f, 0.5f, 0.5f);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.7f, 0.7f);
glVertex2f(-0.7f, 0.7f);
glVertex2f(-0.7f, -0.7f);
glVertex2f(0.7f, -0.7f);
glColor3f(1.0f, 1.0f, 1.0f);
glEnd();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
必须设置颜色属性 (glColor
) before the vertex coordinate is specified by glVertex2f
:
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.5f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.7f, 0.7f);
glVertex2f(-0.7f, 0.7f);
glVertex2f(-0.7f, -0.7f);
glVertex2f(0.7f, -0.7f);
glEnd();
注意,glColor
设置当前颜色,当调用glVertex
时,当前颜色、法线和纹理坐标与顶点相关联。