繁体   English   中英

OpenGL-沿随机方向移动的对象

[英]OpenGL - Objects moving in random direction

我正在使用OpenGL和C制作2D电子游戏,但遇到了一些麻烦。 我正在尝试制作一定数量的对象(例如100),以随机的速度和方向漫游,但是它不起作用,我敢肯定有更好的方法可以做到这一点,但我无法弄清楚。

到目前为止,这是我的课程,我想被绘制100次:

#include "stdafx.h"
#include <math.h>
#ifndef _THING_H
#define _THING_H

class thingClase {
public:
    float ypos, xpos, zpos;
    float count;
    thingClase() {
        xpos = rand()%80;
        ypos = rand()%80;
        zpos = 1;
        count = 1;
    }
    };

thingClase thing[100];
int thingNum = 0;
int thingTotal = 100;
int width = 1;
bool alive;

#endif  /* _THING_H */

我的主要功能包括:

void drawThing() {
for (thingNum = 0; thingNum < thingTotal; thingNum++) {
    glPushMatrix();
    glTranslated(thing[thingNum].xpos, thing[thingNum].ypos, thing[thingNum].zpos);
    glPopMatrix();
}
}

因此,我看到您正在设置一个转换以绘制一些东西:

void drawThing() {

// EDIT:
//
// Set the matrix mode!
glMatrixMode(GL_MODELVIEW);

for (thingNum = 0; thingNum < thingTotal; thingNum++) {

    // Yes, good, matrix is saved!
    glPushMatrix();

    // Yes, translate to the thing's position
    glTranslated(thing[thingNum].xpos, thing[thingNum].ypos, thing[thingNum].zpos);

    // Draw the thing here!
    //
    // You know, like call a display list or something.

    // Restore the matrix, also good!
    glPopMatrix();
}
}

您需要画些东西。 因此,如果您使用的是易于使用的旧OpenGL,请创建显示列表或使用glBeginglEnd

然后,在您感到舒适之后,制作并使用VBO和着色器,这就是我们今天使用OpenGL的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM