How to draw lines in Opengl ES 2.0?

Started by
5 comments, last by CPPNick 13 years, 10 months ago
How do I draw lines in OpenGL ES 2.0?
I have declared this: #include <GLES2/gl2.h>

but all of these functions seem to be undefined =(

glColor4f
glVertexPointer
glEnableClientState
glBegin
glEnd
glVertex3f


Using ES 1.1 is not an option. Can't I still draw lines in 2.0?

Advertisement
I would assume it is the same way you draw triangles. You probably need a shader, plus some of these commands:

glEnableVertexAttribArray()
glVertexAttribPointer()
glDrawArrays()/glDrawElements()
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
That's what I am using for triangles, but I couldn't figure out how I would use that for lines.

I want to draw the edges(defined separately) over top of the scene.
after drawing out my polygons, would it go something like this?

glDisableVertexAttribArray(0);glDisableVertexAttribArray(1);glDisableVertexAttribArray(2);glEnableVertexAttribArray(3);glVertexAttribPointer(3, 3, GL_FLOAT, 0, 0, (void*)lVerts);glDrawArrays(GL_LINES, 0, number_of_edges);


and could I then just put one boolean in my shader to draw the lines white when true? or do I have to make a different shader for the lines?
You've got a few options, you just want to make sure to use the GL_LINES enum for the glDraw command.

I would probably not want to mess with booleans, I think you want to avoid dynamic branching whenever possible.

How are you coloring your triangles? I would either just send the color with your vertices, or have a uniform that defines the draw color, or a separate shader.

glDisableVertexAttribArray(0);glDisableVertexAttribArray(1);glDisableVertexAttribArray(2);glEnableVertexAttribArray(3);

Regarding this, what do you expect this to do? The attrib array maps to an input variable in the shader, you shouldn't just be setting these to constant values.

You'll should have something like:
vert_attrib_index = glGetAttribLocation(shaderNumber, "myShaderVertexAttribute");color_attrib_index = glGetAttribLocation(shaderNumber, "myShaderColorAttribute");glEnableVertexAttribArray(vert_attrib_index);glEnableVertexAttribArray(color_attrib_index);
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
actually, I kept messing around with it, and got this to work:

glDisable(GL_DEPTH_TEST);glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, (void*)lVerts);glDrawArrays(GL_LINES, 0, objects[2].nEdges*2);


my only problem now is that I use textured triangles, so my lines end up being drawn in the last texture that I used, instead of a solid color =/

I could use gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); in the shader, but can I switch fragment shaders on the fly? or do I have to actually create a whole second shader program? this is what I was talking about with the boolean value. This is only for debugging purposes, so I don't care if I have to branch out and make it slow. I jus need my lines visible.
thanks again for the help
Nah you have to switch programs if you want a new shader.

You can use the boolean switch if you like. (I don't remember if uniform bools exist, but if not you can just use an int).
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
ya. I don't want write a class for multiple shaders right now. Thanks for the help. I'll use the int.

This topic is closed to new replies.

Advertisement