OpenGL 移动对象不起作用

OpenGL moving object doesn't work

本文关键字:不起作用 对象 移动 OpenGL      更新时间:2023-10-16

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

#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
#include <GLGL.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();
}

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

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

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

(1) 包括自定义头,并期望社区猜测它是关于什么的。我把它评论掉了。

//#include "stdafx.h" 

(2) 写错转换为函数-应该是:

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

(3) 代码中没有glutDisplayFunc。你应该注册这样的东西:

glutDisplayFunc(main_display_function);

在注册CCD_ 3之前。然后,在您的代码中,您应该以以下方式对其进行定义:

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、绘制梁或轮辐的顺序),您将轻松地理解并自己调试。请看一下这个SO帖子。

PS。你使用C++编译器来构建这个程序并不能使它成为C++代码。您的程序中没有一行特定于C++的代码。此外,OpenGL和GLUT都是纯C API。