gpt4 book ai didi

c++ - OpenGL:指定三角形的意外顶点位置

转载 作者:太空狗 更新时间:2023-10-29 20:22:28 27 4
gpt4 key购买 nike

我在点数组中指定了一组顶点来绘制一个三角形,该三角形应该从窗口的顶部中间开始 - (+0.0f, +1.0f, +0.0f) - 穿过左下角 - (-1.0f, -1.0f, +0.0f) - 最后在窗口的右下角结束 - +1.0f, -1.0f, +0.0f。然而,三角形并没有按照上面的预期呈现,因为它看起来像是从窗口的顶部中间开始,然后继续到左上角,最后在窗口的中间结束。我在下面附上了两个屏幕截图以帮助描述我的问题。

Unexpected rendering

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;
}

最佳答案

缺少逗号。

float points[] = {
+0.0f, +1.0f, +0.0f
-1.0f, -1.0f, +0.0f
+1.0f, -1.0f, +0.0f
};

应该是:

float points[] = {
+0.0f, +1.0f, +0.0f,
-1.0f, -1.0f, +0.0f,
+1.0f, -1.0f, +0.0f,
};

最后一个逗号不是必需的,但它是允许的,我更喜欢它作为一种风格。

关于c++ - OpenGL:指定三角形的意外顶点位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37800998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com