我正试图用box2d来模拟一个柔软的身体。到现在为止还挺好。但是我无法正确地获得纹理贴图。它适用于“opengl es 1.0”,但我无法使其适用于2.0。
柔体是由box2d物体制成的圆圈。
- (void) draw {
triangleFanPos[0] = Vertex2DMake(innerCircleBody->GetPosition().x * PTM_RATIO - self.position.x,
innerCircleBody->GetPosition().y * PTM_RATIO - self.position.y);
// Use each box2d body as a vertex and calculate coordinate for the triangle fan
for (int i = 0; i < NUM_SEGMENTS; i++) {
b2Body *currentBody = (b2Body*)[[bodies objectAtIndex:i] pointerValue];
Vertex2D pos = Vertex2DMake(currentBody->GetPosition().x * PTM_RATIO - self.position.x,
currentBody->GetPosition().y * PTM_RATIO - self.position.y);
triangleFanPos[i+1] = Vertex2DMake(pos.x, pos.y);
}
// Loop back to close off the triangle fan
triangleFanPos[NUM_SEGMENTS+1] = triangleFanPos[1];
textCoords[0] = Vertex2DMake(0.5f, 0.5f);
for (int i = 0; i < NUM_SEGMENTS; i++) {
GLfloat theta = M_PI + (deltaAngle * i);
// Calculate the X and Y coordinates for texture mapping.
textCoords[i+1] = Vertex2DMake(0.5+cosf(theta)*0.5,
0.5+sinf(theta)*0.5);
}
// Close it off.
textCoords[NUM_SEGMENTS+1] = textCoords[1];
ccGLBindTexture(GL_TEXTURE_2D, texture.name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, triangleFanPos);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
glDrawArrays(GL_TRIANGLES, 0, NUM_SEGMENTS+2);
}
其中NUM_SEGMENTS = 18
和PTM_RATIO = 32
。
使用我的旧“opnegl es 1.0”代码,我可以做到这一点,它可以工作:
// Enable texture mapping stuff
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
// Bind the OpenGL texture
glBindTexture(GL_TEXTURE_2D, [texture name]);
// Send the texture coordinates to OpenGL
glTexCoordPointer(2, GL_FLOAT, 0, textCoords);
// Send the polygon coordinates to OpenGL
glVertexPointer(2, GL_FLOAT, 0, triangleFanPos);
// Draw it
glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_SEGMENTS+2);
glEnableClientState(GL_COLOR_ARRAY);
现在它看起来像这样:
非常感谢任何帮助。感谢
答案 0 :(得分:0)
谢谢@fiddler。实际上我在我的opengl代码开始之前错过了一个CC_NODE_DRAW_SETUP()