OpenGL在动画中放大/缩小六边形
我试图在旋转六边形上创建缩放效果。我通过改变窗口来实现这一点。一旦它“放大”,它应该“缩小”,然后不断重复。我已经很好地放大了,从代码的外观来看,它也应该缩小,但一旦它放大,就不会再画其他东西了。我已经调试了我的代码,可以看出变量确实在这一行递增:OpenGL在动画中放大/缩小六边形,opengl,animation,zooming,frame,Opengl,Animation,Zooming,Frame,我试图在旋转六边形上创建缩放效果。我通过改变窗口来实现这一点。一旦它“放大”,它应该“缩小”,然后不断重复。我已经很好地放大了,从代码的外观来看,它也应该缩小,但一旦它放大,就不会再画其他东西了。我已经调试了我的代码,可以看出变量确实在这一行递增: gluOrtho2D(cx - w, cx + w, cy -h, cy +h); 但我仍然看不到我的六边形“缩小”。任何帮助都将不胜感激。我很确定我忘记了一些简单的事情。但它一直在逃避我。我的代码如下: #include <cstdlib&
gluOrtho2D(cx - w, cx + w, cy -h, cy +h);
但我仍然看不到我的六边形“缩小”。任何帮助都将不胜感激。我很确定我忘记了一些简单的事情。但它一直在逃避我。我的代码如下:
#include <cstdlib>
#include <GL/glut.h>
#include <cmath>
#define PI 3.14159265
#define ZOOM_IN 1
#define ZOOM_OUT -1
using namespace std;
const int screenWidth = 500;
const int screenHeight = 500;
float cx = 0.0, cy = 0.0; //center of viewport (cx, cy)
float h=1.2, w = 1.2; //window size
int NumFrames = 10; //frames
int frame = 0;
int direction = ZOOM_IN;
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myinit() {
glClearColor (1.0, 1.0, 1.0, 1.0); //set the background color to white
glColor3f (0.0, 0.0, 0.0); //set the foreground color to black
glPointSize (3.0); //set the point size to 3 X 3 pixels
glViewport (0.0, 0.0, 500.0, 500.0); //set the viewport to be the entire window
//set up a world window to screen transformation
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
// glMatrixMode (GL_MODELVIEW);
}
//<<<<<<<<<<<<<<<<<<<<<<< hexswirl >>>>>>>>>>>>>>>>>>>>
void hexswirl() {
double angle; //the angle of rotation
double angleInc = 2*PI/6.0; //the angle increment
double inc = 5.0/50; //the radius increment
double radius = 5.0/50.0; //the radius to be used
//clear the background
glClear (GL_COLOR_BUFFER_BIT);
//draw the hexagon swirl
for (int j = 0; j <= 50; j++) {
//the angle of rotation depends on which hexagon is
//being drawn.
angle = j* (PI/180.0);
//draw one hexagon
glBegin (GL_LINE_STRIP);
for (int k=0; k <= 6; k++) {
angle += angleInc;
glVertex2d(radius * cos(angle), radius *sin(angle));
}
glEnd();
//determine the radius of the next hexagon
radius += inc;
}
//swap buffers for a smooth change from one
//frame to another
glutSwapBuffers();
glutPostRedisplay();
glFlush();
}
//<<<<<<<<<<<<<<<<<<<<<<< viewZoom >>>>>>>>>>>>>>>>>>>>
void viewZoom(int i) {
if(direction == ZOOM_IN) {
//change the width and height of the window each time
w *= 0.9;
h *= 0.9;
}
if(direction == ZOOM_OUT) {
w /= 0.9;
h /= 0.9;
}
if(i%10 == 0) {
direction = -direction;
}
//change the window and draw the hexagon swirl
gluOrtho2D (cx - w, cx + w, cy - h, cy + h);
hexswirl();
glutPostRedisplay();
glutTimerFunc(200, viewZoom,i+1);
}
//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,100);
glutCreateWindow("hexanim");
glutDisplayFunc(hexswirl);
viewZoom(1);
myinit();
glutMainLoop();
return 1;
}
#包括
#包括
#包括
#定义PI 3.14159265
#在1中定义缩放
#定义缩小-1
使用名称空间std;
常数int屏幕宽度=500;
常数int屏幕高度=500;
浮动cx=0.0,cy=0.0//视口中心(cx、cy)
浮子h=1.2,w=1.2//窗口大小
int NumFrames=10//框架
int帧=0;
int方向=放大;
// 我想出了解决问题的办法。我仍然不知道为什么我的窗口在“放大”后没有重新绘制,但我决定通过更改视口来实现它。我最终放弃了:
gluOrtho2D (cx - w, cx + w, cy - h, cy + h);
为了
(并进行了与之相关的所有相应更改)。我找到了解决问题的方法。我仍然不知道为什么我的窗口在“放大”后没有重新绘制,但我决定通过更改视口来实现它。我最终放弃了:
gluOrtho2D (cx - w, cx + w, cy - h, cy + h);
为了
(并进行了与之相关的所有相应更改)