PDA

View Full Version : Issues drawing a line in openGL



redalwaysmafia
09-06-2015, 02:23 PM
I already have a program that can draw textured objects. I want to draw debug lines, so I tried to copy the same sort of drawing process I use for sprites to draw a line. I made a new fragment and vertex shader because lines aren't going to be textured and having a different debug shader could be useful.

My system continues to work if I try to draw a line, but the line doesn't draw. I tried to write code similar to working code for my sprites, but clearly I've missed something or made a mistake.

I know the following implementation is not efficient/elegant, but I'm just trying to get it to work and then I'll improve it.


Vertex Shader:


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


Drawing Code:


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(1.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(GLfloat), // 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);
}

GClements
09-06-2015, 02:39 PM
glUniform4f(colorLocation, 1.0f, color.x, color.y, color.z);


If color.z is zero, the alpha component will be zero; if blending is enabled, this will typically result in the line being invisible. It would probably make more sense to use:


glUniform4f(colorLocation, color.x, color.y, color.z, 1.0f);

redalwaysmafia
09-06-2015, 02:52 PM
If color.z is zero, the alpha component will be zero; if blending is enabled, this will typically result in the line being invisible. It would probably make more sense to use:


glUniform4f(colorLocation, color.x, color.y, color.z, 1.0f);


Someone pointed this out on stack overflow. I tried making the shader just draw a particular color of line and ignore the uniform, but it didn't change anything.

GClements
09-06-2015, 04:20 PM
In that case, I'd assume that the problem is in a part of the code which wasn't posted (as is usually the case).

Try to reduce your code to a minimal test case which exhibits the problem, and if that doesn't result in you solving the issue as a side effect, post the complete program.