Sign in to follow this  

Calculating indices for vertex array

This topic is 2040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, im having problems calculating indices for a vertex array, im just trying to display terrain from a heightmap using glDrawElements() (OpenGL btw) using a triangle strip.
Here is the code i have so far (may be slightly confusing)
[code]
GLubyte* getIndeces( int Size ) {

int ySize = Size - 1; // Height of array
int xSize = Size * 2; // Width of array
int Array[ySize+1][xSize]; // Temp Array
int right = (xSize)-1; // Rightmost index of array
int index = Size; // First, Third, Fifth ... etc row
int index2 = (Size*3)-1; // Secound, Fourth Sixth ... etc row
int counter = 1; // New array index
// Initialize the array
for( int y = 0; y < ySize; y++ ) {
for( int x = 0; x < xSize; x++ ) {
Array[y][x] = 0;
}
}
// Fill the array
for( int y = 0; y < ySize; y+=2 ) {
for( int x = 0; x < xSize-1; x+=2 ) {

Array[y][x] = index++;
Array[y][x+1] = index -Size ;

Array[y+1][x] = index2--;
Array[y+1][x+1] = index2-Size;
}
index = index + Size;
index2 = index2 + (Size*3);
}
// Size of the new array
for( int z = 0; z < ySize; z++ ) {
for( int x = 0; x < xSize-1; x++ ) {
indexSize++;
}
}
GLubyte *newArray = new GLubyte[indexSize]; // Allocate a new array

newArray[0] = 0; // The first index is 0
// Add array elements to dynamic array
for( int z = 0; z < ySize; z++ ) {
for( int x = 0; x < xSize-1; x++ ) {
newArray[counter++] = Array[z][x];
}
}
// Add last element
newArray[indexSize] = Array[ySize-1][xSize-2];
// Add One for last element
indexSize++;
return newArray; // Return array
}
[/code]

12 13 14 15
08 09 10 11
04 05 06 07
00 01 02 03

In the order 0 - 4- 1- 5 - 2- 6 - 3- 7- 11- 6- 10 ...etc

Now for a 4x4 Sized image, this works fine, indeces are...
[code]0 8 1 9 2 10 3 11 4 12 5 13 6 14 7 15 23 14 22 13 21 12 20 11 19 10 18 9 17 8 16 24 17 25 18 26 19 27 20 28 21 29 22 30 23 31 39 30 38 29 37 28 36 27 35 26 34 25 33 24 32 40 33 41 34 42 35 43 36 44 37 45 38 46 39 47 55 46 54 45 53 44 52 43 51 42 50 41 49 40 48 56 49 57 50 58 51 59 52 60 53 61 54 62 55 63 [/code]
a 16x16 also works
[code]0 16 1 17 2 18 3 19 4 20 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 47 30 46 29 45 28 44 27 43 26 42 25 41 24 40 23 39 22 38 21 37 20 36 19 35 18 34 17 33 16 32 48 33 49 34 50 35 51 36 52 37 53 38 54 39 55 40 56 41 57 42 58 43 59 44 60 45 61 46 62 47 63 79 62 78 61 77 60 76 59 75 58 74 57 73 56 72 55 71 54 70 53 69 52 68 51 67 50 66 49 65 48 64 80 65 81 66 82 67 83 68 84 69 85 70 86 71 87 72 88 73 89 74 90 75 91 76 92 77 93 78 94 79 95 111 94 110 93 109 92 108 91 107 90 106 89 105 88 104 87 103 86 102 85 101 84 100 83 99 82 98 81 97 80 96 112 97 113 98 114 99 115 100 116 101 117 102 118 103 119 104 120 105 121 106 122 107 123 108 124 109 125 110 126 111 127 143 126 142 125 141 124 140 123 139 122 138 121 137 120 136 119 135 118 134 117 133 116 132 115 131 114 130 113 129 112 128 144 129 145 130 146 131 147 132 148 133 149 134 150 135 151 136 152 137 153 138 154 139 155 140 156 141 157 142 158 143 159 175 158 174 157 173 156 172 155 171 154 170 153 169 152 168 151 167 150 166 149 165 148 164 147 163 146 162 145 161 144 160 176 161 177 162 178 163 179 164 180 165 181 166 182 167 183 168 184 169 185 170 186 171 187 172 188 173 189 174 190 175 191 207 190 206 189 205 188 204 187 203 186 202 185 201 184 200 183 199 182 198 181 197 180 196 179 195 178 194 177 193 176 192 208 193 209 194 210 195 211 196 212 197 213 198 214 199 215 200 216 201 217 202 218 203 219 204 220 205 221 206 222 207 223 239 222 238 221 237 220 236 219 235 218 234 217 233 216 232 215 231 214 230 213 229 212 228 211 227 210 226 209 225 208 224 240 225 241 226 242 227 243 228 244 229 245 230 246 231 247 232 248 233 249 234 250 235 251 236 252 237 253 238 254 239 255 [/code]
However anything above this will only display maybe the first 2 Rows? eg 32x32
[code]0 32 1 33 2 34 3 35 4 36 5 37 6 38 7 39 8 40 9 41 10 42 11 43 12 44 13 45 14 46 15 47 16 48 17 49 18 50 19 51 20 52 21 53 22 54 23 55 24 56 25 57 26 58 27 59 28 60 29 61 30 62 31 63 95 62 94 61 93 60 92 59 91 58 90 57 89 56 88 55 87 54 86 53 85 52 84 51 83 50 82 49 81 48 80 47 79 46 78 45 77 44 76 43 75 42 74 41 73 40 72 39 71 38 70 37 69 36 68 35 67 34 66 33 65 32 64 96 65 97 66 98 67 99 68 100 69 101 70 102 71 103 72 104 73 105 74 106 75 107 76 108 77 109 78 110 79 111 80 112 81 113 82 114 83 115 84 116 85 117 86 118 87 119 88 120 89 121 90 122 91 123 92 124 93 125 94 126 95 127 159 126 158 125 157 124 156 123 155 122 154 121 153 120 152 119 151 118 150 117 149 116 148 115 147 114 146 113 145 112 144 111 143 110 142 109 141 108 140 107 139 106 138 105 137 104 136 103 135 102 134 101 133 100 132 99 131 98 130 97 129 96 128 160 129 161 130 162 131 163 132 164 133 165 134 166 135 167 136 168 137 169 138 170 139 171 140 172 141 173 142 174 143 175 144 176 145 177 146 178 147 179 148 180 149 181 150 182 151 183 152 184 153 185 154 186 155 187 156 188 157 189 158 190 159 191 223 190 222 189 221 188 220 187 219 186 218 185 217 184 216 183 215 182 214 181 213 180 212 179 211 178 210 177 209 176 208 175 207 174 206 173 205 172 204 171 203 170 202 169 201 168 200 167 199 166 198 165 197 164 196 163 195 162 194 161 193 160 192 224 193 225 194 226 195 227 196 228 197 229 198 230 199 231 200 232 201 233 202 234 203 235 204 236 205 237 206 238 207 239 208 240 209 241 210 242 211 243 212 244 213 245 214 246 215 247 216 248 217 249 218 250 219 251 220 252 221 253 222 254 223 255 31 254 30 253 29 252 28 251 27 250 26 249 25 248 24 247 23 246 22 245 21 244 20 243 19 242 18 241 17 240 16 239 15 238 14 237 13 236 12 235 11 234 10 233 9 232 8 231 7 230 6 229 5 228 4 227 3 226 2 225 1 224 0 32 1 33 2 34 3 35 4 36 5 37 6 38 7 39 8 40 9 41 10 42 11 43 12 44 13 45 14 46 15 47 16 48 17 49 18 50 19 51 20 52 21 53 22 54 23 55 24 56 25 57 26 58 27 59 28 60 29 61 30 62 31 63 95 62 94 61 93 60 92 59 91 58 90 57 89 56 88 55 87 54 86 53 85 52 84 51 83 50 82 49 81 48 80 47 79 46 78 45 77 44 76 43 75 42 74 41 73 40 72 39 71 38 70 37 69 36 68 35 67 34 66 33 65 32 64 96 65 97 66 98 67 99 68 100 69 101 70 102 71 103 72 104 73 105 74 106 75 107 76 108 77 109 78 110 79 111 80 112 81 113 82 114 83 115 84 116 85 117 86 118 87 119 88 120 89 121 90 122 91 123 92 124 93 125 94 126 95 127 159 126 158 125 157 124 156 123 155 122 154 121 153 120 152 119 151 118 150 117 149 116 148 115 147 114 146 113 145 112 144 111 143 110 142 109 141 108 140 107 139 106 138 105 137 104 136 103 135 102 134 101 133 100 132 99 131 98 130 97 129 96 128 160 129 161 130 162 131 163 132 164 133 165 134 166 135 167 136 168 137 169 138 170 139 171 140 172 141 173 142 174 143 175 144 176 145 177 146 178 147 179 148 180 149 181 150 182 151 183 152 184 153 185 154 186 155 187 156 188 157 189 158 190 159 191 223 190 222 189 221 188 220 187 219 186 218 185 217 184 216 183 215 182 214 181 213 180 212 179 211 178 210 177 209 176 208 175 207 174 206 173 205 172 204 171 203 170 202 169 201 168 200 167 199 166 198 165 197 164 196 163 195 162 194 161 193 160 192 224 193 225 194 226 195 227 196 228 197 229 198 230 199 231 200 232 201 233 202 234 203 235 204 236 205 237 206 238 207 239 208 240 209 241 210 242 211 243 212 244 213 245 214 246 215 247 216 248 217 249 218 250 219 251 220 252 221 253 222 254 223 255 31 254 30 253 29 252 28 251 27 250 26 249 25 248 24 247 23 246 22 245 21 244 20 243 19 242 18 241 17 240 16 239 15 238 14 237 13 236 12 235 11 234 10 233 9 232 8 231 7 230 6 229 5 228 4 227 3 226 2 225 1 224 0 32 1 33 2 34 3 35 4 36 5 37 6 38 7 39 8 40 9 41 10 42 11 43 12 44 13 45 14 46 15 47 16 48 17 49 18 50 19 51 20 52 21 53 22 54 23 55 24 56 25 57 26 58 27 59 28 60 29 61 30 62 31 63 95 62 94 61 93 60 92 59 91 58 90 57 89 56 88 55 87 54 86 53 85 52 84 51 83 50 82 49 81 48 80 47 79 46 78 45 77 44 76 43 75 42 74 41 73 40 72 39 71 38 70 37 69 36 68 35 67 34 66 33 65 32 64 96 65 97 66 98 67 99 68 100 69 101 70 102 71 103 72 104 73 105 74 106 75 107 76 108 77 109 78 110 79 111 80 112 81 113 82 114 83 115 84 116 85 117 86 118 87 119 88 120 89 121 90 122 91 123 92 124 93 125 94 126 95 127 159 126 158 125 157 124 156 123 155 122 154 121 153 120 152 119 151 118 150 117 149 116 148 115 147 114 146 113 145 112 144 111 143 110 142 109 141 108 140 107 139 106 138 105 137 104 136 103 135 102 134 101 133 100 132 99 131 98 130 97 129 96 128 160 129 161 130 162 131 163 132 164 133 165 134 166 135 167 136 168 137 169 138 170 139 171 140 172 141 173 142 174 143 175 144 176 145 177 146 178 147 179 148 180 149 181 150 182 151 183 152 184 153 185 154 186 155 187 156 188 157 189 158 190 159 191 223 190 222 189 221 188 220 187 219 186 218 185 217 184 216 183 215 182 214 181 213 180 212 179 211 178 210 177 209 176 208 175 207 174 206 173 205 172 204 171 203 170 202 169 201 168 200 167 199 166 198 165 197 164 196 163 195 162 194 161 193 160 192 224 193 225 194 226 195 227 196 228 197 229 198 230 199 231 200 232 201 233 202 234 203 235 204 236 205 237 206 238 207 239 208 240 209 241 210 242 211 243 212 244 213 245 214 246 215 247 216 248 217 249 218 250 219 251 220 252 221 253 222 254 223 255 31 254 30 253 29 252 28 251 27 250 26 249 25 248 24 247 23 246 22 245 21 244 20 243 19 242 18 241 17 240 16 239 15 238 14 237 13 236 12 235 11 234 10 233 9 232 8 231 7 230 6 229 5 228 4 227 3 226 2 225 1 224 0 32 1 33 2 34 3 35 4 36 5 37 6 38 7 39 8 40 9 41 10 42 11 43 12 44 13 45 14 46 15 47 16 48 17 49 18 50 19 51 20 52 21 53 22 54 23 55 24 56 25 57 26 58 27 59 28 60 29 61 30 62 31 63 95 62 94 61 93 60 92 59 91 58 90 57 89 56 88 55 87 54 86 53 85 52 84 51 83 50 82 49 81 48 80 47 79 46 78 45 77 44 76 43 75 42 74 41 73 40 72 39 71 38 70 37 69 36 68 35 67 34 66 33 65 32 64 96 65 97 66 98 67 99 68 100 69 101 70 102 71 103 72 104 73 105 74 106 75 107 76 108 77 109 78 110 79 111 80 112 81 113 82 114 83 115 84 116 85 117 86 118 87 119 88 120 89 121 90 122 91 123 92 124 93 125 94 126 95 127 159 126 158 125 157 124 156 123 155 122 154 121 153 120 152 119 151 118 150 117 149 116 148 115 147 114 146 113 145 112 144 111 143 110 142 109 141 108 140 107 139 106 138 105 137 104 136 103 135 102 134 101 133 100 132 99 131 98 130 97 129 96 128 160 129 161 130 162 131 163 132 164 133 165 134 166 135 167 136 168 137 169 138 170 139 171 140 172 141 173 142 174 143 175 144 176 145 177 146 178 147 179 148 180 149 181 150 182 151 183 152 184 153 185 154 186 155 187 156 188 157 189 158 190 159 191 223 190 222 189 221 188 220 187 219 186 218 185 217 184 216 183 215 182 214 181 213 180 212 179 211 178 210 177 209 176 208 175 207 174 206 173 205 172 204 171 203 170 202 169 201 168 200 167 199 166 198 165 197 164 196 163 195 162 194 161 193 160 192 224 193 225 194 226 195 227 196 228 197 229 198 230 199 231 200 232 201 233 202 234 203 235 204 236 205 237 206 238 207 239 208 240 209 241 210 242 211 243 212 244 213 245 214 246 215 247 216 248 217 249 218 250 219 251 220 252 221 253 222 254 223 255 [/code]
Now maybe, and hopefully this is just something small, that iv overlooked, but iv been staring at it to long now to see it ha.
Any help would be greatfully appreciated, or evan a better method to this.

Thanks ! Edited by mcgrane66

Share this post


Link to post
Share on other sites
I think triangle strips essentially are an implicit form of indexing, and that you should therefore not need indices.

Essentially each new vertex forms a triangle with the previous two vertices, and therefore no indices are required.

This [url="http://en.wikipedia.org/wiki/Triangle_strip"]wikipedia article[/url] has an OpenGL example and some more information

Share this post


Link to post
Share on other sites
You'll need indexes if you want to joint multiple strips in a single call, but overall - with perhaps some weird exceptions in the mobile arena - triangle strips should be considered redundant, and a triangle list (GL_TRIANGLES) with indexes should be preferred. It gives better vertex reuse (in particular with this kind of mesh), requires no degenerate triangles and is more likely to be the optimal case on current (and even older - often much older) hardware. It's also much easier to construct.

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1336743216' post='4939290']
You'll need indexes if you want to joint multiple strips in a single call, but overall - with perhaps some weird exceptions in the mobile arena - triangle strips should be considered redundant, and a triangle list (GL_TRIANGLES) with indexes should be preferred. It gives better vertex reuse (in particular with this kind of mesh), requires no degenerate triangles and is more likely to be the optimal case on current (and even older - often much older) hardware. It's also much easier to construct.
[/quote]

Ah of course, forgot about the multiple strips usage.

Share this post


Link to post
Share on other sites

This topic is 2040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this