c - 旋转5圈问题

标签 c opengl 3d rotation

Possible Duplicate:
how to rotate this openGl code…

下面是我的代码,它工作正常..我在其中画了 5 个环。

#include <GL/glut.h>
#include <math.h>

static void redraw(void);
#define PI 3.14159265 
#define EDGES 90
#define factor 10


void display (void) 
{
    glClearColor(1.0,1.0,1.0,0.0);
     glMatrixMode (GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();   


//ring1 
glPushMatrix();
glColor3f (0.0, 0.0 ,1.0);
glTranslatef(-30.0,10.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring2
glPushMatrix();
glColor3f (0.0, 0.0, 0.0);
glTranslatef(-8.0,10.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring3
glPushMatrix();
glColor3f (1.0, 0.0 ,0.0);
glTranslatef(14.0,10.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring4
glPushMatrix();
glColor3f (1.0, 1.0, 0.0);
glTranslatef(-19.0,-2.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

//ring5
glPushMatrix();
glColor3f (0.0, 1.0, 0.0);
glTranslatef(4.0,-2.0,-100.0);
redraw();
glutSwapBuffers();
glPopMatrix();

}

static void redraw(void)
{
for (int i = 0; i < EDGES; i++)
    { 
      glBegin(GL_LINE_LOOP); 
      glVertex2f(factor*cos((2*PI*i)/EDGES),factor*sin((2*PI*i)/EDGES)); 
      glVertex2f(factor*cos((2*PI*(i+1))/EDGES),factor*sin((2*PI*(i+1))/EDGES)); 
      glEnd();
     } 
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*3, 110*3);
glutCreateWindow("draw circle");
glPointSize(3); 
glShadeModel (GL_FLAT); 
glutDisplayFunc(display);
glMatrixMode(GL_PROJECTION);    
gluPerspective(45,1.0,10.0,200.0);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0; 
}

所以我想旋转所有这个圆圈..我该怎么办?

最佳答案

您应该在绘图函数中仅调用一次 glutSwapBuffers。

对于动画,您必须调用 glutPostRedisplay,以确保再次调用显示函数。

每个循环只需调用一次 glBegin 和 glEnd 调用。在 for 循环中,您只需通过调用 glVertex 来提供顶点。

为了简化调试,我安装了一个键盘处理程序,当按下转义键时,该处理程序会关闭窗口。

 // compile in linux: gcc ogl.c -lglut -lGLU

#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>

static void redraw(void);
#define PI 3.14159265 

enum{
  EDGES=90,
};

static void 
circle(float radius)
{
  int i;
  glBegin(GL_LINE_LOOP); 
  for (i = 0; i < EDGES; i++){ 
    glVertex2f(radius*cos((2*PI*i)/EDGES),
           radius*sin((2*PI*i)/EDGES)); 
    glVertex2f(radius*cos((2*PI*(i+1))/EDGES),
           radius*sin((2*PI*(i+1))/EDGES)); 
  } 
  glEnd();
}


int count=0;
void display (void) 
{
  float r=10;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();   

  glRotated(count,0,0,1);
  count++;
  if(count>360)
    count=0;

  glPushMatrix();
  // ring1
  glColor3f (0.0, 0.0 ,1.0);
  glTranslatef(-30.0,10.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  // ring2
  glColor3f (0.0, 0.0, 0.0);
  glTranslatef(-8.0,10.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  //ring3
  glColor3f (1.0, 0.0 ,0.0);
  glTranslatef(14.0,10.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  //ring4
  glColor3f (1.0, 1.0, 0.0);
  glTranslatef(-19.0,-2.0,-100.0);
  circle(r);
  glPopMatrix();

  glPushMatrix();
  //ring5
  glColor3f (0.0, 1.0, 0.0);
  glTranslatef(4.0,-2.0,-100.0);
  circle(r);
  glPopMatrix();

  glutSwapBuffers();
  glutPostRedisplay();
}

static void 
keyb(unsigned char key, int x, int y)
{
  switch (key) {
  case 27:  /* Escape key */
    exit(0);
  }
}


void
init()
{
  glPointSize(3); 
  glShadeModel (GL_FLAT); 
  glMatrixMode(GL_PROJECTION);    
  gluPerspective(45,1.0,10.0,200.0);
  glMatrixMode(GL_MODELVIEW);
  glClearColor(1.0,1.0,1.0,0.0);
  glLineWidth(2);
}

int
main(int argc, char **argv)
{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(100,100);
  glutInitWindowSize(110*3, 110*3);
  glutCreateWindow("draw circle");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyb);
  init();
  glutMainLoop();
  return 0; 
}

关于c - 旋转5圈问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6565630/

相关文章:

c++ - Linux elf .text方向

c - 什么是 (__ASSERT_VOID_CAST (0))?

opengl - 使用 OPENGL 或 P3D 库在Processing.org 中显示复杂矢量文件的 SVG 替代方案

c# - 敌人出现在玩家身后的机会 1 对 5

opengl - 开始 3D 编程/OpenGL

c - 指向 C 中指针的指针

c - fscanf 转结构体数组

delphi - 强制配备 nVidia Optimus 的系统为我的应用程序使用真正的 GPU?

java - LWJGL - 出现错误 : "Cannot use offsets when Element Array Buffer Object is disabled"

opengl - 如何渲染 GL_ALPHA 纹理?