Open GL, GlDrawElements, GL_LINE_LOOP Does Not Connect Vertices Correctly
When I draw one square with following vertices:
private static float[] vertexArray = {
// vertex
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f
};
private static short indicesArray[] = {
0, 1, 2,
0, 2, 3,
};
result is good it draws loop correctly
but when I draw another parallel square by following
private static float[] vertexArray = {
// vertex
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
};
private static short indicesArray[] = {
0, 1, 2,
0, 2, 3,
4, 5, 6,
4, 6, 7,
};
I don't know why it does that. I think there something that i don't know about how GL_LINE_LOOP works. GL_LINE_STRIPS give same result too. How to fix this? I want the squares to be separate.
code is following:
GLES30.glDrawElements(GLES30.GL_LINE_LOOP, indicesArray.length,GLES30.GL_UNSIGNED_SHORT,0);
Thanks in Advance.
sorry for big pictures I hope someone gonna help me to fix it.
Answer
If you want to draw multiple GL_LINE_LOOP
with a single draw call, then you can use the Primitive Restart technology.
The fixed primitiv restart indes has to be 2^N-1, where N is the number of bits of the data type which is used for the indices.
This means 255 for GL_UNSIGNED_BYTE
, 65535 for GL_UNSIGNED_SHORT
and 2147483647 for GL_UNSIGNED_INT
.
Add the index to the index list, in between the indices which form the separated primitives:
private static short indicesArray[] = {
0, 1, 2,
0, 2, 3,
65535, // restart primitive
4, 5, 6,
4, 6, 7,
};
Primitive restart with fixed indices has to be enabled:
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
In desktop OpenGL it would be possible to choose the restart index by glPrimitiveRestartIndex
. This has to be enabled by glEnable(GL_PRIMITIVE_RESTART)
.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View