gpt4 book ai didi

opengl-3 - glDrawArrays GL_LINES 未正确绘制线条

转载 作者:行者123 更新时间:2023-12-04 23:58:18 31 4
gpt4 key购买 nike

我试图用下面的代码画一条线,它的工作原理:

// Include standard headers
#include <stdio.h>
#include <stdlib.h>

// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <GL/glfw.h>

// Include GLM
#include <glm/glm.hpp>
using namespace glm;

// shaders
#include "shader.hpp"

int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}

glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// Open a window and create its OpenGL context
if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}

glewExperimental=GL_TRUE;

// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}

glfwSetWindowTitle( "Tutorial 02" );

// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );

// Dark blue background
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader",
"SimpleFragmentShader.fragmentshader" );

static const GLfloat g_vertex_buffer_data[] = {
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f
};

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

do{

// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );

// Use our shader
glUseProgram(programID);

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

// Draw the line !
glDrawArrays(GL_LINES, 0, 2); // 2 indices for the 2 end points of 1 line


glDisableVertexAttribArray(0);

// Swap buffers
glfwSwapBuffers();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );

// Close OpenGL window and terminate GLFW
glfwTerminate();

// Cleanup VBO
glDeleteBuffers(1, &vertexbuffer);
glDeleteVertexArrays(1, &VertexArrayID);

return 0;
}

我看到一条从屏幕中间到顶部中间的线。但是,当我将 g_vertex_buffer_data[] 中的顶点顺序更改为:
static const GLfloat g_vertex_buffer_data[] = {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

我只看到一个没有线条的蓝屏! opengl 文档中没有任何地方提到 GL_LINES 的特定顶点顺序,所以这也应该有效。

任何人都知道可能有什么问题?

最佳答案

你定义你的顶点是这样的:

glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

即每个顶点 2 个浮点数,没有步幅。所以如果你只渲染两个顶点,它只会读取 4 个浮点数。当您交换顶点时,前 4 个浮点数都为零,并且您得到两个相同的位置,因此没有线。

关于opengl-3 - glDrawArrays GL_LINES 未正确绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14011588/

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