Opengl使用模型視圖變換移動光源

來源:互聯網
上載者:User

標籤:create   ota   gpo   tor   ==   smooth   cti   flush   opengl   

光源繞一個物體旋轉,按下滑鼠左鍵時,光源位置旋轉。

 

#include <GL/glut.h>

static int spin = 0;
static GLdouble x_1 = 0.0;
static GLdouble y_1 = 0.0;
static GLdouble z_1 = 0.0;
void init(void)
{
  glClearColor(0.0,0.0,0.0,0.0);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST);
}

void display(void)
{
  GLfloat position[] = {0.0,0.0,1.5,1.0};
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glTranslatef(0.0,0.0,-5.0);

  glPushMatrix();
  glRotated((GLdouble)spin,x_1,y_1,z_1);
  glLightfv(GL_LIGHT0,GL_POSITION,position);
  glTranslated(0.0,0.0,1.5);

  glDisable(GL_LIGHTING);
  glColor3f(0.0,1.0,1.0);
  glutWireCube(1);
  glEnable(GL_LIGHTING);

  glPopMatrix();
  glutSolidTorus(0.275,0.85,8,15);
  glPopMatrix();
  glFlush();
}

void reshape(int w, int h)
{
  glViewport(0,0,(GLsizei)w,(GLsizei)h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(40.0,(GLfloat)w/(GLfloat)h,1.0,20.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
  switch (button)
  {
  case GLUT_LEFT_BUTTON:
    if (state == GLUT_DOWN)
    {
      x_1 = 1.0;
      y_1 = 0.0;
      z_1 = 0.0;
      spin = (spin + 30) % 360;
      glutPostRedisplay();
    }
  break;
  case GLUT_RIGHT_BUTTON:
    if (state == GLUT_DOWN)
    {
      x_1 = 0.0;
      y_1 = 1.0;
      z_1 = 0.0;
      spin = (spin - 20) % 360;
      glutPostRedisplay();
    }
    break;
  default:
    break;
  }
}

int main(int argc,char** argv)
{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
  glutInitWindowSize(500,500);
  glutInitWindowPosition(100,100);

  glutCreateWindow(argv[0]);
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);

  glutMouseFunc(mouse);
  glutMainLoop();
  return 0;
}

 

Opengl使用模型視圖變換移動光源

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.