在OpenGL中绘制线的问题

Issues drawing a line in OpenGL

本文关键字:问题 绘制 OpenGL      更新时间:2023-10-16

我已经有了一个可以绘制纹理对象的程序。我想绘制调试线,所以我尝试复制我用于精灵绘制线的相同绘制过程。我做了一个新的片段和顶点着色器,因为线条不会被纹理化,有一个不同的调试着色器可能是有用的。

如果我试图画一条线,但这条线没有画出来,我的系统继续工作。我试图为我的精灵编写类似于工作代码的代码,但显然我遗漏了一些东西或犯了一个错误。

顶点着色器:

#version 330 core
layout (location = 0) in vec2 position;
uniform mat4 uniformView;
uniform mat4 uniformProjection;
void main()
{
    gl_Position = uniformProjection * uniformView * vec4(position, 0.0f, 1.0f);
}

片段着色器:

#version 330 core
out vec4 color;
uniform vec4 uniformColor;
void main()
{
    color = uniformColor;
}

绘图代码:

void debugDrawLine(glm::vec3 startPoint, glm::vec3 endPoint, glm::vec3 color, Shader debugShader)
{
  GLint transformLocation, colorLocation;
  GLfloat lineCoordinates[] = { startPoint.x, startPoint.y,
                               endPoint.x, endPoint.y};
  GLuint vertexArray, vertexBuffer;
  glLineWidth(5.0f);
  glGenVertexArrays(1, &vertexArray);
  glGenBuffers(1, &vertexBuffer);
  glBindVertexArray(vertexArray);
  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  debugShader.Use();
  //Copies the Vertex data into the buffer
  glBufferData(GL_ARRAY_BUFFER, sizeof(lineCoordinates), lineCoordinates, GL_STATIC_DRAW);
  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?
    2*sizeof(GL_FLOAT), // stride
    (GLvoid*)0            // array buffer offset
    );

  //Sends the sprite's color information in the the shader 
  colorLocation = glGetUniformLocation(debugShader.Program, "uniformColor");
  glUniform4f(colorLocation, 1.0f, color.x, color.y, color.z);
  //Activates Vertex Position Information
  glEnableVertexAttribArray(0);
  // Draw the line
  glDrawArrays(GL_LINES, 0, 2);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);
}

核心配置文件不允许行宽大于1。如果将大小设置为1

,该行会呈现吗?