這是我第一次瞭解opengl。我爲iphone繪製了一個簡單的opengles示例。起初我忘記使用glBindTexture
,但它的工作。後來我嘗試添加另一個四邊形,並且看到當我使用glBindTexture
時,沒有任何四邊形被紋理化,它們是白色的。然後我刪除了新添加的四元組,當glBindTexture
在那裏時它仍然不起作用。如果我評論說一切都很好。當然,我將需要超過一個四...與紋理。如何在iphone的opengles中使用glBindTexture?
編輯:這種方法是在我的視圖控制器,有一個從Apple OpenglES示例中被盜的EAGLView。它初始化opengl。我爲每個quad有一個Place
類的實例。起初爲了測試我只使用了一個四核;我只有一個Place
的對象,正如我所說我忘了使用glBindTexture
。但在此之後,當我開始使用它時,紋理不再有效,即使只有一個四邊形。
EDIT2:這裏是我想要的東西的例子:http://www.youtube.com/watch?v=U2uH-jrsSxs
-(void) setupOpenGL
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
// Enable blending
glEnable(GL_BLEND);
// Set a blending function to use
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
//!!!!!!!!!!!!
//EDIT: THIS IS WHERE I WAS WRONG: I NEED TO SET THESE FOR EVERY TEXTURE I USE
//THEY ARE NOT GLOBAL
//!!!!!!!!!!!!
// Set the texture parameters to use a minifying filter and a linear filer (weighted average)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//same for magnification
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//-----------------
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
for (Place *p in locations)
{
[p loadTexture];
[p reloadTitle];
/*
p.relativeHeading = 123.45;
p.absoluteHeading = 23.45;
p.distance = 1234.5;
[p reloadHeading];
[p reloadDistance];
*/
}
glError = glGetError(); //check if we f*cked up already or not;
NSLog(@"setupOpenGL result: %i", glError);
}
這裏是代碼我Place
類:
- (void) loadTexture
{
Byte * textureData = [Place getTexture];
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, self.textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
TEXTURE_W, TEXTURE_H, 0,
GL_RGBA, GL_UNSIGNED_BYTE, textureData);
//copy the texture to video memory we can safely release our own copy of the texture after this
GLenum glError = 0;
glError = glGetError();
}
- (void) reloadTitle
{
CGSize actualSize = CGSizeZero; //not used now
CGRect cropRect = CGRectMake(TITLE_X, TITLE_Y, TITLE_W, TITLE_H);
Byte *imageData = [self writeTextOverTexture:
[Place getTextureCrop : cropRect] : cropRect.size :
CGRectMake(0, 0, TITLE_W, TITLE_H) :
self.title : [UIFont fontWithName: @"Arial" size: 14] :
[UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 1] :
&actualSize];
glBindTexture(GL_TEXTURE_2D, self.textureID);
glTexSubImage2D(GL_TEXTURE_2D, 0,
TITLE_X, TITLE_Y, TITLE_W , TITLE_H, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
}
- (void) reloadDistance
{
//...very similar to reloadTitle
}
- (void) reloadHeading
{
// very similar to the other too
}
這裏是繪製四邊形的方法。基本上在每個「地點」我都有經度/緯度,當我繪製時,我使用iPhone羅盤標題,位置和加速計將其他地方繪製在正確的位置。
- (void) drawFrame
{
if (location == nil) return;
if (myHeading == -1) return;
[(EAGLView *)self.glView setFramebuffer];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
perspective(LANDSCAPE_FOV, WIDTH/HEIGHT , 1.0f, 20.0f);
glRotatef((GLfloat)rollAngle, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
double tmpPitchAngle = pitchAngle;
tmpPitchAngle -= 20;
tmpPitchAngle = clampValue(tmpPitchAngle, -85, +85);
//glRotatef((GLfloat) tmpPitchAngle, 1, 0, 0);
glRotatef((GLfloat) myHeading, 0, 1, 0);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for (Place *p in locations)
{
glPushMatrix();
glRotatef(-p.absoluteHeading, 0, 1, 0);
glTranslatef(0, 0, -5);
glScalef(0.4, 0.4, 1.0);
glBindTexture(GL_TEXTURE_2D, p.textureID);
glVertexPointer (3, GL_BYTE, 0, KVertices);
glTexCoordPointer(2, GL_BYTE, 0, texCoords);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glPopMatrix();
}
glError = glGetError();
NSLog(@"glError drawFrame = %i", glError);
[(EAGLView *)self.glView presentFramebuffer];
}
我該如何做這項工作?當我忘記綁定紋理時,我使用了什麼紋理。據我所知glBindTexture
是用來選擇,或激活一個紋理,所以我可以繪製或改變它。
這不是我所有的代碼!我有一個用於初始化opengl的方法,其中包括啓用紋理。然後我爲每個紋理創建一個Objective-C對象。我最好發佈更多的代碼。 – 2011-01-27 11:55:38
@kudor這不起作用,glTexParameter會影響當前綁定的紋理。這不是一個全球性的國家。 – 2011-01-27 12:28:50