PDA

View Full Version : C++ Classes Question



adityagaddam
10-17-2002, 04:10 PM
ok.. I am making this open gl engine.. I have learnt a lot while making this but I want to use classes instead of strcts so I rewriting the engne.. I have the following code in a header file called primitives.h.. this will handle all the primitives ... (ofcourse all of them are not coded yet). I started with the cube.


//-----------------------------------Class------------------------------------------//
// //
// Class : S3DCube //
// Purpose : Cube / Cuboid //
//-----------------------------------Begin------------------------------------------//

class S3DCube
{

public:
float mcube_X;
float mcube_Y;
float mcube_Z;
float mcube_Width;
float mcube_Height;
float mcube_Length;
float mcube_Color[3];
float mcube_vertices[23][2];
void CubeProperties (float X_Position, float Y_Position, float Z_Position,
float Width, float Height, float Length);

void RenderCube();

};


//------------------------------------End-------------------------------------------//


//-------------------------------Class:Function-------------------------------------//
// //
// Function : CubeProperties //
// Purpose : Creates a cube and sets the related variables of the cube according to //
// given dimensions and related parameters given to the function //
// Note: Does not render the cube. Only initializes the variables //
// //
//-----------------------------------Begin------------------------------------------//

void S3DCube::CubeProperties (float X_Pos, float Y_Pos, float Z_Pos,
float CubeWidth, float CubeHeight, float CubeLength)
{

mcube_X = X_Pos;
mcube_Y = Y_Pos;
mcube_Z = Z_Pos;
mcube_Width = CubeWidth;
mcube_Height = CubeHeight;
mcube_Length = CubeLength;

mcube_vertices = {

0.0f, 0.0f, 0.0f,
0.0f, 0.0f, -(mcube_Length),
-(mcube_Width), 0.0f, -(mcube_Length),
-(mcube_Width), 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
-(mcube_Width), 0.0f, 0.0f,
-(mcube_Width), -(mcube_Height), 0.0f,
0.0f, -(mcube_Height), 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, -(mcube_Height), 0.0f,
0.0f, -(mcube_Height), -(mcube_Length),
0.0f, 0.0f, -(mcube_Length),
-(mcube_Width), 0.0f, 0.0f,
-(mcube_Width), 0.0f, -(mcube_Length),
-(mcube_Width), -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), 0.0f,
0.0f, 0.0f, 0.0f,
-(mcube_Width), -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), -(mcube_Length),
0.0f, -(mcube_Height), -(mcube_Length)


};

}


//-------------------------------Class:Function-------------------------------------//
// //
// Function : RenderCube //
// Purpose : Renders the Cube //
// //
//-----------------------------------Begin------------------------------------------//

void S3DCube::RenderCube()
{

int ivert;

glBegin(GL_POLYGON);

for (ivert = 0; ivert <= 24; ++ivert)
{

glVertex3f(mcube_vertices[ivert][0], mcube_vertices[ivert][1], mcube_vertices[ivert][2]);

}

glEnd();

}


But it gives me the following errors when I compile:

--------------------Configuration: Surreal 3D Project - Win32 Debug--------------------
Compiling...
main.cpp
f:\aditya\opengl\projects\surreal 3d project\buildprimitives.h(59) : error C2059: syntax error : '{'
f:\aditya\opengl\projects\surreal 3d project\buildprimitives.h(59) : error C2143: syntax error : missing ';' before '{'
f:\aditya\opengl\projects\surreal 3d project\buildprimitives.h(87) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

Surreal 3D Project.exe - 3 error(s), 0 warning(s)

Can someone please help me?

Thanks a lot,
Aditya

coredump
10-17-2002, 08:35 PM
you see the line mcube_vertices = { and then it's an @$$ load of values? you can't initialize an array like that. you can only use that method when you create the array variable.

b

adityagaddam
10-18-2002, 03:52 AM
well... is there an easier way than putting in each array value? I mean when you are filling in the array after you have already declared it somewhere else...

Thanks,
-Aditya

adityagaddam
10-18-2002, 04:26 AM
ok never mind my previous problem.. I found a way around... what I am doing now is this...

//-----------------------------------Class------------------------------------------//
// //
// Class : S3DCube //
// Purpose : Cube / Cuboid //
//-----------------------------------Begin------------------------------------------//

class S3DCube
{

public:
float mcube_X;
float mcube_Y;
float mcube_Z;
float mcube_Width;
float mcube_Height;
float mcube_Length;
float mcube_Color[3];
float mcube_vertices[24][3];
void CubeProperties (float X_Position, float Y_Position, float Z_Position,
float Width, float Height, float Length);

void RenderCube();

};


//------------------------------------End-------------------------------------------//


//-------------------------------Class:Function-------------------------------------//
// //
// Function : CubeProperties //
// Purpose : Creates a cube and sets the related variables of the cube according to //
// given dimensions and related parameters given to the function //
// Note: Does not render the cube. Only initializes the variables //
// //
//-----------------------------------Begin------------------------------------------//

void S3DCube::CubeProperties (float X_Pos, float Y_Pos, float Z_Pos,
float CubeWidth, float CubeHeight, float CubeLength)
{

mcube_X = X_Pos;
mcube_Y = Y_Pos;
mcube_Z = Z_Pos;
mcube_Width = CubeWidth;
mcube_Height = CubeHeight;
mcube_Length = CubeLength;

float tmp_vertices[24][3] = {

0.0f, 0.0f, 0.0f,
0.0f, 0.0f, -(mcube_Length),
-(mcube_Width), 0.0f, -(mcube_Length),
-(mcube_Width), 0.0f, 0.0f,

0.0f, 0.0f, 0.0f,
-(mcube_Width), 0.0f, 0.0f,
-(mcube_Width), -(mcube_Height), 0.0f,
0.0f, -(mcube_Height), 0.0f,

0.0f, 0.0f, 0.0f,
0.0f, -(mcube_Height), 0.0f,
0.0f, -(mcube_Height), -(mcube_Length),
0.0f, 0.0f, -(mcube_Length),

-(mcube_Width), 0.0f, 0.0f,
-(mcube_Width), 0.0f, -(mcube_Length),
-(mcube_Width), -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), 0.0f,

0.0f, 0.0f, 0.0f,
0.0f, -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), 0.0f,

0.0f, 0.0f, 0.0f,
-(mcube_Width), -(mcube_Height), -(mcube_Length),
-(mcube_Width), -(mcube_Height), -(mcube_Length),
0.0f, -(mcube_Height), -(mcube_Length)


};

int ivert;

for (ivert = 1; ivert <= 24; ++ivert)
{

mcube_vertices[ivert][1] = tmp_vertices[ivert][1];
mcube_vertices[ivert][2] = tmp_vertices[ivert][2];
mcube_vertices[ivert][3] = tmp_vertices[ivert][3];

}

}


//-------------------------------Class:Function-------------------------------------//
// //
// Function : RenderCube //
// Purpose : Renders the Cube //
// //
//-----------------------------------Begin------------------------------------------//

void S3DCube::RenderCube()
{

int ivert;

glTranslatef(mcube_X - (mcube_Width / 2),mcube_Y - (mcube_Height / 2),mcube_Z - (mcube_Length / 2));

glBegin(GL_POLYGON);

for (ivert = 1; ivert <= 24; ++ivert)
{

glVertex3f(mcube_vertices[ivert][1], mcube_vertices[ivert][2], mcube_vertices[ivert][3]);

}

glEnd();

}


It is not giving me any errors but when I run it.. I get this error in a Message box:

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

The instruction at "0x0012fe8a" referenced memory at "0x00000001".The memory could not be "read".

Click on OK to terminate the program
Click on Cancel to debug the program

[OK] [Cancel]

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


Please Help!

Thanks,
Aditya

[This message has been edited by adityagaddam (edited 10-18-2002).]

Bob
10-18-2002, 04:28 AM
Fill it element by element, "by hand".

You can also do it like this. Make an array of the same size somewhere in your file. Preferably at file scope or so.



const float some_array_name[32][2] =
{
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, -(mcube_Length),

.
.
.

0.0f, -(mcube_Height), -(mcube_Length)
};

Then, when initializing your object in CubeProperties(), copy the entire array to where you want it.



copy(some_array_name, some_array_name + 46, mcube_vertices);

You probably have to #include <algorithm> to get the copy function. 46, by the way, is the number of elements in the array, 23 * 2 in your case.

Bob
10-18-2002, 04:33 AM
OK, we posted at the same time, you already do what I suggested http://www.opengl.org/discussion_boards/ubb/smile.gif

I can see one two mistakes in your code. First, ou have declared mcube_vertices as [23][2], but you treat is as it it was a [23][3] array. Second, your indices into the array are one based, but C++ arrays are zero based. Elements are counted from 0, not 1.

adityagaddam
10-18-2002, 04:49 AM
thanks Bob,

It is now working YAYA!!! YIPPEE!!!


Thanks again.
Aditya