OpenGL DrawArrays acting strangely

Started by
4 comments, last by Trienco 12 years, 1 month ago
Hello. Whenever I try to draw a square using OpenGL's drawarrays function, it acts strange... it draws my square, but it also draws a giant polygon-like thing right below it. I can't get an image (because Vista doesn't capture what I draw with OpenGL with PrntScreen sleep.png), but I was wondering if it was something I am doing wrong?

[size="2"]How I call it: drawSquare(30, 30, 20, 20);


Function:

[size=2][color=#7f0055][size=2][color=#7f0055]void[size=2][color=#000000] drawSquare([size=2][color=#7f0055][size=2][color=#7f0055]const[size=2][color=#000000] [size=2][color=#005032][size=2][color=#005032]GLshort[size=2][color=#000000] posX, [size=2][color=#7f0055][size=2][color=#7f0055]const[size=2][color=#000000] [size=2][color=#005032][size=2][color=#005032]GLshort[size=2][color=#000000] posY, [size=2][color=#7f0055][size=2][color=#7f0055]const[size=2][color=#000000] [size=2][color=#005032][size=2][color=#005032]GLshort[size=2][color=#000000] sizeX, [size=2][color=#7f0055][size=2][color=#7f0055]const[size=2][color=#000000] [size=2][color=#005032][size=2][color=#005032]GLshort[size=2][color=#000000] sizeY){



[size=2][color=#005032][size=2][color=#005032]GLshort[size=2] vertices[8];



[size=2][color=#3f7f5f][size=2][color=#3f7f5f]//bottom left


[size=2]vertices[0] = posX;


[size=2]vertices[1] = posY;


[size=2][color=#3f7f5f][size=2][color=#3f7f5f]//bottom right


[size=2]vertices[2] = posX + sizeX;


[size=2]vertices[3] = posY;


[size=2][color=#3f7f5f][size=2][color=#3f7f5f]//top right


[size=2]vertices[4] = posX + sizeX;


[size=2]vertices[5] = posY - sizeY;


[size=2][color=#3f7f5f][size=2][color=#3f7f5f]//top left


[size=2]vertices[6] = posX;


[size=2]vertices[7] = posY - sizeY;



[size=2][color=#642880][size=2][color=#642880]glEnableClientState[size=2](GL_VERTEX_ARRAY);


[size=2][color=#642880][size=2][color=#642880]glEnableClientState[size=2](GL_TEXTURE_COORD_ARRAY);



[size=2][color=#642880][size=2][color=#642880]glVertexPointer[size=2](2, GL_SHORT, 0, vertices);


[size=2][color=#642880][size=2][color=#642880]glColor4f[size=2](1.0f, 1.0f, 0.0f, 0.5f);


[size=2][color=#642880][size=2][color=#642880]glDrawArrays[size=2](GL_QUADS, 0, 8);



[size=2][color=#642880][size=2][color=#642880]glDisableClientState[size=2](GL_VERTEX_ARRAY);


[size=2][color=#642880][size=2][color=#642880]glDisableClientState[size=2](GL_TEXTURE_COORD_ARRAY);


[size=2]}
Advertisement
A (x or y) coordinate isn't a vertex and a quad only has 4 vertices. Why are you passing 8?
f@dzhttp://festini.device-zero.de

A (x or y) coordinate isn't a vertex and a quad only has 4 vertices. Why are you passing 8?



I thought it treated every two ints as a vertex?
You're already telling it to use 2 shorts per vertex, glDrawArrays expects a vertex count, not a component count.

You're already telling it to use 2 shorts per vertex, glDrawArrays expects a vertex count, not a component count.


Thanks, guys! Fixed it, and it runs perfectly now. I do have one last question: can I just enable and disable the vertex and textrue arrays when I startup and shutdown my program, or should I enable and disable them every time I draw? I'm trying to conserve speed.
Now that you mention it. You enable texture coord arrays but never set up the pointer. Ignoring what any defaults maybe you are technically lucky that it doesn't crash. In other words, no, it's not safe to always keep them enabled unless you make sure to always have them setup and point at valid data. Deciding "I don't need textures for this part" and just keeping it enabled will use whatever you set up last. And if whatever you set up doesn't have at least as many texture coordinates as you have vertices now, you will be again lucky if it doesn't crash.

It's perfectly fine to have them enabled by default, but not when you don't actually use them.
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement