Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: glDrawArrays(GL_LINES)

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    8

    glDrawArrays(GL_LINES)

    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:
    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_STOCK_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:
    Code :
    #version 330
     
    uniform mat4 mvpMatrix;
    attribute vec4 vVertex;
     
    void main(void)
    {
      gl_Position = mvpMatrix * vVertex;
    }

    Fragment Shader:
    Code :
    #version 330
     
    void main(void)
    {
      gl_FragColor = gl_Color;
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,101
    who loads this

    uniform mat4 mvpMatrix

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

  3. #3
    Junior Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    249
    maybe you want
    GL_LINE_STRIP

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    8
    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:
    Code :
    	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):
    Code :
    	glGenVertexArrays(1, &m_vao);
    	glBindVertexArray(m_vao);
     
    	LoadVBO();
     
    	glBindVertexArray(0);

    Drawing:
    Code :
    	glBindVertexArray(m_vao);
    	//LoadVBO();
     
    	glLineWidth(3.f);
     
    	glDrawArrays(GL_LINE_LOOP, 0, 3);
     
    	glBindVertexArray(0);
    	//glDisableVertexAttribArray(AGL_ATTRIBUTE_VERTEX);

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,101
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •