在这里我每个周期在不同的位置显示一个多维数据集,但是我希望它带有过渡和动画生成,例如,我希望将其从-1转换为+1作为循环(对多维数据集进行动画处理)>
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdio.h>
#include <stdlib.h>
float angle_Rotation = 0;
float angle_Rotation2 = 0;
float xx=0.5;
float yy=0.5;
float zz=0.5;
int dx=0;
int dy=1;
int dz=0;
float tabPos[3] = {-4.0,0.0,4.0};
int cpt=0;
GLint rings = 50;
GLint side =50;
GLdouble inner = 0.5;
GLdouble outter = 1;
void init(){
/* choisir la couleur d'effacement */
glClearColor(0.0,0.0,0.0,0.0);
/* Projection et positionement de la camera*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,0.1,100);
gluLookAt(0,10,8,0,0,0,0,1,0);
}
static void Reshape(int width, int height){
glViewport(0,0,width,height);
}
void whatINeed(){
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glRotatef(angle_Rotation, 0.0, 1.0, 0);
glRotatef(angle_Rotation2, 1.0, 0.0, 0);
}
static void cercle(float r){
float i;
float PI=3.14;
glBegin(GL_POLYGON);
for(i=0;i<2*PI;i+=PI/48){
glVertex3f(cos(i)*r,sin(i)*r,0.0);
glVertex3f(cos(i)*r,sin(i)*r,6.0);
}
glEnd();
//glFlush();
}
static void KeyPressed(unsigned char touche, int x, int y){
switch (touche)
{
case 'q': ///rotaion � gauche
angle_Rotation = angle_Rotation + 2;
break;
case 'd': ///rotaion � droite
angle_Rotation = angle_Rotation - 2;
break;
case 'z': ///rotaion � gauche
angle_Rotation2 = angle_Rotation2 + 2;
break;
case 's': ///rotaion � droite
angle_Rotation2 = angle_Rotation2 - 2;
break;
}
glutPostRedisplay();
}
void light(void){
glEnable(GL_LIGHTING);
//glEnable(GL_COLOR_MATERIAL);
GLfloat light_couleur1[] = {0.7,0.5,0.6,0.7};
GLfloat light_couleur2[] = {0.7,0.7,0.5,0.7};
//GLfloat light_couleur3[] = {0.7,0.3,1.0,1.0};
//GLfloat light_position[] = {0.0,1.0,0.5,0.5};
GLfloat light_position2[] = {0.0,1.0,0.5,0.0};
GLfloat obj_shine[] = {50.0};
glLightfv(GL_LIGHT1,GL_POSITION,light_position2);
glLightfv(GL_LIGHT1,GL_AMBIENT,light_couleur1);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light_couleur2);
//glLightfv(GL_LIGHT4,GL_SPECULAR,light_couleur2);
//glMaterialfv(GL_FRONT,GL_DIFFUSE,light_couleur2);
glMaterialfv(GL_FRONT,GL_SPECULAR,light_couleur2);
//glMaterialfv(GL_FRONT,GL_SHININESS,obj_shine);
glEnable(GL_LIGHT1);
}
void table(void){
glColor3f(0.6,0.0,0.6);
glPushMatrix(); // Cube
glTranslatef(0,0,0.0);
glScalef(4,1,4); // Scale Sur X et Z (Réctangle)
glutSolidCube(3);
glPopMatrix();
glColor3f(0.3,0.0,0.8);
glPushMatrix(); // Sphere
glTranslatef(-4,1.2,-4); // Ligne Haut Gauche
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(0,1.2,-4); // Ligne haut Milieu
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(4,1.2,-4); // Ligne haut droite
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(-4,1.2,0); // Ligne Milieu droite
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(0,1.2,0); // Ligne Milieu Milieu
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(4,1.2,0); // Ligne Milieu Gauche
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(-4,1.2,4); // Ligne Bas droite
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(0,1.2,4); // Ligne Bas milieu
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(4,1.2,4); // Ligne Bas gauche
glRotatef(90,1,0,0);
glutSolidTorus(inner,outter,side,rings);
glPopMatrix();
}
void taupe(void){
glPushMatrix();
glTranslatef(tabPos[dx],dy,tabPos[dz]); // Ligne Milieu droite
glColor3f(0.8,0.3,0.5);
glutSolidCube(2);
glPopMatrix();
}
在此函数生成中,我必须更新dy 我必须以0.05为步长从-1变成1 问题是,当我添加代码时,我失去了主要的动画效果
void spawn(void){
if(cpt%500000==0){
if(cpt>500000){
cpt=0;
}
dx = rand() % 3; //generates a ra
0至2之间的随机数 dz = rand()%3; //产生介于0到3之间的随机数
glutPostRedisplay();
}
cpt++;
}
void display(){
whatINeed();
table();
taupe();
glutSwapBuffers();
}
int main(){
// Considérer la profondeur
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
/* Initialisationd e glut et création de la fenetre */
glutInitWindowSize(720,720);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL TP");
/*Autre Initialisation */
init();
/*enregistrement des fonction rappel*/
glutDisplayFunc(display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(KeyPressed);
在这里,我将立方体生成为动画
glutIdleFunc(spawn);
glEnable(GL_DEPTH_TEST);
//light();
/* entré dans mla boucle principal de glut*/
glutMainLoop();
return 0;
}
答案 0 :(得分:3)
要制作动画,必须不断更新显示。删除所有对glutPostRedisplay
的呼叫,并在warning: operation on ‘j’ may be undefined [-Wsequence-point]
scanf("%f %f", &p[i][j], &p[i][j++]);
~^~
中进行单个呼叫。例如:
function async populateInventories(custID){
this.inventories = await this.inventoryService.getCustomerInventories(custID)
//do some stuff with Inventories
// another await
}
要在2个位置之间进行动画处理,您必须知道上一个(function async populateInventories(custID){
let invetroySubscription$;
if(invetroySubscription$!==undefined){
invetroySubscription$.unsubscribe();
}
invetroySubscription$=this.inventoryService.getCustomerInventories(custID).subscribe((data:any)=>{
this.inventories = data.response;
//do some stuff with Inventories
// another subscription here -- I was told this way of doing is wrong
})
}
)和新位置(display
):
void display(){
whatINeed();
table();
taupe();
glutSwapBuffers();
glutPostRedisplay(); // <---
}
定义动画的时间间隔。使用计时器(glutTimerFunc
)代替glutIdleFunc
来开始新的动画:
pdx
dx
在int pdx=0;
int dx=0;
中将当前位置存储到int interval = 1000; // 1 second
void spawntimer(int value);
并获得一个随机位置。将当前经过的时间存储到int main()
{
// [...]
// glutIdleFunc(spawn); <--- DELETE
glutTimerFunc(interval, spawntimer, 0); // <--- ADD
// [...]
。这说明了动画的开始时间,可以通过glutGet(GLUT_ELAPSED_TIME)
获得。重新启动计时器:
spawntimer
在dx
中,根据时间范围[0.0,1.0]计算对象的相对位置,并在start_time
和int start_time = 0;
void spawntimer( int value )
{
pdx = dx;
dx = rand() % 3;
start_time = glutGet(GLUT_ELAPSED_TIME);
glutTimerFunc(interval, spawntimer, 0);
}
之间进行插值。例如:
taupe