我只是想做水平移動的輪子。有一個代碼,但它不起作用 - 車輪只是作爲一個圓圈移動,留下痕跡。我試圖glTranslatef(0.0f, moveY, 0.0f);
但它不工作。OpenGL移動對象不起作用
#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();
}
你可以添加屏幕截圖結果嗎?什麼不行?我認爲你需要在glTranslatef之前調用glRotatef。 – Unick
@不用你太棒了! – phen0men
你不客氣。 – Unick