【问题标题】:Displaying and moving a square显示和移动正方形
【发布时间】:2018-12-12 19:05:23
【问题描述】:
#include <stdio.h> // this library is for standard input and output
#include "glut.h"// this library is for glut the OpenGL Utility Toolkit
#include <math.h>

float squareX = 0.0f;
float squareY = -0.3f;
float squareZ = 0.0f;

static int flag = 1;

void drawShape(void) {
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0); // set colour to red
    glVertex2f(162, 50);
    glVertex2f(162, 10);
    glVertex2f(220, 10);
    glVertex2f(220, 50);
    glVertex2f(162, 50);
    glTranslatef(squareX, squareY, squareZ); // moving it toward the screen a bit on creation
    glEnd();
}

void initRendering() {
    glEnable(GL_DEPTH_TEST);
}

// called when the window is resized
void handleResize(int w, int h) {
    // tell OpenGL how to convert from coordinates to pixel values
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION); // switch to setting the camera perspective
    // set the camera perspective
    glLoadIdentity(); // reset the camera
    gluPerspective(45.0,                  // the camera angle
    (double)w / (double)h, // the width-to-height ratio
    1.0,                   // the near z clipping coordinate
    200.0);                // the far z clipping coordinate
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    drawShape();
    glutSwapBuffers();
}

// float _angle = 30.0f;
void update(int value) {
    if (flag) {
        squareY += 0.001f;
        if (squareY > 0.3) {
            flag = 0;
        }
    }
    if (!flag) {
        squareY -= 0.001f;
        if (squareY < -0.3) {
            flag = 1;
        }
    }
    glutPostRedisplay(); // tell GLUT that the display has changed
    // tell GLUT to call update again in 25 milliseconds
    glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Moving Square");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0);
    glutMainLoop();
    return(0);
}

我在代码中添加了 cmets,以便您理解。我正在尝试创建一个移动到屏幕顶部的正方形,但我无法显示该正方形。由于某种原因,当我运行代码时它没有出现。当我删除运动代码时,我只能显示正方形。如何显示正方形并使其上升?

【问题讨论】:

  • 为什么要混合gluOrtho2DgluPerspective?仅使用gluOrtho2D 并尝试使用float squareZ = 0.0f;
  • ???我想,你应该删除gluPerspective
  • 现代 OpenGL 怎么样 learnopengl.com 我会在顶点着色器上执行此操作,顺便说一下 glut 多年来没有更新,为什么不高兴 GLFW 呢?
  • glTranslatef 不允许在 glBegin/glEnd 序列中
  • 我应该把glTranslatef放在哪里?

标签: c++ opengl glut


【解决方案1】:

请注意,由glBegin/glEnd 序列和固定函数管道矩阵堆栈绘制的绘图自几十年以来已被弃用。 阅读Fixed Function Pipeline 并查看Vertex SpecificationShader,了解最先进的渲染方式。


无论如何,glTranslatef 不允许出现在 glBegin/glEnd 序列中。在glBegin之前做:

void drawShape(void) {
    glTranslatef(squareX, squareY, squareZ); 
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(162, 50);
    glVertex2f(162, 10);
    glVertex2f(220, 10);
    glVertex2f(220, 50);
    glVertex2f(162, 50);
    glEnd();
}

如果要在窗口坐标中绘制四边形,则必须使用正交投影(glOrtho):

void handleResize(int w, int h) {

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    /*
    gluPerspective(45.0,   // the camera angle
    (double)w / (double)h, // the width-to-height ratio
    1.0,                   // the near z clipping coordinate
    200.0);                // the far z clipping coordinate
    */

    glOrtho( 0.0f, (float)w, 0.0f,(float)h, -1.0f, 1.0f );
}  

而且你必须改变翻译量:

void update(int value) {
    if (flag) {
        squareY += 1.0f;
        if (squareY > 350.0) {
            flag = 0;
        }
    }
    if (!flag) {
        squareY -= 1.0f;
        if (squareY < 0.0) {
            flag = 1;
        }
    }
    glutPostRedisplay(); 
    glutTimerFunc(25, update, 0);
}

预览:

【讨论】:

  • 我的方块在我运行代码时仍然没有出现。我在glBegin(GL_POLYGON); 上使用的值有什么问题吗?
  • @Muddy 这对我来说很好。我使用了您问题的代码,并用我的答案之一交换了函数drawShapehandleResizeupdate
猜你喜欢
  • 2018-01-04
  • 1970-01-01
  • 2014-04-30
  • 2018-01-19
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多