目前我正在使用 iphone 中的 OpenGLES 在球类游戏应用程序中工作,使用 GL_TRIANGLE_STRIP 从左到右绘制球和运动动作,然后 GL_LINES 画一条线,但线不能在球运动内部绘制,如何解决这个问题?
我尝试了示例代码:
#1
- (void)update
{
self.timeSinceLastSpawn += self.timeSinceLastUpdate;
self.timeSinceLastSpawn = 0;
if (self.timeSinceLastSpawn > 2.0)
{
[self target]; // Call the Method target
}
for (SGGSprite * sprite in self.children) //SGGSprite is a class
{
[sprite update:self.timeSinceLastUpdate]; // call the update method in SGGSprite class.
}
}
#2
- (void)update:(float)dt
{
GLKVector2 curMove = GLKVector2MultiplyScalar(self.moveVelocity, dt);
self.position = GLKVector2Add(self.position, curMove);
}
#3
- (void)target
{
SGGSprite * target = [[SGGSprite alloc] initWithFile:@"Ballimage.png" effect:self.effect];
[self.children addObject:target];
int minY = target.contentSize.height/2;
int maxY = 320 - target.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
if(actualY <= 100)
{
actualY = 215;
}
target.position = GLKVector2Make(480 + (target.contentSize.width), actualY);
int minVelocity = 480.0/12.0;
int maxVelocity = 480.0/6.0;
int rangeVelocity = maxVelocity - minVelocity;
int actualVelocity = (arc4random() % rangeVelocity) + minVelocity;
target.moveVelocity = GLKVector2Make(-actualVelocity, 0);
}
#4
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
for (SGGSprite * sprite in self.children) // SGGSprite is a class, render method in SGGSprite class
{
[sprite render];
}
}
#5
- (void)render
{
self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;
self.effect.transform.modelviewMatrix = self.modelMatrix; // Call the method modelMatrix
[self.effect prepareToDraw];
long offset = (long)&_quad;
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
#6
- (GLKMatrix4) modelMatrix
{
GLKMatrix4 modelMatrix = GLKMatrix4Identity;
modelMatrix = GLKMatrix4Translate(modelMatrix, -self.position.x, -self.position.y, 0);
modelMatrix = GLKMatrix4Translate(modelMatrix, self.contentSize.width, self.contentSize.height+140, 0);
return modelMatrix;
}
// To draw a line, when the user tab on a screen.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
float xPosition = location.x;
float yPosition = location.y;
X = xPosition/50;
resultX = X - 4.8;
Y = yPosition/61.538461538461538;
resultY = Y - 3.2;
self.effect = [[GLKBaseEffect alloc] init];
[self.effect prepareToDraw];
Test1 = YES;
const GLfloat line[] =
{
0.0f, 0.5f,
resultX, resultY,
};
// Create buffer object array
GLuint bufferObjectNameArray;
// Have OpenGL generate a buffer name and store the buffer object array
glGenBuffers(1, &bufferObjectNameArray);
// Bind the buffer object array to the GL_ARRAY_BUFFER target buffer
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);
// Send the line data over to the target buffer
glBufferData(
GL_ARRAY_BUFFER, // the target buffer
sizeof(line), // the number of bytes to put into the buffer
line, // a pointer to the data being copied
GL_STATIC_DRAW); // the usage pattern of the data
// Enable vertex data to be fed down the graphics pipeline to be drawn
glEnableVertexAttribArray(GLKVertexAttribPosition);
// Specify how the GPU looks up the data
glVertexAttribPointer(
GLKVertexAttribPosition, // the currently bound buffer holds the data
2, // number of coordinates per vertex
GL_FLOAT, // the data type of each component
GL_FALSE, // can the data be scaled
2*4, // how many bytes per vertex (2 floats per vertex)
NULL);
glDrawArrays(GL_LINES, 0, 2);
}