OpenGL移动对象不起作用

时间:2016-06-09 08:01:11

标签: c++ opengl rotation

我只想做水平移动的轮子。有一个代码,但它不起作用 - 轮子只是一个圆圈移动,留下痕迹。我试过glTranslatef(0.0f, moveY, 0.0f);,但它也不起作用。

#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
#include <GL\GL.h>

#define window_width  1080  
#define window_height 720 

GLfloat angle; // wheel's rotator
GLfloat moveX; // wheel's mover by X

void DrawCircle(float cx, float cy, float r, int num_segments) {
    glLoadIdentity();

    glTranslatef(0, 0, -10); // set place where wheel starts to draw

    glRotatef(angle, 0.0f, 0.0f, -0.1f); // rotate our wheel. it works correctly I think

    glTranslatef(moveX, 0.0f, 0.0f); // but it makes me wonder. moving circle by X doesn't work 

    // part of a circle
    glBegin(GL_TRIANGLE_FAN);
    glColor3f(255, 255, 255);
    for (int ii = 0; ii < num_segments; ii++) {
        double twicePi = 2.0 * 3.142;
        float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);
        float x = r * cosf(theta);
        float y = r * sinf(theta);
        glVertex2f(x + cx, y + cy);
    }

    glEnd();

    // second part of a circle
    int i, x, y;
    double radius = 1.20;    

    glColor3ub(0.0, 0.0, 0.0);
    double twicePi = 2.0 * 3.142;
    x = 0, y = 0;
    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x, y); 
    for (i = 0; i <= 20; i++) {
        glVertex2f(
            (x + (radius * cos(i * twicePi / 20))),
            (y + (radius * sin(i * twicePi / 20)))
        );
    }
    glEnd();
    // there's an end of drawning of a circle

    // this is where wheel starts to rotate and move
    angle += 0.02f;
    moveX += 0.00005f;



    // there are spokes of a wheel
    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, -1.2f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 1.2f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.2f, 0.0, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, 0.0, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.f, 1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.f, 1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.f, -1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.f, -1.f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.2f, 0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, -0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, -0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-1.2f, 0.5f, 0.0);
    glEnd();

    glBegin(GL_LINES);
        glColor3f(255, 255, 255);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.2f, -0.5f, 0.0);
    glEnd();

}


void main_loop_function() {
    DrawCircle(0.0, 0.0, 1.45, 200);
    glutSwapBuffers();
}

void GL_Setup(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(45, (float) width / height, 0.1, 100);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(window_width, window_height);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("trata");
    glutIdleFunc(main_loop_function);
    GL_Setup(window_width, window_height);
    glutMainLoop();
}

1 个答案:

答案 0 :(得分:2)

这只是一个快速修复,使您的代码工作,所以你可以进一步开发和调试它。我在您的代码中看到了许多关键问题:

(0)将POSIX路径与Windows路径混合:

#include <GL/freeglut.h>
#include <GL\GL.h>

(1)包括自定义标题,并期望社区猜测它是什么。我评论了它。

//#include "stdafx.h" 

(2)将强制转换误写为函数 - 它应该是:

    float theta = 2.0f * 3.1415926f * (float)ii / (float)num_segments;

(3)代码中没有glutDisplayFunc。您应该注册以下内容:

glutDisplayFunc(main_display_function);

在注册glutIdleFunc()之前。然后,在您的代码中,您应该通过以下方式进行一些定义:

void main_display_function() {
    DrawCircle(0.0, 0.0, 1.45, 200);
    glutSwapBuffers();    
}

(4)即使在该功能中,您也不会在重绘之前清除“窗口内容”:

void DrawCircle(float cx, float cy, float r, int num_segments) {
    glClearColor (0., 0., 0., 1);
    glClear (GL_COLOR_BUFFER_BIT);
    {…   …   … } //the rest of your code
}

(5)传递错误数据类型的颜色值

    glColor3f(255, 255, 255);  //unsigned byte passed, float expected!

    glColor3ub(0.0, 0.0, 0.0); //float passed, unsigned byte expected!

还有一些问题直接解决了您的问题 - 一旦您修复程序正常工作(调用顺序),您将轻松地理解并调试它们自己glRotatef和第二个glTranslatef,正确计算movex,绘制横梁或辐条)。请同时查看this SO post

PS。使用C ++编译器构建此程序不会使其成为C ++代码。程序中没有一行特定于C ++的代码。此外,OpenGL和GLUT都是普通的C API。