【问题标题】:Want to move one 2d object and not the other想要移动一个二维对象而不是另一个
【发布时间】:2013-06-15 12:41:30
【问题描述】:

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

}

【问题讨论】:

    标签: c++ eclipse opengl graphics glut


    【解决方案1】:

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

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

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

    顺便说一句,考虑使用顶点缓冲区对象 (VBO),而不是已弃用的 Begin/glVertex2d/End

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 2018-02-04
        • 1970-01-01
        相关资源
        最近更新 更多