首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

OPENGL有关问题(光照球的有关问题)

2012-01-06 
OPENGL问题(光照球的问题)以下是光照球的代码,为何在init()中定义的变量,比如GLfloat mat_specular[] ,将

OPENGL问题(光照球的问题)
以下是光照球的代码,为何在init()中定义的变量,比如GLfloat mat_specular[] ,将这个变量的定义放在下面的函数后面的时候(如放在 glClearColor (0.0, 0.0, 0.0, 0.0);之后时,会出现错误:\ss\ewqedwasdad\simpleopengl\light.c(77) : error C2275: 'GLfloat' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\gl\gl.h(53) : see declaration of 'GLfloat'
D:\ss\ewqedwasdad\simpleopengl\light.c(77) : error C2146: syntax error : missing ';' before identifier 'spot_direction'
D:\ss\ewqedwasdad\simpleopengl\light.c(77) : error C2065: 'spot_direction' : undeclared identifier
D:\ss\ewqedwasdad\simpleopengl\light.c(77) : error C2059: syntax error : ']'
D:\ss\ewqedwasdad\simpleopengl\light.c(90) : warning C4047: 'function' : 'const float *' differs in levels of indirection from 'int '
D:\ss\ewqedwasdad\simpleopengl\light.c(90) : warning C4024: 'glLightfv' : different types for formal and actual parameter 3
执行 cl.exe 时出错.
Creating browse info file...

simpleopengl.exe - 1 error(s), 0 warning(s)
,难道opengl中变量不可以定义在被调用之前的任何位置,而必须定义在开始么



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

/* Initialize material property, light source, lighting model,
 * and depth buffer.
 */
void init(void) 
{
  GLfloat mat_specular[] = { 1.0, 1.0, 1.0,1.0 };
  GLfloat mat_shininess[] = { 50.0 };
  GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  GLfloat white_light[]={1.0,1.0,1.0,1.0};
  GLfloat lmodel_ambient[]={0.1,0.1,0.1,1.0};
  GLfloat light_position2[] = { -2.0, -2.0, 1.0, 1.0 };
  GLfloat white_light2[] = {1.0, 1.0, 1.0, 1.0};
  GLfloat light_ambient[] = { 0.2, 0.2, 0.2, 1.0};
  GLfloat spot_direction[]= {-1.0, -1.0, 0};
  glClearColor (0.0, 0.0, 0.0, 0.0);
  glShadeModel (GL_SMOOTH);

  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  //glLightfv(GL_LIGHT0,GL_POSITION,light_position);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
  glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
   
  
   
   
  glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
  //glLightfv(GL_LIGHT0,GL_POSITION,light_position);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,white_light2);
  glLightfv(GL_LIGHT1,GL_SPECULAR,white_light2);
  glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient);

  glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,1.5);
  glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.5);
  glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,0.2);
  glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,45);
  glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spot_direction);
   
  glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0);

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);
}

void display(void)
{
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glutSolidSphere (1.0, 20, 16);
  glFlush ();
}

void reshape (int w, int h)
{
  glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity();
  if (w <= h)
  glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
//glOrtho(-1.5,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w,-10,10);
  else
  // glOrtho(-1.5,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10,10);
  glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);

  glMatrixMode(GL_MODELVIEW);


  glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
  switch (key) {
  case 27:
  exit(0);
  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);
  glutKeyboardFunc(keyboard);
  glutMainLoop();
  return 0;
}


[解决办法]
只有C++才能随用随定义(包括初始化),C不允许。

热点排行