开发者

Placing objects from an array in individual places OpenGL GLUT C++

I have an array working but I am not able to place each ball separately, all the balls are being drawn within each other at the origin, here is my code:

Ball.cpp

#include "Ball.h"
#include "Vector2f.h"
#include "Vector3f.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"


Ball::Ball(void)
{
    Vector3f Temp_position;
    position = Temp_position;
    Vector3f Temp_velocity;
    velocity = Temp_velocity;
}


Ball::~Ball(void)
{
}

void Ball::SetPos(Vector3f New_position)
{
    position = New_position;
}


void Ball::Draw()
{
    glPushMatrix();
    glTranslatef(position.X(), position.Y(), position.Z());
    glColor3d(1, 0, 0);
    glutSolidSphere(0.3, 50, 50);
    glPopMatrix();
}

void Ball::ArrayPosition()
{

Ball *Yellowball = new Ball[8];
Yellowball[0].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[1].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[2].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[3].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[4].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[5].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[6].SetPos(Vector3f (position.X(), po开发者_开发技巧sition.Y(), position.Z()));
Yellowball[7].SetPos(Vector3f (position.X(), position.Y(), position.Z()));

}

void Ball::DrawYellow()
{

glPushMatrix();
glColor3f(2,1,0);
glutSolidSphere(0.3, 50, 50);
glPopMatrix();
}

void Ball::SetVel(Vector3f New_velocity)
{
    velocity = New_velocity;
}



Vector3f Ball::GetPos()
{
    Vector3f temp;
    temp = position;
    return temp;
}

Vector3f Ball::GetVel()
{
    Vector3f temp;
    temp = velocity;
    return temp;
}

Display.cpp

#include "Display.h"
#include "Vector3f.h"
#include "Ball.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include <math.h>
#include "Bitmap.h"

Timer Display::m_Timer = Timer();


static float TableWidth = 4;  // Z axis normal = 4
float Display::eyeX = -7.5; //-7.5
float Display::eyeY = 3; //3
float Display::eyeZ = 5; //5
float Display::Position[4] = { 1.0f, 0.0f, -3.5, 1.0f };
float Display::translateZ = -3.5;
float Display::translateX = 0.0;
float Display::lightX = 5.0; //5 2.5
float Display::lightY = 5.0;
float Display::lightZ = 2.5;

float m_TableX = -5.0f;
float m_TableZ = -2.5f;
float m_TableWidth = 2.5f;
float m_TableLength = 5.0f;

float ballx = 0.7;
float bally = 0.1;
float ballz = -0.7;

Ball Redball;
float BALL_RED_START = 0;
float RADIUS_OF_BALL = 0.3;
float BALL_RED_END = 8;

Ball Yellowball; 
float BALL_YELLOW_START = 0;

float BALL_YELLOW_END = 8;

 Bitmap Display::m_HeightMap;
unsigned int Display::m_TextureID;





void Display::Init(int argc, char ** argv)
{
    glutInit(&argc, argv); // initializes glut
    // sets display mode. These parameter set RGB colour model
    // and double buffering.
    glutInitWindowSize(500,500);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("Pool Version 1.0");


    // Set glut callback functions
    glutDisplayFunc(Display::DisplayScene);
    glutIdleFunc(Display::Idle);
    glutReshapeFunc(Display::Resize);
    glutKeyboardFunc(Display::KeyboardInput);
    m_Timer.getSeconds();
    glEnable(GL_DEPTH_TEST);
    glPointSize(5);

        glEnable(GL_NORMALIZE);
        glEnable(GL_LIGHTING);
        glClearColor(0,0,0,1);
        glEnable(GL_COLOR_MATERIAL);

        glutFullScreen();



float white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);

glEnable(GL_LIGHT0);

Redball.SetPos(Vector3f(-5.0,0.5,0.0));
Redball.SetVel(Vector3f(1.0,0.0,0.0));



Ball *Yellowball = new Ball[8];

for(int i = BALL_YELLOW_START; i < BALL_YELLOW_START; i++)
{

Yellowball[0].SetPos(Vector3f (1,0,0));
Yellowball[1].SetPos(Vector3f (0,0,0));
Yellowball[2].SetPos(Vector3f (0,0,0));
Yellowball[3].SetPos(Vector3f (0,0,0));
Yellowball[4].SetPos(Vector3f (0,0,0));
Yellowball[5].SetPos(Vector3f (0,0,0));
Yellowball[6].SetPos(Vector3f (0,0,0));
Yellowball[7].SetPos(Vector3f (0,0,0));
}


//
//Ball Redball[8];
//
//for(int i = BALL_RED_START; i < BALL_RED_START; i++)
//{
//  glColor3f(1,0,0);
//  Redball[i].SetPos(Vector3f (i+128,RADIUS_OF_BALL,45));
//}



glEnable (GL_TEXTURE_2D);
Bitmap image;
image.loadBMP ("myTexture.bmp");
image.loadBMP ("myTexture2.bmp");



glGenTextures(2, &m_TextureID); // change to 2 for 2 textures
glBindTexture ( GL_TEXTURE_2D, m_TextureID);

glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps ( GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);

// Begin glut main loop
    glutMainLoop();
}




void Display::DisplayScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the back buffer

    glPushMatrix();
    glLoadIdentity();
    glNormal3f(0,1,0);
    Vector3f redpos = Redball.GetPos();
    gluLookAt(eyeX, eyeY, eyeZ,     // eye position
            0, 0, 0,        // what I'm looking at
            0.0, 1.0, 0); // Up direction

    float Position[] = {lightX, lightY, lightZ, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, Position);
DrawLight(0, Position);



    /* Rendering code goes here */


//Ball Redball[8];
//for (int i = BALL_RED_START; i<BALL_RED_END;i++)
//{
//
//float x = 0;
//glTranslatef(x+1,1,0);
//glColor3f(1,0,0);
//Redball[i].DrawRed();
//}
//glPopMatrix();

Redball.Draw();

glPushMatrix();
Ball Yellowball[8];

for (int i = BALL_YELLOW_START; i<BALL_YELLOW_END;i++)
{
glColor3f(2,1,0);

glTranslatef(1,0,0); //draws the array in a straight line

Yellowball[i].DrawYellow();

}
glPopMatrix();


drawcue();
drawTable();
drawTableLegFrontLeft();
drawTableLegFrontRight();
drawTableLegBackLeft();
drawTableLegBackRight();
drawCushions();
//drawCircle();
//drawHCircle();
Table(-2,-4.5,2,4.5); // Draws the table top in trianglestrip       -4.5, 0.5, -0.5, 9.5



    glPopMatrix();
    glutSwapBuffers(); // Swap the front and back buffers
}


You create YellowBall arrays three times, set positions and then, well you loose the pointer to the arrays. You're not experiencing a OpenGL problem, but a lack of understanding how a program manages its data.

Also OpenGL has no persistency. It's just a sophisticated drawing API. Drawing commands will not be remembered.

A bit of advice: It seems you suffer from something I call "OOP induced blindnes": You tried to put everything into classes, while not understanding what's actually going on. Try to get rid of that Display class, there's little benefit having that one. Writing a class to manage geometry is not a bad idea by itself, but before you do that you must understand how OpenGL works and how data is passed around in a program. Passing class instances around is orders of degrees more difficult if you want to do it properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜