PDA

View Full Version : glDrawArrays(GL_LINES)



jonpb1
12-12-2012, 04:18 PM
Could someone please either confirm that the following code either should work or tell me what I'm doing wrong? Assuming that the orthographic matrix calculation is correct and the shaders compile, link and bind.

Thanks in advance.

drawing code:

wglMakeCurrent(m_hDC, m_hRC);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

RECT rc;
::GetClientRect(m_hWnd, &rc);

Frustum frustum;
frustum.SetOrthographic(rc.left, rc.right, rc.top, rc.bottom, 0.f, 1.f);

m_scene.GetShaderManager().UseStockShader(AGL_STOC K_SHADER::AGL_SHADER_FLAT,
frustum.GetProjectionMatrix().data());

GLuint pBuffer;
glGenBuffers(1, &pBuffer);

M3DVector3f box[5] = {
{ 100, 200, 0.f },
{ 200, 200, 0.f },
{ 200, 100, 0.f },
{ 100, 100, 0.f },
{ 100, 200, 0.f }
};

glBindBuffer(GL_ARRAY_BUFFER, pBuffer);
glEnableVertexAttribArray(AGL_ATTRIBUTE_VERTEX);
glBufferData(GL_ARRAY_BUFFER, sizeof(M3DVector3f)*5, box, GL_STATIC_DRAW);
glVertexAttribPointer(AGL_ATTRIBUTE_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, 0);

glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glColor3f(0.f, 1.f, 1.f);
glLineWidth(2.f);

glDrawArrays(GL_LINES, 0, 5);

glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);

glDisableVertexAttribArray(AGL_ATTRIBUTE_VERTEX);
glDeleteBuffers(1, &pBuffer);

SwapBuffers(m_hDC);

Vertex Shader:

#version 330

uniform mat4 mvpMatrix;
attribute vec4 vVertex;

void main(void)
{
gl_Position = mvpMatrix * vVertex;
}

Fragment Shader:

#version 330

void main(void)
{
gl_FragColor = gl_Color;
}

tonyo_au
12-12-2012, 08:45 PM
who loads this

uniform mat4 mvpMatrix

GL_LINES requires an even number of vertices as each pair is a line segment

dukey
12-13-2012, 02:21 AM
maybe you want
GL_LINE_STRIP

jonpb1
12-17-2012, 04:22 PM
Ok, thanks guys, there were a couple problems, I needed GL_LINE_STRIP and also both gl_FragColor and gl_Color are deprecated in version 330.
Unfortunately it still doesn't quite work but I have managed to boil it down to something I fundamentally do not understand about the new API.

If I wrap, what I call the VBO loading code, inside a VAO it works, but if I call the VBO loading code directly in the drawing code it does not. If it is not possible to draw without using a VAO, are there methods to dynamically change the buffer data in the drawing code? I have listed below some sample code. In the drawing there are commented lines which can be toggled use or not use the VAO.

VBO Loading:
GLuint pBuffer;
glGenBuffers(1, &pBuffer);

float box[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8
};

glBindBuffer(GL_ARRAY_BUFFER, pBuffer);
glEnableVertexAttribArray(AGL_ATTRIBUTE_VERTEX);
glBufferData(GL_ARRAY_BUFFER, sizeof(box), box, GL_STATIC_DRAW);
glVertexAttribPointer(AGL_ATTRIBUTE_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);

General Loading (only run this code if using the VAO):
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);

LoadVBO();

glBindVertexArray(0);

Drawing:
glBindVertexArray(m_vao);
//LoadVBO();

glLineWidth(3.f);

glDrawArrays(GL_LINE_LOOP, 0, 3);

glBindVertexArray(0);
//glDisableVertexAttribArray(AGL_ATTRIBUTE_VERTEX);

tonyo_au
12-17-2012, 07:00 PM
glBufferSubdata still works. VAO's are basically a short cut to load all the states and bind the buffers in a more efficient method. They don't stop you from binding the buffer and updating the vertex data.