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

Want to move one 2d object and not the other

本文关键字:对象 2d 另一个 一个 移动      更新时间:2023-10-16

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

模拟有限状态机的现代图形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,考虑使用顶点缓冲对象(VBOs),而不是弃用的Begin/glVertex2d/End

试试这个:

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