在错误位置呈现的顶点 (OpenGl)
Verticies rendered at the wrong position (OpenGl)
当我尝试渲染一个 PERFECT 立方体时,一个顶点渲染在立方体的中间 - (.5, .5, -.5) 而不是 (1, 1, 1) - 一个使一个单位过高 - (1, 1, 1) 而不是 (1, 0, 0)。我真的不知道这是怎么发生的。我首先检查它是否与 obj 加载有关并单独定义数组,它没有。这是 program. This is from another perspective for better understanding of the problem.
的输出
首先我初始化 glfw
,创建一个 window,创建一个上下文并设置各种变量:
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW, press enter to close the application..." << std::endl;
std::cin.get();
return false;
}
GLFWWindow *window= glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "Failed to creat GLFW window, press enter to close the application..." << std::endl;
std::cin.get();
return false;
}
glfwMakeContextCurrent(m_GLFWWindow);
然后我初始化一些启用一些 gl 选项并设置各种其他变量:
glClearColor(0, 0, 0, 0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthRange(0, 1);
glEnable(GL_FRAMEBUFFER_SRGB);
完成后我声明一个顶点数组和一个索引数组并将它们传递给 OpenGL:(顶点只是一个包含三个浮点数的结构)
Vertex verticies[] = { Vertex(Vector3( 1, -1, -1)),
Vertex(Vector3( 1, -1, 1)),
Vertex(Vector3( -1, -1, 1)),
Vertex(Vector3( -1, -1, -1)),
Vertex(Vector3( 1, 1, -1)),
Vertex(Vector3( 1, 1, 1)),
Vertex(Vector3( -1, 1, 1)),
Vertex(Vector3( -1, 1, -1)) };
unsigned int indicies[] = { 2, 4, 1,
8, 6, 5,
5, 2, 1,
6, 3, 2,
3, 8, 4,
1, 8, 5,
2, 3, 4,
8, 7, 6,
5, 6, 2,
6, 7, 3,
3, 7, 8,
1, 4, 8};
GLu vbo;
Glu ibo;
glGenBuffers(1, &vbi);
glGenBuffers(1, &ino);
glBindBuffer(GL_ARRAY_BUFFER, m_Vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies[0]) * 8, &verticies, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies[0]) * 36, &indicies, GL_STATIC_DRAW);
然后我创建一个程序,编译着色器并将它们 link 到程序中:
GLuint program = glCreateProgram();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
GLchar vertexCode = "vertexShader code";
GLchar fragmentCode = "fragmenShader code";
glShaderSource(vertexShader , 1, &vertexCode, NULL);
glCompileShader(vertexShader );
glShaderSource(fragmentShader, 1, &fragmentCode, NULL);
glCompileShader(fragmentShader);
glAttachShader(program , vertexShader );
glAttachShader(program , fragmentShader);
glLinkProgram(m_Program);
glValidateProgram(m_Program);
现在我们进入游戏循环:
while(1)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
}
你的索引差了一个,它们在 OpenGL 中是从 0 开始的,所以只有 0 到 7 的值对你的数组有效,使用 8 将访问一些具有未定义内容的越界内存。
您的索引数组看起来不正确。它应该在 [0...7]
范围内索引,而不是 [1...8]
.
当我尝试渲染一个 PERFECT 立方体时,一个顶点渲染在立方体的中间 - (.5, .5, -.5) 而不是 (1, 1, 1) - 一个使一个单位过高 - (1, 1, 1) 而不是 (1, 0, 0)。我真的不知道这是怎么发生的。我首先检查它是否与 obj 加载有关并单独定义数组,它没有。这是 program. This is from another perspective for better understanding of the problem.
的输出首先我初始化 glfw
,创建一个 window,创建一个上下文并设置各种变量:
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW, press enter to close the application..." << std::endl;
std::cin.get();
return false;
}
GLFWWindow *window= glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "Failed to creat GLFW window, press enter to close the application..." << std::endl;
std::cin.get();
return false;
}
glfwMakeContextCurrent(m_GLFWWindow);
然后我初始化一些启用一些 gl 选项并设置各种其他变量:
glClearColor(0, 0, 0, 0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthRange(0, 1);
glEnable(GL_FRAMEBUFFER_SRGB);
完成后我声明一个顶点数组和一个索引数组并将它们传递给 OpenGL:(顶点只是一个包含三个浮点数的结构)
Vertex verticies[] = { Vertex(Vector3( 1, -1, -1)),
Vertex(Vector3( 1, -1, 1)),
Vertex(Vector3( -1, -1, 1)),
Vertex(Vector3( -1, -1, -1)),
Vertex(Vector3( 1, 1, -1)),
Vertex(Vector3( 1, 1, 1)),
Vertex(Vector3( -1, 1, 1)),
Vertex(Vector3( -1, 1, -1)) };
unsigned int indicies[] = { 2, 4, 1,
8, 6, 5,
5, 2, 1,
6, 3, 2,
3, 8, 4,
1, 8, 5,
2, 3, 4,
8, 7, 6,
5, 6, 2,
6, 7, 3,
3, 7, 8,
1, 4, 8};
GLu vbo;
Glu ibo;
glGenBuffers(1, &vbi);
glGenBuffers(1, &ino);
glBindBuffer(GL_ARRAY_BUFFER, m_Vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies[0]) * 8, &verticies, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies[0]) * 36, &indicies, GL_STATIC_DRAW);
然后我创建一个程序,编译着色器并将它们 link 到程序中:
GLuint program = glCreateProgram();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
GLchar vertexCode = "vertexShader code";
GLchar fragmentCode = "fragmenShader code";
glShaderSource(vertexShader , 1, &vertexCode, NULL);
glCompileShader(vertexShader );
glShaderSource(fragmentShader, 1, &fragmentCode, NULL);
glCompileShader(fragmentShader);
glAttachShader(program , vertexShader );
glAttachShader(program , fragmentShader);
glLinkProgram(m_Program);
glValidateProgram(m_Program);
现在我们进入游戏循环:
while(1)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
}
你的索引差了一个,它们在 OpenGL 中是从 0 开始的,所以只有 0 到 7 的值对你的数组有效,使用 8 将访问一些具有未定义内容的越界内存。
您的索引数组看起来不正确。它应该在 [0...7]
范围内索引,而不是 [1...8]
.