C++ 要移动一个二维对象而不是另一个

C++ 要移动一个二维对象而不是另一个,c++,eclipse,opengl,graphics,glut,C++,Eclipse,Opengl,Graphics,Glut,我必须在openGL中制作一个保龄球游戏。这是我目前掌握的代码。它所做的是画一个球,并在按下箭头键时相应地移动 到目前为止,我有球在移动,这很好我想做的是我创建的另一点,该点不应移动。因为,当球到达那个点时,它应该掉下来,或者我要做的事情,障碍物掉下来了 代码是用EclipseIDE编写的 #include <stdio.h> #include <GL/glut.h> #include <math.h> #include <stdio.h>

我必须在openGL中制作一个保龄球游戏。这是我目前掌握的代码。它所做的是画一个球,并在按下箭头键时相应地移动

到目前为止,我有球在移动,这很好我想做的是我创建的另一点,该点不应移动。因为,当球到达那个点时,它应该掉下来,或者我要做的事情,障碍物掉下来了

代码是用EclipseIDE编写的

#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>       /* printf, scanf, puts, NULL */

float posX = 0, posY = -0.1, posZ = 0;
GLfloat rotation = 90.0;
double x, y, angle;
#define PI 3.1415926535898
GLint circle_points = 50;

void reshape(int width, int heigth) {

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //clip the windows so its shortest side is 2.0
    if (width < heigth) {
        glOrtho(-2.0, 2.0, -2.0 * (GLfloat) heigth / (GLfloat) width,
                    2.0 * (GLfloat) heigth / (GLfloat) width, 2.0, 2.0);
    } else {
        glOrtho(-2.0, 2.0, -2.0 * (GLfloat) width / (GLfloat) heigth,
                2.0 * (GLfloat) width / (GLfloat) heigth, 2.0, 2.0);
    }
    // set viewport to use the entire new window
    glViewport(0, 0, width, heigth);
}

void circ() {

    glColor3f(0.0, 0.0, 1.0);
     glPointSize(11.0);
     glBegin(GL_POINTS);
     glVertex3f(0.1, 0.1, 0.0);
     glEnd();

    glBegin(GL_TRIANGLE_FAN);
    for (int i = 0; i <= 300; i++) {
        angle = 2 * PI * i / 300;
        x = cos(angle) / 20;
        y = sin(angle) / 20;
        glVertex2d(x, y);
    }
    glEnd();

}

void display() {
    //Clear Window
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(posX, posY, posZ);
    circ();
    glPopMatrix();
    glFlush();
}

void init() {
    // set clear color to black
    glClearColor(1.0, 1.0, 1.0, 0.0);

    // set fill color to white
    glColor3f(1.0, 1.0, 1.0);

    //This is the default view and these statements could be removed
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

}
float move_unit = 0.02f;
void keyboardown(int key, int x, int y) {
    switch (key) {
    case GLUT_KEY_RIGHT:
        posX += move_unit;

        break;

    case GLUT_KEY_LEFT:
        posX -= move_unit;

        break;

    case GLUT_KEY_UP:
        posY += move_unit;

        break;

    case GLUT_KEY_DOWN:
        posY -= move_unit;

        break;

    default:
        break;
    }
    glutPostRedisplay();

}

int main(int argc, char** argv) {

    //initialize mode and open a windows in upper left corner of screen
    //Windows tittle is name of program

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Practice 1");
    glutDisplayFunc(display);
    init();
    glutSpecialFunc(keyboardown);
    glutMainLoop();

}
#包括
#包括
#包括
#include/*printf、scanf、put、NULL*/
浮点posX=0,posY=-0.1,posZ=0;
GLfloat旋转=90.0;
双x,y,角;
#定义PI 3.1415926535898
闪烁圆_点=50;
空洞重塑(内部宽度、内部高度){
glMatrixMode(GL_投影);
glLoadIdentity();
//剪裁窗口,使其最短边为2.0
如果(宽度<高度){
格洛托(-2.0,2.0,-2.0*(GLfloat)高度/(GLfloat)宽度,
2.0*(GLfloat)高度/(GLfloat)宽度,2.0,2.0);
}否则{
格洛托(-2.0,2.0,-2.0*(GLfloat)宽度/(GLfloat)高度,
2.0*(GLfloat)宽度/(GLfloat)高度,2.0,2.0);
}
//将“视口”设置为使用整个新窗口
glViewport(0,0,宽度,高度);
}
无效保监会(){
GL3F(0.0,0.0,1.0);
glPointSize(11.0);
glBegin(总分);
glVertex3f(0.1,0.1,0.0);
格伦德();
glBegin(GLU三角形风扇);
对于(int i=0;i请尝试以下方法:

#include <GL/glut.h>
#include <cmath>

float posX = 0, posY = -0.1, posZ = 0;
GLfloat rotation = 90.0;
double x, y, angle;
#define PI 3.1415926535898
GLint circle_points = 50;

void point()
{
    glColor3f(0.0, 0.0, 1.0);
    glPointSize(11.0);
    glBegin(GL_POINTS);
    glVertex3f(0.1, 0.1, 0.0);
    glEnd();
}

void circ()
{
    glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_TRIANGLE_FAN);
    for (int i = 0; i <= 300; i++)
    {
        angle = 2 * PI * i / 300;
        x = cos(angle) / 20;
        y = sin(angle) / 20;
        glVertex2d(x, y);
    }
    glEnd();
}

void display()
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    point();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(posX, posY, posZ);
    circ();
    glPopMatrix();

    glutSwapBuffers();
}

float move_unit = 0.02f;
void keyboardown(int key, int x, int y)
{
    switch (key) 
    {
    case GLUT_KEY_RIGHT:
        posX += move_unit;
        break;
    case GLUT_KEY_LEFT:
        posX -= move_unit;
        break;
    case GLUT_KEY_UP:
        posY += move_unit;
        break;
    case GLUT_KEY_DOWN:
        posY -= move_unit;
        break;
    default:
        break;
    }
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Practice 1");
    glutDisplayFunc(display);
    glutSpecialFunc(keyboardown);
    glutMainLoop();
}
#包括
#包括
浮点posX=0,posY=-0.1,posZ=0;
GLfloat旋转=90.0;
双x,y,角;
#定义PI 3.1415926535898
闪烁圆_点=50;
空位点()
{
GL3F(0.0,0.0,1.0);
glPointSize(11.0);
glBegin(总分);
glVertex3f(0.1,0.1,0.0);
格伦德();
}
无效保监会()
{
GL3F(0.0,0.0,1.0);
glBegin(GLU三角形风扇);

对于(int i=0;i模拟有限状态机的现代图形API。这意味着在绘制调用之前,必须完全配置(或保留默认)图形管道“机器”:

对于许多对象,您可以使用for循环包装所有内容:

for( each_object )
{
    SetStates(); // current object's vertex/index buffer, texture, matrices, etc.
    Begin();
    Draw();
    End();
}
效率不高。下一步的改进可能包括:截锥剔除、实例化、顶点缓冲区合并、纹理地图集、绘制调用排序等

BTW,考虑使用顶点缓冲对象(VBOS),而不是<代码>开始/ GLVTEX2D/Eng/EX> >

for( each_object )
{
    SetStates(); // current object's vertex/index buffer, texture, matrices, etc.
    Begin();
    Draw();
    End();
}