1.实验一(改进后使用glTranslate函数)


#include "pch.h"
#include <GL/glut.h>
#include <math.h>
#define Rwidth 0.15
#define Rheight 0.15
#define n 360
#define DEG_TO_RAD 0.017453
GLfloat theta = 0.0;
GLfloat th = 0.0;
const GLfloat radius = 0.6;
const GLfloat Pi = 3.1415926536;
const GLfloat ac = 0.1;
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT); //clear the window
glBegin(GL_LINE_LOOP); //draw a circle
for (GLint i = 0; i < n; i++) {
glColor3f(0.0, 0.0, 0.0);
glVertex2f(radius *cos(2 * Pi / n * i), radius * sin(2 * Pi / n * i));
}
glEnd();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-radius * cos(th * DEG_TO_RAD), - radius * sin(th * DEG_TO_RAD), 0.0); //the rectanguler's translation
glBegin(GL_POLYGON); // Rotation
glVertex2f(-Rwidth / 2, -Rheight / 2 );
glVertex2f(Rwidth / 2 , -Rheight / 2 );
glVertex2f(Rwidth / 2 , Rheight / 2);
glVertex2f(-Rwidth / 2 , Rheight / 2);
glEnd();
glFlush();
}
void rotate()
{
th += 0.1;
if (th > 360.0)
th -= 360.0;
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 1000);
glutCreateWindow("rect");
glutDisplayFunc(displayFcn);
init();
glutIdleFunc(rotate);
glutMainLoop();
return 0;
}
2.实验二(只使用了glRotare)


#include "pch.h"
#include <GL/glut.h>
#include <math.h>
#define Rwidth 0.2
#define Rheight 0.15
#define n 360
#define DEG_TO_RAD 0.017453
GLfloat theta = 0.0;
GLfloat th = 0.0;
const GLfloat radius = 0.6;
const GLfloat Pi = 3.1415926536;
const GLfloat ac = 0.1;
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT); //clear the window
glBegin(GL_LINE_LOOP); //draw a circle
for (GLint i = 0; i < n; i++) {
glColor3f(0.0, 0.0, 0.0);
glVertex2f(radius *cos(2 * Pi / n * i), radius * sin(2 * Pi / n * i));
}
glEnd();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-radius * cos(th * DEG_TO_RAD), -radius * sin(th * DEG_TO_RAD), 0.0);
glRotatef(theta *4, 0.0, 0.0, 1); //4:1
glBegin(GL_POLYGON);
glVertex2f(-Rwidth / 2, -Rheight / 2);
glVertex2f(Rwidth / 2, -Rheight / 2);
glVertex2f(Rwidth / 2, Rheight / 2);
glVertex2f(-Rwidth / 2, Rheight / 2);
glEnd();
glFlush();
}
void rotate()
{
theta += 0.18;
if (theta > 360.0)
theta -= 360.0;
th += 0.18;
if (th > 360.0)
th -= 360.0;
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 1000);
glutCreateWindow("rect");
glutDisplayFunc(displayFcn);
init();
glutIdleFunc(rotate);
glutMainLoop();
return 0;
}
注:注意在实验二中,glTranslatef()和glRotatef()是理解这个实验的关键。
网友评论