PDA

View Full Version : Defining object structures

nexusone
01-16-2002, 08:08 AM
I am just getting back into progamming with C and want to create a object structure for vertex varibles.

My problem is defining the pointer for the vertex array and storage.

example:
// define a point in 3D space
typedef struct
{
GLfloat xyz[3];

}Point3F;

// define my primitive object
typedef struct
{
int Sides;
GLfloat Color3f[3];
Point3F* Vertex3f;
}Primitive3F;

Primitive Myobject[];

main()
{
GLinit() // GL setup stuff omitted.

CreatePolygon(Myobject);

}

CreatePolygon(*ploygon)
{
do math stuff here and return vertex data
}

I have all the math stuff done, just creating the structure correctly to house the data.

Thanks.

Keermalec
01-16-2002, 09:20 AM
So whats the problem?

nexusone
01-16-2002, 11:18 AM
Originally posted by Keermalec:
So whats the problem?

In how to correctly write the pointer to the vertex data structure and allocat the data space for the storage of the data.

With the example above, I get pointer errors.

nexusone
01-16-2002, 11:33 AM
Here is my program!
Let me know how I can improve my pointer or data storage handeling.

/* main.c */

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include "mystuff.h"

static int year = 0, day = 0;

//static Point3F dataf[15]; //= {{1.0f,1.0f,0.0f}, {1.0f,0.0f,0.0f}, {0.0f,0.0f,0.0f}, {0.0f,1.0f,0.0f}, {1.0f,1.0f,0.0f}};

static Primitive3F object[1];

static Point3F Vmemory[100];

Point3F buildquad( int sides, Point3F polygon[])
{
float angle, xangle;
float x, y, z;
float r;
int i;

r = 2.0f;
xangle = 3.1415927f / sides;

for (i=0; i <= sides; i++)
{
angle = xangle * i * 2;
polygon[i].xyz[0] = r * cos( angle ); // X
polygon[i].xyz[1] = r * sin( angle ); // Y
polygon[i].xyz[2] = 0.0f; // Z
}
polygon[i].xyz[0] = polygon[0].xyz[0];
polygon[i].xyz[1] = polygon[0].xyz[1];
polygon[i].xyz[2] = polygon[0].xyz[2];

}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
object[0].Vertex3f = Vmemory;
object[0].Sides = 5;
object[0].Color3f[0] = 1.0f;
object[0].Color3f[1] = 1.0f;
object[0].Color3f[2] = 1.0f;
}

void display(void)
{
int i;

glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);

glPushMatrix();

glBegin(GL_POLYGON);
glColor3f (1.0, 1.0, 1.0);
for(i=0;i<=object[0].Sides;i++)
{
glVertex3fv( &object[0].Vertex3f[i] );
}
/* glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
*/
glEnd();

glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'd':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

/* Mystuff.h

by Eric Stringer

*/

typedef struct
{
GLfloat xyz[3];

}Point3F;

typedef struct
{
int Sides;
GLfloat Color3f[3];
Point3F* Vertex3f;
}Primitive3F;

Thug
01-17-2002, 05:32 AM
I did not go into a lot of your code... But just glancing at it I noticed that you have the following code in one of your functions.

polygon[i].xyz[0] = polygon[0].xyz[0];
polygon[i].xyz[1] = polygon[0].xyz[1];
polygon[i].xyz[2] = polygon[0].xyz[2];

Even though it is called only one it can be rewritten as
polygon[i] = polygon[0]

It is possible to assign structures to another structure if and only if they are the same datatype. This will only make one call to copy the structure instead of three.

nexusone
01-17-2002, 05:47 AM
You are right on that one, I had overlooked it.

Thanks

Originally posted by Thug:
I did not go into a lot of your code... But just glancing at it I noticed that you have the following code in one of your functions.

polygon[i].xyz[0] = polygon[0].xyz[0];
polygon[i].xyz[1] = polygon[0].xyz[1];
polygon[i].xyz[2] = polygon[0].xyz[2];

Even though it is called only one it can be rewritten as
polygon[i] = polygon[0]

It is possible to assign structures to another structure if and only if they are the same datatype. This will only make one call to copy the structure instead of three.