OpenGL 移动对象不起作用
OpenGL moving object doesn't work
我只想做水平移动轮子。有一个代码,但它不起作用——轮子只是像一个圆圈一样移动,留下痕迹。我试过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。
相关文章:
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- 即使我正在.cpp文件中实例化一个伪对象,.cpp文件内模板函数的定义也不起作用
- C++ 对象数组字符串输入在控制台上不起作用
- 自定义类型转换运算符在转发引用上调用时不起作用(当对象按值传递时有效)
- 模板参数类型推导在函数对象中不起作用
- 对列表迭代器指向的对象不起作用的引用
- 更新对象中的值不起作用
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- GCC:当层次结构中存在虚拟继承时,C++11 内联对象初始化(使用 "this")不起作用
- 抽象类型的对象 - 覆盖不起作用吗?
- 面向对象 成本计算C++不起作用
- 对象的排序向量不起作用
- 操作员=超载不起作用,除非对象已经初始化
- OpenGL 移动对象不起作用
- 使用纹理绘制多个对象不起作用
- 创建不可变的 CFArray 对象 - Apple 示例不起作用
- 矢量push_back在类对象中不起作用
- 线程对象的 WaitForSingleObject 在 DLL 卸载中不起作用
- 为什么类构造函数在通过复制初始化对象时不起作用
- 传递的临时 concreate 派生对象,其中需要对抽象基的引用不起作用?