Teen Programmers Unite  
 

 

Return to forum top

Direct3D

Posted by ScaryGuy [send private reply] at July 28, 2002, 09:50:10 PM

Could someone tell me why

CUSTOMVERTEX triangle[] =
{
{x, y, z, rhw, color, },
{x, y, z, rhw, color, },
{x, y, z, rhw, color, },
}
works, but


CUSTOMVERTEX *triangle = new CUSTOMVERTEX[3];

triangle[0].x = x;
triangle[0].y = y;
triangle[0].z = z;
triangle[0].rhw = rhw;
triangle[0].color = color;

triangle[1].x = x;
triangle[1].y = y;
triangle[1].z = z;
triangle[1].rhw = rhw;
triangle[1].color = color;

triangle[2].x = x;
triangle[2].y = y;
triangle[2].z = z;
triangle[2].rhw = rhw;
triangle[2].color = color;

doesnt work?

Posted by CodeRed [send private reply] at July 28, 2002, 10:52:53 PM

should it be:

triangle[0] = x;
triangle[1] = y;
triangle[2] = z;
triangle[3] = rhw;
triangle[4] = color;

triangle[5] = x2;
triangle[6] = y2;
triangle[7] = z2;
triangle[8] = rhw2;
triangle[9] = color2;

triangle[10] = x3;
triangle[11] = y3;
triangle[12] = z3;
triangle[13] = rhw3;
triangle[14] = color3;


Maybe???? (why not use OpenGL instead?)

Posted by ScaryGuy [send private reply] at July 29, 2002, 02:56:21 AM

I agree with opengl, boss doesnt agree with opengl...therefore I dont agree with opengl:-(

Posted by CodeRed [send private reply] at July 29, 2002, 03:08:28 AM

Try what I said using this as the first line:

CUSTOMVERTEX *triangle = new CUSTOMVERTEX[];

because if you look at the one that works, it is a variable sized array that you put 15 elements into (0-14)

Posted by Psion [send private reply] at July 29, 2002, 08:03:05 AM

I think CodeRed's advice is completely bizarre and will almost certainly lead to a crash.

ScaryGuy, if you would explain what "doesn't work" means, then perhaps I could help.

Posted by miken [send private reply] at July 29, 2002, 10:33:43 AM

Interestingly enough, your top example will definitely not work (for many, many reasons), but the bottom one should! First, you forgot a semicolon after the last brace. Second, you have commas after 'color' on each line that will cause compile errors. Also note that if the variable is declared outside of a function body, it is likely that the variables used inside of the declaration don't exist yet (unless they're constants). I see nothing wrong with the second example. Here is what the top one should look like:

CUSTOMVERTEX triangle[] =
{
{ x, y, z, rhw, color },
{ x, y, z, rhw, color },
{ x, y, z, rhw, color },
};

I hope that helps some ...

Posted by CodeRed [send private reply] at July 29, 2002, 04:24:19 PM

"I think CodeRed's advice is completely bizarre and will almost certainly lead to a crash"

Says you... (I never said I knew D3D, I assumed what he said worked actually worked)

Posted by Psion [send private reply] at July 29, 2002, 08:35:11 PM

miken, there is nothing wrong with his first code as long as it occurs in the proper place (or if this is C++), x/y/z/rhw/color are defined, and we assume that he just forgot the semicolon in pasting it here. I tend to believe people when they say things "work," though "it doesn't work" is confusing. ;-) Check the ANSI standard or some other reference if you want to see that you are allowed to have "extra" commas like he had.

Posted by CodeRed [send private reply] at July 29, 2002, 10:48:10 PM

Psion, now I am intrigued, please explain something to me.

Isn't this setting up an array of type CUSTOMVERTEX?

"CUSTOMVERTEX triangle[];"

If so, then in this:

CUSTOMVERTEX triangle[] =
{
{x, y, z, rhw, color, },
{x, y, z, rhw, color, },
{x, y, z, rhw, color, },
}

x, y, z, rhw, and color have to be of type CUSTOMVERTEX right? That is obviously not what he wants.

It seems to me that the code that he says doesn't work should (because he is creating a 3 element array of type CUSTOMVERTEX and setting up all the member variables, which is what you would want to do I can only assume) and the code that he says does, shouldn't (because x, y, z etc. would have to be CUSTOMVERTEX objects)

????????????????????????????????????????????????????????????

Posted by unknown_lamer [send private reply] at July 30, 2002, 09:30:56 AM

No, the { }, inside of the other { } means that CUSTOMVERTEX is probably a struct of some sort and x, y, z, rhw, and color are being assigned to the members of the struct.

Posted by CodeRed [send private reply] at July 30, 2002, 05:08:08 PM

Oh, okay. Not that familiar with structs, is that how their constructors work? Cus thats definately not a class

Posted by CViper [send private reply] at August 05, 2002, 04:49:07 AM

It's not a constructor, rather some kind of "blind copy".

Posted by ScaryGuy [send private reply] at August 06, 2002, 01:55:10 AM

sry for not postin in a while. first of all, id like to thank @Psion for defending me. Id also like to say that having comma's at the end of 'color' will NOT cause compiler issues. Also...

It's not a constructor, rather some kind of "blind copy".

It is a constructor, rather the 'copy' constructor. Remember, the only difference between a struct and a class is that a structs members are dafaulted to public.

Now that I got a cool discussion goin, heres what im actually tryin to do. normally, you can give the vertexbuffer a fixed amount of vertices, thus...

CUSTOMVERTEX triangle[] =
{
{x, y, z, rhw, color, },
{x, y, z, rhw, color, },
{x, y, z, rhw, color, },
}

and when u go to draw the primitive, it draws it. But what if you were to put D3D in a class...and a function addVertex(), so u can keep adding vertices until the heart desires, as in...

CUSTOMVERTEX *triangle = new CUSTOMVERTEX[iNumVertices];

unfortunatly, I go to draw the primitive, and it dont draw it.

Any ideas?

Posted by Psion [send private reply] at August 06, 2002, 07:47:00 AM

I don't see what that alternate declaration has to do with "putting D3D in a class."

Posted by CViper [send private reply] at August 06, 2002, 03:01:09 PM

hmm... let me see.. if you got a class like this in the beginning

class c1
{
public/whatever:
CUSTOMVERTEX tri[];
...
};

you can pass a pointer to an instance of c1 to the D3D calls and it will work (provided that 'tri' is the first member in the class). However if you change that to
class c2
{
public:
CUSTOMVERTEX* p;
...
};

you have to pass 'p', since a pointer to a instance of c2 will point to 'p' instead of the data.

Of course that is a wild guess about what is going on :)
Posted by ScaryGuy [send private reply] at August 07, 2002, 11:33:01 AM

I don't see what that alternate declaration has to do with "putting D3D in a class."

It doesnt have anything to do with putting it in a class, but it has everything to do about making a vertexbuffer that's dynamic.

CViper - U got a good idea that'll work with a little modyfying...but I dont want my CUSTOMVERTEX to be public...all members must be private to ensure someone doesnt do things they dont need to be doin:-D

Posted by CViper [send private reply] at August 07, 2002, 01:40:14 PM

you can declare it private too - won't matter :)
Just remember that the pointer to any instance of any class will point to the first data member in it (no matter if it's private, protected or public).

Posted by ScaryGuy [send private reply] at August 08, 2002, 12:01:25 AM

sry viper, i read through what you were tryin to say too quickly...is this waht your tryin to do??

Given that all D3D calls are in the class, make a class

class c1
{
public/whatever:
CUSTOMVERTEX tri[];
...
};

make a pointer of that class in the main game code

c1 * classPointer;

and then pass that pointer into its own calls??

if so, theres a much easier and smaller memory way.

Posted by CViper [send private reply] at August 08, 2002, 03:28:43 AM

not into it's own calls (thats already 'this'), but rather to the d3d calls. (or opengl or whatever you use)

eg.

c1 instance;
DrawInterleavedArray( (CUSTOMVERTEX*)&instance );

is the "same" as (provided that tri is the first data member in the class and *not* a pointer to dynamcily allocated memory)
DrawInterleavedArray( instance.tri );


DrawInterleavedArray is just a example, you have to use whatever drawing functions you would use otherwise.
And it won't matter is tri is a protected member in the first example; the second wont work if that's the case, unless DrawInterleavedArray is declared a friend to the class (urgh)
Posted by ScaryGuy [send private reply] at August 14, 2002, 12:56:40 AM

having to cal the D3D functions outside of the manager ruins the purpose of having a manager.

Now before we go to far askew, I'd like to throw out a hint that we're headin of track...and it's gotta do with what both arrays are (first post)...what type of arrays they are;-)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.