PDA

View Full Version : Help with simple classes and translations ...



adityagaddam
07-28-2003, 05:45 PM
ok.. I am trying to learn opengl and I am trying to make classes for each primitive so that its easier to reuse the code..

this is the primitives.h right now :




// Primitives Header - Sphere/Ellipsoid , Cube/Cuboid, Cylinder, Cone

#pragma once

#include "materials.h"

// - > Cube Class <- Begin //

class i3dCube
{

float cvar_CubeVertices[24][3];


float cvar_CubeWidth;
float cvar_CubeHeight;
float cvar_CubeLength;

float cvar_CubeX;
float cvar_CubeY;
float cvar_CubeZ;

float cvar_CubeRotX;
float cvar_CubeRotY;
float cvar_CubeRotZ;

float cvar_CubeScale;

bool cvar_CubeTexed;

float cvar_CubeColor_AMBI[4];
float cvar_CubeColor_DIFF[4];
float cvar_CubeColor_SPEC[4];
float cvar_CubeColor_EMIT[4];
float cvar_CubeColor_SHINE;

public :
void SetCube (float posX, float posY, float posZ,
float rotX, float rotY, float rotZ,
float Width, float Height, float Length,
float Scale, bool Textured, i3dMaterial Material);

void SetDimensions (float CWidth, float CHeight, float CLength);
void SetPosition (float posX, float posY, float posZ);
void Move(float changeX, float changeY, float changeZ);
void SetRotation (float rotX, float rotY, float rotZ);
void Rotate(float changeX, float changeY, float changeZ);
void SetScale (float Scale);
void Scale(float changeScale);
void SetMaterial (i3dMaterial Material);
void ToggleTexture (bool Textured);
void Render ();

};

// Function - Set all properties of the Cube (Useful for initialization)
void i3dCube::SetCube (float posX, float posY, float posZ,
float rotX, float rotY, float rotZ,
float CWidth, float CHeight, float CLength,
float Scale, bool Textured, i3dMaterial Material)
{

cvar_CubeX = posX;
cvar_CubeY = posY;
cvar_CubeZ = posZ;

cvar_CubeRotX = rotX;
cvar_CubeRotY = rotY;
cvar_CubeRotZ = rotZ;

cvar_CubeWidth = CWidth;
cvar_CubeHeight = CHeight;
cvar_CubeLength = CLength;

cvar_CubeScale = Scale;

cvar_CubeTexed = Textured;

memcpy(cvar_CubeColor_AMBI,Material.MatColor_AMBI, 4);
memcpy(cvar_CubeColor_DIFF,Material.MatColor_DIFF, 4);
memcpy(cvar_CubeColor_SPEC,Material.MatColor_SPEC, 4);
memcpy(cvar_CubeColor_EMIT,Material.MatColor_EMIT, 4);
cvar_CubeColor_SHINE = Material.MatColor_SHINE;

} // Done


// Function - Set Width, Height and Length Only of Cube
void i3dCube::SetDimensions (float CWidth, float CHeight, float CLength)
{

cvar_CubeWidth = CWidth;
cvar_CubeHeight = CHeight;
cvar_CubeLength = CLength;

} // Done

// Function - Set X,Y and Z Position Only of Cube
void i3dCube::SetPosition (float posX, float posY, float posZ)
{

cvar_CubeX = posX;
cvar_CubeY = posY;
cvar_CubeZ = posZ;

} // Done

// Function - Move Cube relatively to its current position
void i3dCube::Move (float changeX, float changeY, float changeZ){
cvar_CubeX = cvar_CubeX + changeX;
cvar_CubeY = cvar_CubeY + changeY;
cvar_CubeZ = cvar_CubeZ + changeZ;
}

// Function - Set X,Y and Z Rotation Only of Cube
void i3dCube::SetRotation (float rotX, float rotY, float rotZ)
{

cvar_CubeRotX = rotX;
cvar_CubeRotY = rotY;
cvar_CubeRotZ = rotZ;

} // Done

// Function - Rotate Cube relatively to its current rotation
void i3dCube::Rotate (float changeX, float changeY, float changeZ){
cvar_CubeRotX = cvar_CubeRotX + changeX;
cvar_CubeRotY = cvar_CubeRotY + changeY;
cvar_CubeRotZ = cvar_CubeRotZ + changeZ;
}

// Function - Set Scale Only of Cube
void i3dCube::SetScale (float Scale)
{

cvar_CubeScale = Scale;

} // Done

// Function - Scale Cube relatively to its current scale
void i3dCube::Scale(float changeScale){

cvar_CubeScale = cvar_CubeScale + changeScale;

}

// Function - Set various properties of the Cube's material
void i3dCube::SetMaterial (i3dMaterial Material)
{
memcpy(cvar_CubeColor_AMBI,Material.MatColor_AMBI, 4);
memcpy(cvar_CubeColor_DIFF,Material.MatColor_DIFF, 4);
memcpy(cvar_CubeColor_SPEC,Material.MatColor_SPEC, 4);
memcpy(cvar_CubeColor_EMIT,Material.MatColor_EMIT, 4);
cvar_CubeColor_SHINE = Material.MatColor_SHINE;

} // Done

// Function - Change setting that determines whether a given texture should be used
void i3dCube::ToggleTexture (bool Textured)
{

cvar_CubeTexed = Textured;

} // Done

// Function - Render the Cube!
void i3dCube::Render()
{

glTranslatef(cvar_CubeX, cvar_CubeY, cvar_CubeZ);
glRotatef(cvar_CubeRotX,1.0,0.0,0.0);
glRotatef(cvar_CubeRotY,0.0,1.0,0.0);
glRotatef(cvar_CubeRotZ,0.0,0.0,1.0);
glScalef(cvar_CubeWidth * cvar_CubeScale, cvar_CubeHeight * cvar_CubeScale, cvar_CubeLength * cvar_CubeScale);

glMaterialfv(GL_FRONT, GL_AMBIENT, cvar_CubeColor_AMBI);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cvar_CubeColor_DIFF);
glMaterialfv(GL_FRONT, GL_SPECULAR, cvar_CubeColor_SPEC);
glMaterialfv(GL_FRONT, GL_EMISSION, cvar_CubeColor_EMIT);
glMaterialf(GL_FRONT, GL_SHININESS, cvar_CubeColor_SHINE);

glPushMatrix();

glBegin(GL_QUADS);

// xy face front
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);

// xy face back
glNormal3f(0.0, 0.0, -1.0);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);

// yz face right
glNormal3f(-1.0, 0.0, 0.0);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(-0.5, -0.5, 0.5);

// yz face left
glNormal3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);

// xz face top
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);

// xz face bottom
glNormal3f(0.0, -1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);


glEnd();

glPopMatrix();

}// Done

// - > Cube Class <- End //

//================================================== ================================================== ========================//

// - > Sphere Class <- Begin //

class i3dSphere
{


float cvar_SphereWidth;
float cvar_SphereHeight;
float cvar_SphereLength;

float cvar_SphereX;
float cvar_SphereY;
float cvar_SphereZ;

float cvar_SphereRotX;
float cvar_SphereRotY;
float cvar_SphereRotZ;

float cvar_SphereScale;

bool cvar_SphereTexed;

float cvar_SphereColor_AMBI[4];
float cvar_SphereColor_DIFF[4];
float cvar_SphereColor_SPEC[4];
float cvar_SphereColor_EMIT[4];
float cvar_SphereColor_SHINE;



public :
void SetSphere (float posX, float posY, float posZ,
float rotX, float rotY, float rotZ,
float Width, float Height, float Length,
float Scale, bool Textured, i3dMaterial Material);

void SetDimensions (float CWidth, float CHeight, float CLength);
void SetPosition (float posX, float posY, float posZ);
void Move(float changeX, float changeY, float changeZ);
void SetRotation (float rotX, float rotY, float rotZ);
void Rotate(float changeX, float changeY, float changeZ);
void SetScale (float Scale);
void Scale(float changeScale);
void SetMaterial (i3dMaterial Material);
void ToggleTexture (bool Textured);
void Render ();

};

// Function - Set all properties of the Sphere (Useful for initialization)
void i3dSphere::SetSphere (float posX, float posY, float posZ,
float rotX, float rotY, float rotZ,
float CWidth, float CHeight, float CLength,
float Scale, bool Textured, i3dMaterial Material)
{

cvar_SphereX = posX;
cvar_SphereY = posY;
cvar_SphereZ = posZ;

cvar_SphereRotX = rotX;
cvar_SphereRotY = rotY;
cvar_SphereRotZ = rotZ;

cvar_SphereWidth = CWidth;
cvar_SphereHeight = CHeight;
cvar_SphereLength = CLength;

cvar_SphereScale = Scale;

cvar_SphereTexed = Textured;

memcpy(cvar_SphereColor_AMBI,Material.MatColor_AMB I,4);
memcpy(cvar_SphereColor_DIFF,Material.MatColor_DIF F,4);
memcpy(cvar_SphereColor_SPEC,Material.MatColor_SPE C,4);
memcpy(cvar_SphereColor_EMIT,Material.MatColor_EMI T,4);
cvar_SphereColor_SHINE = Material.MatColor_SHINE;


} // Done


// Function - Set Width, Height and Length Only of Sphere
void i3dSphere::SetDimensions (float CWidth, float CHeight, float CLength)
{

cvar_SphereWidth = CWidth;
cvar_SphereHeight = CHeight;
cvar_SphereLength = CLength;

} // Done

// Function - Set X,Y and Z Position Only of Sphere
void i3dSphere::SetPosition (float posX, float posY, float posZ)
{

cvar_SphereX = posX;
cvar_SphereY = posY;
cvar_SphereZ = posZ;

} // Done

// Function - Move Sphere relatively to its current position
void i3dSphere::Move (float changeX, float changeY, float changeZ){
cvar_SphereX = cvar_SphereX + changeX;
cvar_SphereY = cvar_SphereY + changeY;
cvar_SphereZ = cvar_SphereZ + changeZ;
}

// Function - Set X,Y and Z Rotation Only of Sphere
void i3dSphere::SetRotation (float rotX, float rotY, float rotZ)
{

cvar_SphereRotX = rotX;
cvar_SphereRotY = rotY;
cvar_SphereRotZ = rotZ;

} // Done

// Function - Rotate Sphere relatively to its current rotation
void i3dSphere::Rotate (float changeX, float changeY, float changeZ){
cvar_SphereRotX = cvar_SphereRotX + changeX;
cvar_SphereRotY = cvar_SphereRotY + changeY;
cvar_SphereRotZ = cvar_SphereRotZ + changeZ;
}

// Function - Set Scale Only of Sphere
void i3dSphere::SetScale (float Scale)
{

cvar_SphereScale = Scale;

} // Done

// Function - Scale Sphere relatively to its current scale
void i3dSphere::Scale(float changeScale){

cvar_SphereScale = cvar_SphereScale + changeScale;

}

// Function - Set various properties of the Sphere's material
void i3dSphere::SetMaterial (i3dMaterial Material)
{
memcpy(cvar_SphereColor_AMBI,Material.MatColor_AMB I,4);
memcpy(cvar_SphereColor_DIFF,Material.MatColor_DIF F,4);
memcpy(cvar_SphereColor_SPEC,Material.MatColor_SPE C,4);
memcpy(cvar_SphereColor_EMIT,Material.MatColor_EMI T,4);
cvar_SphereColor_SHINE = Material.MatColor_SHINE;

} // Done

// Function - Change setting that determines whether a given texture should be used
void i3dSphere::ToggleTexture (bool Textured)
{

cvar_SphereTexed = Textured;

} // Done

// Function - Render the Sphere!
void i3dSphere::Render()
{
GLUquadricObj *g_texturedObject = NULL;

glPushMatrix();

glTranslatef(cvar_SphereX, cvar_SphereY, cvar_SphereZ);
glRotatef(cvar_SphereRotX,1.0,0.0,0.0);
glRotatef(cvar_SphereRotY,0.0,1.0,0.0);
glRotatef(cvar_SphereRotZ,0.0,0.0,1.0);
glScalef(cvar_SphereWidth * cvar_SphereScale, cvar_SphereHeight * cvar_SphereScale, cvar_SphereLength * cvar_SphereScale);

glMaterialfv(GL_FRONT, GL_AMBIENT, cvar_SphereColor_AMBI);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cvar_SphereColor_DIFF);
glMaterialfv(GL_FRONT, GL_SPECULAR, cvar_SphereColor_SPEC);
glMaterialfv(GL_FRONT, GL_EMISSION, cvar_SphereColor_EMIT);
glMaterialf(GL_FRONT, GL_SHININESS, cvar_SphereColor_SHINE);

g_texturedObject = gluNewQuadric();
gluQuadricTexture(g_texturedObject, GL_TRUE);
gluSphere(g_texturedObject,1,20,20);

glPopMatrix();

}// Done

// - > Sphere Class <- End //


Here is the main rendering code




// Render Stuff : Called every frame
void i3dRender(){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glPushMatrix();

//----------------- ALL RENDER STUFF ---------------//

gluLookAt(0.0f,0.0f,50.0f,0.0f,0.0f,-500.0f,0.0f,1.0f,0.0f);

TestCube.Move(0.01,0.01,0.01);
//TestSphere.Move(0.01,0.01,0.01);
TestCube.Rotate(1,1,1);


TestCube.Render();
TestSphere.Render();

//----------------- ALL RENDER STUFF --------------//

glPopMatrix();

}


Note: code does not show initialization and windows window stuff

The problem is when I run the app, the sphere rotates and moves with the cube. I have checked the variables and they are not nameds the same.. so why ?

Also if I move TestSphere.Render above TestCube.Render, I get what I need.. the sphere stays intact and the cube goes through transformations..

Please help... Thanks,
Aditya

[This message has been edited by adityagaddam (edited 07-28-2003).]

mikael_aronsson
07-28-2003, 10:30 PM
Hi !

in your ::Render() method for the cube for example you call glTranslate() and glRotate() before your call glPushMatrix(), if you put the glPushMatrix() at the top of the ::Render() emthod it should work better.

Mikael

adityagaddam
07-29-2003, 05:37 AM
Thanks!!! for the promt reply..

That helped!

One more thing to put on my remember to do always list http://www.opengl.org/discussion_boards/ubb/biggrin.gif

-Aditya
P.S. THANKS AGAIN! :P