HI there,
I've been trying to implement a class of a ball i was given by an tutor but am having troubles.
She gave me:
TBall.h

class TBall
{
public:
double _radius; // defines the radius of the ball
TVector _position; // defines the position of the ball
TVector _velocity; // defines the velocity v0 of the ball
// Constructors
TBall();
TBall(const double& Radius, const TVector& Position, const
TVector& Velocity)
{_radius=Radius; _position=Position; _velocity=Velocity;};
// Selectors
double GetBallRadius() const {return _radius;};
TVector GetBallPosition() const {return _position;};
TVector GetBallVelocity() const {return _velocity;};
void DrawBall(); // Draws the ball
void CalculateVelocity(const TVector& velocity, const double&
seconds); // Assigns the ball a velocity
TVector CalcDistanceTravelled(const double& seconds) const;
// Calculates the distance traveled
void MoveBall(const double& seconds); // Moves the ball
};

TBall.cpp

#include "tball.h"

TBall::TBall()
{
// Assign default values for the attributes
// of the ball
_radius = 4.0;
_position = TVector(1.0, 1.0, 1.0);
_velocity = TVector(0.1, 0.0, 0.0);
}

void TBall::DrawBall()
{
glPushMatrix();
glTranslatef(_position.X(),
_position.Y(), _position.Z());
glutSolidSphere(_radius, 20, 20);
glPopMatrix();
}

Both the code segements have no errors within them (i have all the associated vector,matrix etc header files.
I believe my problem is how to implement it within the display loop.
I use &TBall::DrawBall; But the ball doesnt seem to appear. Has ayone got any advice on how i get this to work?

Is this clear? i hope it is.

Thanks in advance from me!

Dani AI

Generated

Short answer: ’s use of &TBall::DrawBall only produces a pointer-to-member, it does not call the method, and it cannot be used where a C-style function pointer is required (for example glutDisplayFunc). is right that a method must be invoked on an instance — the common GLUT solution is to register a non-member display function that calls the instance's DrawBall().

Example (older C++ / GLUT style — keep the object accessible to the free function):

// global instance
TBall gBall;

void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0,0,10,  0,0,0,  0,1,0); // camera
  gBall.DrawBall();
  glutSwapBuffers();
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  // create window...
  glutDisplayFunc(display);
  glutMainLoop();
}

If a pointer-to-member was intentional, it must be called on an instance, e.g.:

void (TBall::*pmf)() = &TBall::DrawBall;
TBall b;
(b.*pmf)();    // calls b.DrawBall()

Troubleshooting checklist (common reasons a sphere “doesn’t appear”):

  • Confirm display() is actually registered with glutDisplayFunc and being called (put a console print or breakpoint in it).
  • Ensure GL_DEPTH_TEST is enabled and glutInitDisplayMode includes GLUT_DEPTH.
  • Verify projection/camera (near/far planes) so the ball isn’t clipped or behind the camera; use gluLookAt / gluPerspective or a matching glOrtho.
  • Check lighting/material or color — with lighting off a white sphere on white background can be invisible.
  • Make sure model transforms happen while GL_MATRIXMODE is GL_MODELVIEW and that glPushMatrix/glPopMatrix bracket transforms.
  • Use glGetError() if nothing renders.

These points extend ’s correction by showing how to integrate a member-drawing method safely into GLUT and how to debug common visibility problems.

Well &TBall::DrawBall; is just a pointer to a function.

To actually call it, you need say TBall::DrawBall()

Maybe even

TBall theBall;
// init code
theBall.DrawBall();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.