开发者

OpenGL behaving strangely

OpenGL is acting very strangely for some reason. In my subclass of NSOpenGLView, I have the following code in the -prepareOpenGL method:

- (void)prepareOpenGL {
 GLfloat lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
 GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
 GLfloat lightPosition[] = { 0.0f, 0.0f, 2.0f };

 quality = 0;
 zCoord = -6;

 [self loadTextures];

 glEnable(GL_LIGHTING);
 glEnable(GL_TEXTURE_2D);
 glShadeModel(GL_SMOOTH);
 glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
 glClearDepth(1.0f);
 glEnable(GL_DEPTH_TEST);
 glDepthFunc(GL_LEQUAL);
 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

 glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient);
 glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse);
 glLightf开发者_JAVA技巧v(GL_LIGHT1, GL_POSITION, lightPosition);
 glEnable(GL_LIGHT1);

 gameState = kGameStateRunning;
 int i = 0; // HERE ********
 [NSTimer scheduledTimerWithTimeInterval:0.03f target:self
           selector:@selector(processKeys) userInfo:nil repeats:YES];

 // Synchronize buffer swaps with vertical refresh rate
 GLint swapInt = 1;
 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
 // Setup and start displayLink
 [self setupDisplayLink];
}

I wanted to assign the timer that processes key input to an ivar so that I could invalidate it when the game is paused (and reinstantiate it on resume), however when I did that (as apposed to leaving it at [NSTimer scheduledTimer…), OpenGL doesn't display the cube I draw. When I take it away, it's fine. So i tried just adding a harmless statement, int i = 0; (maked // HERE *******), and that makes the lighting not work! When I take it away, everything is fine, but when I put it back, everything is dark. Can someone come up with a rational explanation for this? Just creating any variable makes it do this, even though there are already several declared.

Thanks.


Very strange, but I found the problem. In this line:

GLfloat lightPosition[] = { 0.0f, 0.0f, 2.0f };

I have only 3 elements instead of 4. I have no idea why it worked when I didn't have int i = 0;, but when I add 0.0f to the array everything works normally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜