Polygon point data type with Arrays

I don't understand why I can use

---------

POINT point_list[1] = { POStoFIXED(10.0, 10.0, 0.0); }

---------

But not

---------

POINT point_list[1];

* * *

point_list[0] = POStoFIXED(10.0, 10.0, 0.0);

---------

Performing the second example results in GCC giving me "parse error before { " on any line with POStoFIXED. I understand that POStoFIXED is just a macro that returns an array sequence, and I guess it doesn't make sense to do this:

point_list[0] = { 10.0, 10.0, 10.0 };

But, if I can't do that, that means I'll never be able to assign a new vector to any polygon with POStoFIXED; I'd have to do it with individual elements of the array (because point_list would actually be a multidimensional array, I guess...), and I know I'm kinda new to C, but...it just doesn't seem right that I can't assign a new vector to the array by index. Am I doing something wrong?
 
That syntax is legal only when initializing arrays/structs. Elsewhere you have to assign to each element separately or use a memcopy or similar.
 
New question, sorry if this is getting annoying. I know this has to be almost elementary...

On page 196 of the SGL Reference (not the actual manual), the PDATA (polygon model structure) has the following definition:

-------

typedef struct PDATA

{

POINT *pntb1

uint32 nbPoint;

POLYGON *pltb1

uint32 nbPolygon

ATTR *attbl1

}

-------

Whenever I try the following...

-------

PDATA model;

model.pntb1 = point_array;

-------

GCC says "structure has no member pntb1". ...Why would the structure have members in the Reference that I can't access here?

EDIT: checking the actual Manual, the 3D tutorials (page 48) in there don't use the members of PDATA, instead they treat assigning PDATA members like initializing an array (it would be a list of the objects in the above type defintion, rather than any actual members assigned). So...I guess the Reference goofed? Or maybe it's outdated...
 
Are you sure you don't mean the pltbl member?

As I said above, the syntax is legal when initializing structs/arrays, so the example is correct. Ie. this is OK:
Code:
typedef struct {

  int a;

  int b;

} foo;

foo bar = {1, 2};
but this is not:
Code:
typedef struct {

  int a;

  int b;

} foo;

foo bar;

bar = {1, 2};
For the second example, you would have to do
Code:
foo bar;

foo.a = 1;

foo.b = 2;
 
I think I understand the syntax, and I figured out the problem. I was typing "pntb1" instead of "pntbl". I realized it stood for table, but I forgot to change my 1 to an L. ...oops :)
 
I ran into something else...and I believe I know how typecasting should work, but...well, anyway...

It's two problems.

1. A simple function to assign values of a POINT SGL type...

------

void oVertex(POINT *point, float x, float y, float z) {

//Set the coordinates for a Vertex, or a POINT type structure.



point[0] = (FIXED)18;

point[1] = (FIXED)18;

point[2] = (FIXED)18;

}

------

When I compile this, on each of the three lines GCC returns "incompatible types in assignment". I don't see how, as each element of POINT is a FIXED...and I really have no clue here. EDIT: I really suppose I should use toFIXED instead of typecasting FIXED, but it still gives me the same error.

2. Another function, similar, to add a POINT type to a PDATA vertex list.

-------

void oModel_SetVertex(PDATA *model, int vertex, POINT *point) {

//Set the vertex for a certain point in the point list of a model.



//Out of bounds, return;

if (vertex > (*model).nbPoint-1) return;



(*model).pntbl[vertex][0] = (FIXED)point[0];

(*model).pntbl[vertex][1] = (FIXED)point[1];

(*model).pntbl[vertex][2] = (FIXED)point[2];

}

-------

This one compiles fine, but I keep getting warnings about "passing argument 3 from incompatible pointer type" whenever I use it. How is this? I know my problems have to be in typecasting, but I can't figure out what I'm doing wrong...I've looked up typecasting on some reference websites, and I can't quite understand what I'm not doing right. EDIT: fixed.

I also need to go look up whatever an "Lvalue" is, because whatever it is, I apparently can't typecast it right, either...
 
I've never used polygons before, but to set sprite coordinates I do this:

Code:
stat[spriteNum][0] = toFIXED(star->x);

stat[spriteNum][1] = toFIXED(star->y);

stat[spriteNum][1] = toFIXED(star->z);

The toFIXED function converts an integer value to whatever it is the saturn uses. Give that toFixed() function a go instead of trying to typecast it.
 
But shouldn't x, y, and z be float? (Oh, right, sprite values)

Ah, well, I'll try it.

I figured out some interesting things about problem #2.

When I use two array parameters in a function, and assign the values of one to another:

void SetArray(int *dest, int *source)

If I access the individual values of the Source array (to assign it), it gives me a warning unless I put "&" in front of the array. But it doesn't give me a warning if I don't put "&" in front of dest; in fact, it gives me a warning if I do!

If I don't assign the individual values of Source, but just assign Source's pointer, then I have to leave the "&" off of source or it gives me a warning. ...I'm not sure I understand that, but apparently pointing to an array is supposed to be hard to grasp for the beginner, so I guess I'll get it eventually...

Any of these warnings are regarding the "trying to assign incompatible pointer type".

EDIT: I also figured out a solution to problem 1.

-------

void oVertex(POINT *point, float x, float y, float z) {

//Set the coordinates for a Vertex, or a POINT type structure.

(*point)[0] = (FIXED)x;

(*point)[1] = (FIXED)y;

(*point)[2] = (FIXED)z;

}

-------

I had to set the assignment to assign to a space pointed to by "point", and I had to pass point with & to get the address (even though point was an array). ...I think that's how it works.
 
The issue with your problem one, if you didn't figure it out yourself, is that in the code you first posted you were trying to assign to elements in a POINT array, rather than to the elements of a single POINT. By using the indirection operator (*) you say that you are trying to access elements of the POINT array in the given pointer. You must do this in your second function (oModel_SetVertex) as well or you'll end up assigning the addresses of elements of some fictional POINT array instead of the coordinates you want.

Also, if you have a pointer to a struct, C has a shorthand syntax for accessing members of that struct:

Code:
(*model).pntbl[vertex][0] = (*point)[0]; /*is equivalent to*/

model->pntbl[vertex][0] = (*point)[0];

Finally, you should never use floats or doubles directly in your code. The only place they're allowed is as constant arguments to the toFIXED macro. The point of this is that converting them as constants lets the compiler do it beforehand, which means you can avoid having to include costly floating-point emulation in your program. Casting to a FIXED is also not the same as using the conversion macro, as casting to an integral type will simply discard the fractional part.
 
I remade it using macros rather than typecasting. Thanks for the help, hopefully I won't run into too many array related problems anymore.
 
You got the option to convert existing .DXF 3d models to C include files(named .SGL on Mac and .MDL on Windows).

On antimes page you can find the official SEGA 3d editor, which on Mac can open SG3 and DXF files(although the later never worked for me) and on windows got the command line tool DXF2SG3.EXE for conversion. Both of them allow Saturn specific 3d settings and the export to C includes.

The SEGA 3D format SG3 is a textual description, like many other formats do.

There was this 3D creation software, which got Saturn support through export plugins, I guess. Can someone help me how it was called? Maybe Lightwave 3D? I tried to get it, but never succeeded.
 
Originally posted by antime@Sun, 2005-01-09 @ 09:37 PM

I believe most modelling packages that targeted consoles also targeted the Saturn at some point. Eg. Softimage 3D and Alias|Wavefront.

[post=127398]Quoted post[/post]​


Now that you list it: I meant Softimage3D(not Lightwave3D), of course.

Shit, we need those apps RIGHT NOW! :smash
 
AFAIK Alias|Wavefront was only available on SGI workstations at that time, better luck with Softimage. Writing exporters for Blender or some other current modeller would still be more valuable, IMO.
 
I'm back, with a new data type problem...this has nothing to do with arrays, I'm sure of it :)

I've defined a function before called

oModel_SetVertex(PDATA *model, int vertex, int x, int y, int z)

Which sets the correct vertex in the point table "pntbl" of PDATA type "model" to the given coordinates.

The problem is, my finished polygon, when I was done, wouldn't display. I wondered what the problem is and I decided to make a function to print out all the data on my PDATA models, essentially an slPrintPDATA(PDATA *model).

Anyway, I did this, and I found out something unusual.

When I use

-----------

slPrintFX(toFIXED(-10.0), slLocate(9, 6));

-----------

The Saturn prints -10.0 on the screen. But, when I use...

-----------

(*model).pntbl[0][0] = toFIXED(-10.0);

slPrintFX((*model).pntbl[0][0], slLocate(9, 6));

-----------

to assign the X coordinate of the model's first vertex, the Saturn prints "8192.00832" on the screen.

Why is the directly assigned coordinate value changed so drastically? There's nothing else in my code messing with that value, and I figure this is why I can't get my polygons to display -- they're off the other edge of the visual world. Does anyone know what I'm doing wrong?

EDIT: -------

Here's something new. It turns out, the above code actually works.

However, if I do the following...

-----------

(*model).pntbl[0][0] = toFIXED(-10.0);

(*model).pntbl[0][1] = toFIXED(10.0);

-----------

Then the vertex coordinate number begins to mess up. Why does assigning a second element alter the value of the first? Is there an important lesson I'm missing here? Perhaps it is an array problem after all. As a side note, I also tried assigning values used the constant X, Y, and Z values in the POINT arrays, ie pntbl[0][X] = toFIXED(-10.0), but that doesn't solve anything.
 
Try to reduce your program to a minimal testcase and post that, it will be far quicker than us trying to guess from the random bits you have posted.
 
Right.

Code:
--------------

main.c

--------------

//Include the SGL.

#include "sgl.h"

void oModel_Setup(PDATA *model, Uint32 num_points, Uint32 num_poly)	{

//I use this to quickly create the tables for a PDATA model.

	POINT temp_point_list[num_points];

	POLYGON temp_poly_list[num_poly];

	ATTR temp_attr_list[num_poly];

	

	(*model).pntbl = temp_point_list;

	(*model).nbPoint = num_points;

	(*model).pltbl = temp_poly_list;

	(*model).nbPolygon = num_poly;

	(*model).attbl = temp_attr_list;

	}

void oModel_VertexSet(PDATA *model, int vertex, float x, float y, float z)	{

//Sets the coordinates for a certain vertex in the point list of a model.

//Note: Pass the POINT parameter using "&".

	

	//Out of bounds, return;

	if (vertex > (*model).nbPoint-1) return;

	

	(*model).pntbl[vertex][X] = toFIXED(x);  //Goes in fine...

	(*model).pntbl[vertex][Y] = toFIXED(y);  //Goes in fine...

	(*model).pntbl[vertex][Z] = toFIXED(z);  //Goes in fine...

	}

//Begin!

void ss_main()	{

	slInitSystem(TV_320x224, NULL, 1);

	//Create the polygon model, with 4 points and 1 face.

	PDATA my_model;

	oModel_Setup(&my_model, 4, 1);

	

	//Set up vertex list.

	//Set vertices.

	oModel_VertexSet(&my_model, 0, -10.0, -10.0, 0.0);	

	

	//Actual program!

	slPrint("Sample program 2.2", slLocate(9,2));

	slPrintFX(toFIXED(-10.0), slLocate(9, 5));

	slPrintFX(my_model.pntbl[0][X], slLocate(9, 6));  //Comes out weird...

	slPrintFX(my_model.pntbl[0][Y], slLocate(9, 7));  //Comes out weird...

	slPrintFX(my_model.pntbl[0][Z], slLocate(9, 8));  //Comes out weird...

	

	while(1){

 slSynch();

 }

	}

//eof

--------------

The problem is that I believe when my VertexSet applies coordinates to all three array positions, they end up affecting each other. I don't know why. Maybe I'm messing up on a missing type cast or something. But this is enough to bring the error.

Note: When I only assign one coordinate, such as X, then it works fine. Values are only weird when I try to assign all three coordinates.
 
Hi Omni!

The POINT, POLYGON and ATTR variables are what you named them like: temporary.

The storage is lost and possibly used for other stuff after the function oModel_Setup() is exited.

As you want to create those variables/arrays of dynamic size, you should use malloc to allocate memory.

Something like:

model->pntbl = (POINT *)malloc(num_points * sizeof(POINT));
 
Back
Top