3
我在點數組中指定了一組頂點以繪製一個三角形,該三角形假定從窗口的頂部中間開始 - (+ 0.0f,+ 1.0f,+ 0.0f ) - 通過左下角 - (-1.0f,-1.0f,+ 0.0f) - 最後結束於窗口的右下角 - + 1.0f,-1.0f,+ 0.0f。 但是,這個三角形沒有如上面那樣呈現,因爲它看起來像從窗口的頂部中間開始,然後到達左上角,最後在窗口中間右邊結束。 我收錄了兩張截圖,以幫助我下面的問題描述。OpenGL:指定三角形的意外頂點位置
Expected triangle hinted in red
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GLFW\glfw3.h>
int main() {
// GLFW
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
return EXIT_FAILURE;
}
// Create window
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Test", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
// GLEW
glewExperimental = GL_TRUE;
glewInit();
// Get version info
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
printf("Renderer: %s\n", renderer);
printf("Supported OpenGL version: %s\n", version);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
/* Put code here */
// Vertices to be rendered
float points[] = {
+0.0f, +1.0f, +0.0f
-1.0f, -1.0f, +0.0f
+1.0f, -1.0f, +0.0f
};
// Vertex buffer object
GLuint bufferId = 0;
glGenBuffers(1, &bufferId);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
// Vertex attribute object
GLuint attributeId = 0;
glGenVertexArrays(1, &attributeId);
glBindVertexArray(attributeId);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Shaders
const char* vertexShaderSource =
"#version 400\n"
"in vec3 position;"
"void main() {"
" gl_Position = vec4(position.x, position.y, position.z, 1.0);"
"}";
const char* fragmentShaderSource =
"#version 400\n"
"out vec4 outColor;"
"void main() {"
" outColor = vec4 (0.0, 0.0, 1.0, 1.0);"
"}";
// Compile shaders
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Link shaders
GLuint shaderPorgram = glCreateProgram();
glAttachShader(shaderPorgram, fragmentShader);
glAttachShader(shaderPorgram, vertexShader);
glLinkProgram(shaderPorgram);
// Render loop
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderPorgram);
glBindVertexArray(attributeId);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return EXIT_SUCCESS;
}
哇,一個完整但短小的代碼?示例圖片的問題的簡要說明?不妨給我們免費的蛋糕! –