想要移动一个2d对象而不是另一个

时间:2013-06-19 05:27:02

标签: c++ eclipse opengl graphics glut

我必须在openGL中打保龄球。这是我到目前为止的代码。它的作用是它绘制一个球,并在按下箭头键时相应地移动。

到目前为止,我有那个球移动,这很好。 我想要做的是我创建的其他一点,不应该被移动 。因为,当那个球到达那个点时,它应该是掉落的,或者我会做出的东西会让障碍物掉落。

代码是用Eclipse IDE编写的。

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

}

2 个答案:

答案 0 :(得分:0)

试试这个:

#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();
}

答案 1 :(得分:0)

现代图形API模拟有限状态机。这意味着在Draw调用之前,您必须完全配置(或保留默认)图形管道“machine”:

SetStates(); // Configure pipeline state: set geometry, textures, matrices, etc.
Begin();
Draw(); // Render frame according to current pipeline configuration (state)
End(); // Swap screen buffers

如果有很多对象,你可以用for循环包装所有东西:

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

效率不高。下一步的改进可能包括:视锥体剔除,实例化,顶点缓冲合并,纹理图集,绘制调用排序等。

BTW,考虑使用顶点缓冲区对象(VBO),而不是不推荐使用的Begin/glVertex2d/End