OpenGL - 随机方向移动的对象

时间:2014-11-12 00:24:53

标签: c++ visual-studio opengl

我使用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();
}
}

1 个答案:

答案 0 :(得分:0)

所以,我看到你正在设置一个转换来绘制一些东西:

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方式。