glBegin/glEnd to glDrawElements() - opengl

I've been trying to port this immediate mode(glBegin/glEnd) code to direct mode(VAs) for rendering a plane. Please let me know if the direct mode code will exactly work as the immediate mode code.
Note: consider a 50X50 mesh
Immediate mode code:
int once=0, a=0,b=0;
for(int j=0; j<50-1; j++)
{
once=0;
for(int i=0; i<50; i++)
{
a=i+j*(50);
b=i+(j+1)*50;
if(once)
{
glBegin(GL_TRIANGLE_STRIP);
once=1;
}
else
{
glTexCoord2f(Texture[a].x, Texture[a].y);
glVertex2f(Mesh[a].x, Mesh[a].y);
glTexCoord2f(Texture[a].x, Texture[a].y);
glVertex2f(Mesh[b].x, Mesh[b].y);
}
}
if(once)
{
glEnd();
}
}
Direct mode code:
unsigned int indexArray[50*50];
int idx=0;
for(int j=0; j<50-1; j++)
{
for(int i=0; i<50; i++)
{
a=i+j*(50);
b=i+(j+1)*50;
indexArray[idx]=a;
indexArray[idx+1]=b;
idx+=2;
}
}
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(2dPoint), Texture);
glVertexPointer(3, GL_FLOAT, sizeof(2dPoint), Mesh);
glDrawElements(GL_TRIANGLE_STRIP, (50-1)*(50-1)*2, GL_UNSIGNED_INT, indexArray);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Note: 2dPoint is a structure for 2 floating point values holding x and y
Update
After correcting the glVertexPointer() for 2-d co-ordinates. I observed the triangulation happening the following way:
With glBegin()-glEnd():
/\ /\ /\ /\ /
/ \ / \ / \ / \ /
\ / \ / \ / \ / \ /
\ / \ / \ / \/ \ /
\/ \ \ / /\ \ /
/\ / \ \/ / \ \/
/ \ / \ /\ / \ /\
/ \ / \ / \ / \ / \
\ / \ / \ / \ / \
\ / \ / \/ \/ \
\ / \ \ /\ /\ \
\ / \ / \ / \ / \ \
\/ \ / \ / \ / \ \
/\ \ / \ / \ / \ \
/ \ \ / / \ / \ \
/ \ / / \ \/ \ \
/ \ / \ / \ /\ \ \
\ / \ / \ / \ \ \
\/ \ / \ / \ \ \
/\ / \ / \ \ \
With glDrawElements():
/\ /\ /\ /\ /
/ \ / \ / \ / \ /
\ / \ / \ / \ / \ /
\ / \ / \ / \/ \ /
--\/--------\--------\--/-------/\-------\--/
/\ / \ \/ / \ \/
/ \ / \ /\ / \ /\
/ \ / \ / \ / \ / \
------\-/-------\---/----\--/--------\--/----\
\ / \ / \/ \/ \
\ / \ \ /\ /\ \
\ / \ / \ / \ / \ \
\/ \ / \ / \ / \ \
/\ \ / \ / \ / \ \
/ \ \ / / \ / \ \
-/----\----- \-------/-\--------\/----------\-------\
/ \ / \ / \ /\ \ \
\ / \ / \ / \ \ \
\/ \ / \ / \ \ \
/\ / \ / \ \ \
Sorry for the alignment issues in the illustration. but as you can see, with the index array and glDrawElements(), the number of triangles increased. how can i modify the index array to match the winding similar to the results of glBegin()/glEnd()?

Since you only have 2d coordinates, the glVertexPointer call is wrong.
glVertexPointer(3, GL_FLOAT, sizeof(2dPoint), Mesh);
This line tell OpenGL to always read 3 floats per vertex, so if you only have 2 of them you have to change it to:
glVertexPointer(2, GL_FLOAT, sizeof(2dPoint), Mesh);
^

Related

How can I merge two X-Macros together?

I have a lot of repetitive code that needs me to use different sets of data frequently in some function or some operation. i.e as shown below (the numbers and letters are just place holders, all i need to do is string two sets of data together using x macros)
a = 1
a = 2
a = 3
a = 4
.
.
.
then
b = 1
b = 2
b = 3
.
.
.
and
c = 1
c = 2
c = 3
.
.
.
I was trying to create an X-macro that combines the following two X-macros into one
//X-macro 1
#define SET_1 \
X(a) \
X(b) \
X(c) \
//X-macro 2
#define SET_2 \
X(1) \
X(2) \
X(3) \
X(4)
Any help?
How about this approach:
#define X_abc(X,X2) \
X(a,X2) \
X(b,X2) \
X(c,X2)
#define X_1234(x,X2) \
X2(x,1) \
X2(x,2) \
X2(x,3) \
X2(x,4)
#define SET(x,y) x = y;
#define DEFINE(x,y) int x = y;
X_abc(X_1234,DEFINE)

C++ Boost::Spirit parsing complex boolean expressions and constructing an equivalent tree

Our input expressions are similar to this (even more complex):
( ( ?var1 <= (?var2 + 125) && ?var1 > (?var2 + 10) ) || !(?var1 == ?var3) )
Note: variables are always started by either '?' or '_'
Our desired output:
||
/ \
/ \
/ \
/ \
/ \
&& !
/ \ |
/ \ |
/ \ ==
/ \ / \
/ \ ?var1 ?var3
<= >
/ \ / \
/ \ / \
/ \ / \
?var1 + ?var1 +
/ \ / \
/ \ / \
/ \ / \
?var2 125 ?var2 10
Your helps are really appreciated.

How to print the following pattern by using while-loops in python?

How can I print the following pattern using while loops in python?
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
\ \ ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! / /
\ \ \ \ ! ! ! ! ! ! ! ! ! ! ! ! ! ! / / / /
\ \ \ \ \ \ ! ! ! ! ! ! ! ! ! ! / / / / / /
\ \ \ \ \ \ \ \ ! ! ! ! ! ! / / / / / / / /
\ \ \ \ \ \ \ \ \ \ ! ! / / / / / / / / / /
Any help would be really appreciated. Thank you.
This may also be solved using for loop but since the question explicitly mentioned while loop:
total = 22
n = 0
while n < 6:
side = n*2
middle = total - side*2
line = '\\'*side + '!'*middle + '/'*side
print(line)
n += 1
Let me know if anything needs explanation.
there are two easy ways to do this
while True:
print '! '*22
print '\ '*2+'! '*18+'/ '*2
print '\ '*4+'! '*14+'/ '*4
print '\ '*6+'! '*10+'/ '*6
print '\ '*8+'! '*6+'/ '*8
print '\ '*10+'! '*2+'/ '*10
break
or maybe
x=0
while x<6:
print '\ '*(2*x)+'! '*(22-(4*x))+'/ '*(2*x)
x+=1
Try this:
ExcCount = 22
SlashCount = 2
i = 1
while i <= 6:
String = (SlashCount * "\ ") + (ExcCount * "! ") + (SlashCount * "/ ")
print(String)
ExcCount -= 4
SlashCount -= 2
i += 1

__m256d TRANSPOSE4 Equivalent?

Intel has included __MM_TRANPOSE4_PS to transpose a 4x4 matrix of vectors. I'm wanting to do the equivalent with __m256d. However, I can't seem to figure out how to get _mm256_shuffle_pd in the same manner.
_MM_TRANSPOSE4_PS Code
#define _MM_TRANSPOSE4_PS(row0, row1, row2, row3) { \
__m128 tmp3, tmp2, tmp1, tmp0; \
\
tmp0 = _mm_shuffle_ps((row0), (row1), 0x44); \
tmp2 = _mm_shuffle_ps((row0), (row1), 0xEE); \
tmp1 = _mm_shuffle_ps((row2), (row3), 0x44); \
tmp3 = _mm_shuffle_ps((row2), (row3), 0xEE); \
\
(row0) = _mm_shuffle_ps(tmp0, tmp1, 0x88); \
(row1) = _mm_shuffle_ps(tmp0, tmp1, 0xDD); \
(row2) = _mm_shuffle_ps(tmp2, tmp3, 0x88); \
(row3) = _mm_shuffle_ps(tmp2, tmp3, 0xDD); \
}
My attempt at a _MM_TRANSPOSE4_PD inside a loop i need it in
for (int copy = i; copy < m2.size();)
{
__m256d row0 = _mm256_load_pd(m2data + copy);
copy += m2.col();
__m256d row1 = _mm256_load_pd(m2data + copy);
copy += m2.col();
__m256d row2 = _mm256_load_pd(m2data + copy);
copy += m2.col();
__m256d row3 = _mm256_load_pd(m2data + copy);
copy += m2.col();
__m256d tmp3, tmp2, tmp1, tmp0;
tmp0 = _mm256_shuffle_pd(row0,row1, 0x44);
tmp2 = _mm256_shuffle_pd(row0,row1, 0xEE);
tmp1 = _mm256_shuffle_pd(row2,row3, 0x44);
tmp3 = _mm256_shuffle_pd(row2,row3, 0xEE);
row0 = _mm256_shuffle_pd(tmp0, tmp1, 0x88);
row1 = _mm256_shuffle_pd(tmp0, tmp1, 0xDD);
row2 = _mm256_shuffle_pd(tmp2, tmp3, 0x88);
row3 = _mm256_shuffle_pd(tmp2, tmp3, 0xDD);
_mm256_store_pd(reinterpret_cast<double*>(buffer + counter++),row0);
_mm256_store_pd(reinterpret_cast<double*>(buffer + counter++),row1);
_mm256_store_pd(reinterpret_cast<double*>(buffer + counter++),row2);
_mm256_store_pd(reinterpret_cast<double*>(buffer + counter++),row3);
}
Here is the macro equivalent of the solution I found.
#define _MM_TRANSPOSE4_PD(row0,row1,row2,row3) \
{ \
__m256d tmp3, tmp2, tmp1, tmp0; \
\
tmp0 = _mm256_shuffle_pd((row0),(row1), 0x0); \
tmp2 = _mm256_shuffle_pd((row0),(row1), 0xF); \
tmp1 = _mm256_shuffle_pd((row2),(row3), 0x0); \
tmp3 = _mm256_shuffle_pd((row2),(row3), 0xF); \
\
(row0) = _mm256_permute2f128_pd(tmp0, tmp1, 0x20); \
(row1) = _mm256_permute2f128_pd(tmp2, tmp3, 0x20); \
(row2) = _mm256_permute2f128_pd(tmp0, tmp1, 0x31); \
(row3) = _mm256_permute2f128_pd(tmp2, tmp3, 0x31); \
}

Duplicate / multiplicate tree efficiently

I need (for g++) a (computation time) optimized algorithm tree structure to duplicate/multiplicate a tree.
My tree will be a k-ary tree, but not necessarily filled.
The main operation is to multiplicate (up to k times) the existing tree and add the trees as subtrees to a new node. Then the leaf node level will be erased to hold the fixed-level rule.
Does anybody know of a data structure offering this?
An example for the multiplication: Suppose we have a binary tree
A
|
/ \
/ \
B C
| / \
| / \
D E F
and we want to add a new node / multiply like
R
/ \
/ \
.. ..
So the result will look like
R
/ \
/ \
/ \
/ \
/ \
A A
| |
/ \ / \
/ \ / \
B C B C
| / \ | / \
| / \ | / \
D E F D E F
I tried to organize this on a std::vector in a heap-like structure, but multiplying the tree is still kind of slow, because I have to copy each tree level by itself rather than just copying the whole tree at once.
When you add R, it is trivial to give it 2 pointers to A, rather than copying the entire subtree starting at A.
R
/ \
| |
\ /
A
|
/ \
/ \
B C
| / \
| / \
D E F
This is both very fast and very easy to code.
Now, the hitch in this comes in if you later want to update one side of the tree, but not the other. For example, perhaps you want to change the "right" F to a G. At that point you can use a copy-on-write strategy on only certain of the nodes, in this case leading to
R
/ \
/ \
A A <-- copied, left side points to B
| / \
/ \ * \
/ \ \
B C C <-- copied, left side points to E
| / \ / \
| / \ * \
D E F G
Basically, you only need to copy the path from the point of the change (F/G) up to either the root (easiest to implement) or up to the highest node that is shared (A in this example).
Maybe take a look on Androids code for the T9-dictionary. AFAIR it looks flat, but basically what they do is build a tree of letters, so that traversing the tree from top to bottom makes words. And I think they used relative offsets to jump from on node to the next (like a linked list).
So you should be able to copy the whole tree in one run.
I don't remember the exact layout thou, and i think it didn't do ugly padding as I do here, but to continue w/ your example it would look something(!) like this:
# your tree
__________
/// _ \ _
/// /// \ \ /// \
A007021B007000D000000C007014E000000F000000
\\\_/ \\\_____/
# copying it, "under" R:
__________ __________
_ /// _ \ _ /// _ \ _
/// \ /// /// \ \ /// \ /// /// \ \ /// \
R007049A007021B007000D000000C007014E000000F000000A007021B007000D000000C007014E000000F000000
\\\ \\\_/ \\\_____/ / \\\_/ \\\_____/
\\\______________________________________/