C++ 在OpenGL Glut中绘制对象数组

C++ 在OpenGL Glut中绘制对象数组,c++,arrays,opengl,object,C++,Arrays,Opengl,Object,我一直在尝试为我正在制作的一个游戏绘制多个球,我试图使其正常工作,但在我的init类和display方法类中存在问题,我已经在错误所在的位置对其进行了注释 B.h: #pragma once #include "Vector2f.h" #include "Vector3f.h" class Ball { private: Vector3f position; Vector3f velocity; public: Ball(void); ~Ball(void);

我一直在尝试为我正在制作的一个游戏绘制多个球,我试图使其正常工作,但在我的init类和display方法类中存在问题,我已经在错误所在的位置对其进行了注释

B.h:

#pragma once
#include "Vector2f.h"
#include "Vector3f.h"

class Ball
{
private:
    Vector3f position;
    Vector3f velocity;

public:
    Ball(void);
    ~Ball(void);

    void Draw();
    void SetPos(Vector3f New_position);
    void SetVel(Vector3f New_velocity);
    Vector3f GetPos();
};
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::SetVel(Vector3f New_velocity)
{
    velocity = New_velocity;
}

Vector3f Ball::GetPos()
{
    Vector3f temp;
    temp = position;
    return temp;
}
我希望能够在Main.cpp中绘制一组球

Main.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>



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;
//Timer Display::m_Timer = Timer();
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;
float m_ball;




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);


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

glEnable(GL_LIGHT0);
Redball.SetPos(Vector3f(0.0,0.3,0.0));

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));  //I tried this but it doesn't work Error C2227
}



    // Begin glut main loop
    glutMainLoop();
}




void BallMovement()
{
    //Vector3f position(0.0,0.3,0.0);
    /*Redball.SetPos(Vector3f(0.0,0.3,0.0));*/
    Vector3f New_velocity(0.01,0,0);
    Redball.SetVel(New_velocity);

    Vector3f New_position;
    Vector3f Old_position;

    Old_position = Redball.GetPos();

    //New_position = Old_position + New_velocity;

    New_position.SetX(Old_position.X() + New_velocity.X());
    New_position.SetY(Old_position.Y() + New_velocity.Y());
    New_position.SetZ(Old_position.Z() + New_velocity.Z());


    Redball.SetPos(New_position);
}





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


    glPushMatrix();
    glLoadIdentity();
    glNormal3f(0,1,0);
    Vector3f didums = 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 */

for (int i = BALL_RED_START; i<BALL_RED_END;i++)
{
glColor3f(1,0,0);
Redball[i]->Draw();     //I tried this but it doesn't work Error C2227
}




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
}

void Display::Resize(int w, int h)
{
    /* Resize is called when window is resized */
    glMatrixMode(GL_PROJECTION); // set matrix mode to profection
    // this dictates how the 3d scene is "squashed" onto the 2d screen
    glLoadIdentity();
    glViewport(0, 0, w, h);  // Set the part of the window to use.
    gluPerspective(45,              // field of view
                (float)w/(float)h,  // ration of window
                1,                  // front clipping plane
                1000                // back clipping plane
                );          // set the area in the 3d scene to draw

    glMatrixMode(GL_MODELVIEW); // Setthe matrix mode to model view
        // the matrix specifies how the 3d scene is viewed
    /*glLoadIdentity();
    gluLookAt(-3.5, 2, eyeZ,        // eye position
            1, 1, 0,        // what I'm looking at
            0.0, 1.0, 0); // Up direction*/
}




void Display::Idle()
{
    /* When nothing else is happening, idle is called.
     * Simulation should be done here and then
     * the display method should be called
     */

    BallMovement();

    glutPostRedisplay();
}
Main.cpp
#包括“Display.h”
#包括“Vector3f.h”
#包括“Ball.h”
#包括“Glut/Glut.h”
#包括“总账/总账h”
#包括“GL/glu.h”
#包括
静态浮动表宽度=4;//Z轴法线=4
浮动显示:eyeX=-7.5//-7.5
浮动显示::eyeY=3//3.
浮动显示::eyeZ=5//5.
浮动显示::位置[4]={1.0f,0.0f,-3.5,1.0f};
浮动显示::translateZ=-3.5;
浮动显示::translateX=0.0;
//计时器显示::m_Timer=Timer();
浮动显示::lightX=5.0//5 2.5
浮动显示::lightY=5.0;
浮动显示::lightZ=2.5;
浮动m_表x=-5.0f;
浮动m_表z=-2.5f;
浮动m_台宽=2.5f;
浮动m_表长度=5.0f;
浮球x=0.7;
浮球y=0.1;
浮球z=-0.7;
红球;
浮球\u红色\u开始=0;
浮球的浮球半径=0.3;
浮球\u红色\u端=8;
浮球;
void Display::Init(int argc,char**argv)
{
glutInit(&argc,argv);//初始化glut
//设置显示模式。这些参数设置RGB颜色模式
//和双缓冲。
glutInitWindowSize(500500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow(“池版本1.0”);
//设置glut回调函数
glutDisplayFunc(显示::显示场景);
glutIdleFunc(显示::空闲);
GLUTEFUNC(显示::调整大小);
glutKeyboardFunc(显示::键盘输入);
//m_Timer.getSeconds();
glEnable(GLU深度试验);
gl点大小(5);
glEnable(GL_正常化);
glEnable(德国劳埃德大学照明);
glClearColor(0,0,0,1);
glEnable(GL_颜色_材料);
浮动白色[]={1.0f,1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0,GL_漫反射,白色);
glEnable(GL_LIGHT0);
设置位置(向量3f(0.0,0.3,0.0));
对于(int i=球\u红色\u开始;i<球\u红色\u开始;i++)
{
gl3f(1,0,0);
Redball[i]->SetPos(向量3f(i+128,球的半径,45));//我尝试了这个方法,但没有成功,错误是C2227
}
//开始glut主循环
glutMainLoop();
}
无效球移动()
{
//矢量3f位置(0.0,0.3,0.0);
/*设置位置(向量3f(0.0,0.3,0.0))*/
矢量3f新_速度(0.01,0,0);
Redball.SetVel(新速度);
矢量3F新位置;
向量3f旧位置;
Old_position=Redball.GetPos();
//新位置=旧位置+新速度;
新位置.SetX(旧位置.X()+新速度.X());
新位置.SetY(旧位置.Y()+新速度.Y());
新位置.SetZ(旧位置.Z()+新速度.Z());
设置位置(新位置);
}
void Display::displaysecene()
{
glClear(GL_颜色_缓冲区_位| GL_深度_缓冲区_位);//清除后缓冲区
glPushMatrix();
glLoadIdentity();
glNormal3f(0,1,0);
Vector3f didums=Redball.GetPos();
gluLookAt(eyeX,eyeY,eyeZ,//眼睛位置
0,0,0,//我在看什么
0.0,1.0,0);//向上方向
浮动位置[]={lightX,lightY,lightZ,1.0f};
glLightfv(GL_LIGHT0,GL_位置,位置);
牵引灯(0,位置);
/*这里是渲染代码*/
for(int i=BALL\u RED\u START;iDraw();//我尝试了这个方法,但没有成功,错误是C2227
}
绘图台();
drawTableLegFrontLeft();
drawTableLegFrontRight();
drawTableLegBackLeft();
DrawtableBackRight();
拉丝垫();
//画圈();
//drawHCircle();
Table(-2,-4.5,2,4.5);//以三角形绘制桌面trip-4.5,0.5,-0.5,9.5
glPopMatrix();
glutSwapBuffers();//交换前后缓冲区
}
无效显示::调整大小(整数w,整数h)
{
/*调整窗口大小时调用Resize*/
glMatrixMode(GL_投影);//将矩阵模式设置为profection
//这说明如何将3d场景“挤压”到2d屏幕上
glLoadIdentity();
glViewport(0,0,w,h);//设置要使用的窗口部分。
透视图(45,//视野
(浮动)w/(浮动)h,//窗的比率
1,//前剪裁平面
1000//back-clipping平面
);//设置3d场景中要绘制的区域
glMatrixMode(GL_MODELVIEW);//将矩阵模式设置为模型视图
//矩阵指定如何查看三维场景
/*glLoadIdentity();
gluLookAt(-3.5,2,eyeZ,//眼睛位置
1,1,0,//我在看什么
0.0,1.0,0);//向上方向*/
}
void Display::Idle()
{
/*当没有其他事情发生时,调用idle。
*模拟应该在这里进行,然后
*应该调用display方法
*/
球运动();
再发现();
}

我看到您已将
Ball
声明为
Ball Redball;
这将在堆栈上创建一个球

然后您尝试将其视为具有
Redball[i]->SetPos(…)
Redball[i]->Draw()
Ball
的集合。似乎您正在尝试使用其中的8个

您要做的是创建一个最大大小为8的
Ball
s数组(根据
Ball\u RED\u END

Ball RedBall[8];
for( //some conditions here )
{
    RedBall[i].Draw();
}
作为您的声明和用法


请记住,无论何时使用
Redball.SetPos(…)
将不再有效。

谢谢!这已经被红色的错误和它的编译好了!现在唯一的事情是我只能看到一个,但那是因为我相信我在同一个地方画了它们?没关系!我已经做到了,我在循环中放置了一个GLTRANSTAREF来分隔球,谢谢你的帮助!