在OpenGL中绘制一条线的问题

时间:2015-09-06 00:24:02

标签: c++ opengl

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

如果我尝试绘制一条线,我的系统会继续工作,但线条不会绘制。我试着为我的精灵编写类似于工作代码的代码,但很明显我错过了一些东西或犯了错误。

顶点着色器:

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

Fragment Shader:

#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 个答案:

答案 0 :(得分:0)

核心配置文件中不允许使用大于1的线宽。如果将大小设置为1

,是否会渲染线条