在Open GL Glut中绘制对象数组

如何解决在Open GL Glut中绘制对象数组

| 我一直在尝试为自己正在制作的游戏画多个球,我试图使其正常工作,但是我的init类和display方法类存在问题,我在错误所在的地方对此进行了评论。 Ball.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();
};
球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);
    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,1);
        glEnable(GL_COLOR_MATERIAL);


float white[] = { 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);
  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.0);
    /*Redball.SetPos(Vector3f(0.0,0.0));*/
    Vector3f New_velocity(0.01,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,// 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);
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,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,// eye position
            1,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();
}
    

解决方法

我看到您已经将
Ball
声明为
Ball Redball;
,这将在堆栈上创建一个Ball。 然后,您尝试将其视为具有
Redball[i]->SetPos(...)
Redball[i]->Draw()
Ball
的集合。您似乎正在尝试使用其中的8种。 您要做的是创建一个3个数组,最大大小为8(根据9个数组)。为简单起见,您可以
Ball RedBall[8];
for( //some conditions here )
{
    RedBall[i].Draw();
}
作为您的声明和用法。 请记住,您每次使用
Redball.SetPos(...)
都将不再有效。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-