私はこのsource codeをベースに使用しており、私の要件に応じてコードを変更しようとしています。画像にメッシュを作成するためのコードを以下に示します。OpenGL iphone sdkでのメッシュ作成
- (ボイド)populateMesh {
verticalDivisions = kVerticalDivisions; horizontalDivisions = kHorisontalDivisions; unsigned int verticesArrsize = (kVerticalDivisions * ((2 + kHorisontalDivisions * 2) * 3)); unsigned int textureCoordsArraySize = kVerticalDivisions * ((2 + kHorisontalDivisions * 2) * 2); verticesArr = (GLfloat *)malloc(verticesArrsize * sizeof(GLfloat)); textureCoordsArr = (GLfloat*)malloc(textureCoordsArraySize * sizeof(GLfloat)); if (verticesArr == NULL) { NSLog(@"verticesArr = NULL!"); } float height = kWindowHeight/verticalDivisions; float width = kWindowWidth/horizontalDivisions; int i,j, count; count = 0; for (j=0; j<verticalDivisions; j++) { for (i=0; i<=horizontalDivisions; i++, count+=6) { //2 vertices each time... float currX = i * width; float currY = j * height; verticesArr[count] = currX; verticesArr[count+1] = currY + height; verticesArr[count+2] = 0.0f; verticesArr[count+3] = currX; verticesArr[count+4] = currY; verticesArr[count+5] = 0.0f; } } float xIncrease = 1.0f/horizontalDivisions; float yIncrease = 1.0f/verticalDivisions; int x,y; //int elements; count = 0; for (y=0; y<verticalDivisions; y++) { for (x=0; x<horizontalDivisions+1; x++, count+=4) { float currX = x *xIncrease; float currY = y * yIncrease; textureCoordsArr[count] = (float)currX; textureCoordsArr[count+1] = (float)currY + yIncrease; textureCoordsArr[count+2] = (float)currX; textureCoordsArr[count+3] = (float)currY; } } // int cnt; // int cnt = 0; NSLog(@"expected %i vertices, and %i vertices were done",(verticalDivisions * ((2 + horizontalDivisions*2) * 2)) , count); }
drawViewコードである後。私は[自己populateMesh]と呼ばれているセットアップビューで
- (void)drawView:(GLView*)view;
{
static GLfloat rot = 0.0;
glBindTexture(GL_TEXTURE_2D, texture[0]);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordsArr);
glVertexPointer(3, GL_FLOAT, 0, verticesArr);
glPushMatrix();{
int i;
for (i=0; i<verticalDivisions; i++) {
glDrawArrays(GL_TRIANGLE_STRIP, i*(horizontalDivisions*2+2), horizontalDivisions*2+2);
}
}glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}
。を関数の最後に追加します。 私の問題は、コードを変更した後に、画面に黒色の表示が表示されるという空白が表示されることです。誰かが私がどこで何か間違いをしているのを理解できますか?私はOpenGLの初心者で、メッシュを使って画像を操作しようとしています。できるだけ早く手伝ってください。前もって感謝します。
セットアップビューのコードは次のとおりです。
-(void)setupView:(GLView*)view {
const GLfloat zNear = 0.01, zFar = 1000.0, fieldOfView = 45.0;
GLfloat size;
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView)/2.0);
CGRect rect = view.bounds;
glFrustumf(-size, size, -size/(rect.size.width/rect.size.height), size/ (rect.size.width/rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
NSString *path = [[NSBundle mainBundle] pathForResource:@"texture" ofType:@"png"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil)
NSLog(@"Do real error checking here");
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc(height * width * 4);
CGContextRef context = CGBitmapContextCreate(imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM (context, 0, height);
CGContextScaleCTM (context, 1.0, -1.0);
CGColorSpaceRelease(colorSpace);
CGContextClearRect(context, CGRectMake(0, 0, width, height));
CGContextDrawImage(context, CGRectMake(0, 0, width, height),image.CGImage);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
CGContextRelease(context);
free(imageData);
[image release];
[texData release];
[self populateMesh];
}
EDITこれは私が出したとして取得していますものです。期待通りですが、通常のグリッドは...
モデルビューと投影ビューのマトリックスを正しく設定しましたか?行列の初期設定でコードを提供できますか? – Prcela
画像のピクセルサイズが2の累乗で表すことができることを確認してください。例:512x512 – Prcela
設定したビューコードが含まれています。 populateMesh関数を実装する前に、私は画像を表示することができました... – DivineDesert