2011-03-10 56 views
0

我在Xcode中創建了一個新的iPhone OpenGL項目。我用三角形填充了我的背景,並給了它們一個紋理,如下所示:在三角形頂部繪製一條線

CGImageRef spriteImage; 
CGContextRef spriteContext; 
GLubyte *spriteData; 
size_t width, height; 

// Sets up matrices and transforms for OpenGL ES 
glViewport(0, 0, backingWidth, backingHeight); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
//glRotatef(-90,0,0,1); 
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f); 
glMatrixMode(GL_MODELVIEW); 

// Clears the view with black 
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 

// Sets up pointers and enables states needed for using vertex arrays and textures 
glVertexPointer(2, GL_FLOAT, 0, vertices); 
glEnableClientState(GL_VERTEX_ARRAY); 
//glColorPointer(4, GL_FLOAT, 0, triangleColors); 
//glColor4f(0.0f,1.0f,0.0f,1.0f); 
//glEnableClientState(GL_COLOR_ARRAY); 
glTexCoordPointer(2, GL_FLOAT, 0, spriteTexcoords); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

// Creates a Core Graphics image from an image file 
spriteImage = [UIImage imageNamed:@"Bild.png"].CGImage; 
// Get the width and height of the image 
width = CGImageGetWidth(spriteImage); 
height = CGImageGetHeight(spriteImage); 
// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image, 
// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2. 

if(spriteImage) { 
    // Allocated memory needed for the bitmap context 
    spriteData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte)); 
    // Uses the bitmap creation function provided by the Core Graphics framework. 
    spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast); 
    // After you create the context, you can draw the sprite image to the context. 
    CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage); 
    // You don't need the context at this point, so you need to release it to avoid memory leaks. 
    CGContextRelease(spriteContext); 

    // Use OpenGL ES to generate a name for the texture. 
    glGenTextures(1, &spriteTexture); 
    // Bind the texture name. 
    glBindTexture(GL_TEXTURE_2D, spriteTexture); 
    // 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); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    // Specify a 2D texture image, providing the a pointer to the image data in memory 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData); 
    // Release the image data 
    free(spriteData); 

    // Enable use of the texture 
    glEnable(GL_TEXTURE_2D); 
    // Set a blending function to use 
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 
    // Enable blending 
    glEnable(GL_BLEND); 

我有兩個問題,BC。我對OpenGL並不熟悉。

我想寫一個方法,我給兩個點作爲參數,我希望這兩個點之間的直線被繪製在我的三角形(背景)之上。

 - (void) drawLineFromPoint1:(CGPoint)point1 toPoint2:(CGPoint)point2 { 
GLfloat triangle[] = {   //Just example points 
     0.0f, 0.0f,  
     0.1f, 0.0f, 
     0.1f, 0.0f, 
     0.1f, 0.1f 
    }; 
    GLfloat triangleColors[] = { 
     0.5f, 0.5f, 0.5f, 1.0f 
    }; 
    //now draw the triangle 
} 

就是這樣的。現在,我想有一個第二個方法,它會清除這條線(而不是背景)

我的畫法是這樣的:

- (void)drawView 
{ 
    // Make sure that you are drawing to the current context 
    [EAGLContext setCurrentContext:context]; 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glDrawElements(GL_TRIANGLES, number_vertices, GL_UNSIGNED_SHORT, indices); 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

} 

將是巨大的,如果你可以給é一些提示/幫助,

歡呼

回答

1

傳統的方法是重繪一切每當移動或刪除一條線。

+0

好吧,好吧,我想過這個。似乎需要重新繪製整個場景。所以我的問題仍然是如何在三角形上方繪製一條線。我是否需要擴展在drawView中使用的索引數組,或者可以在代碼中的某處調用glVertePointer和glDrawArrays? – peter 2011-03-10 09:26:45

+0

@peter,可以通過將GL_LINE_STRIP,GL_LINE_LOOP或GL_LINES傳遞給glDrawElements()來繪製線條。如果你的線與你的三角形在同一個平面上,並且如果啓用了深度測試,你應該考慮你的深度測試選項,參見[glDepthFunc()](http://www.khronos.org/opengles/documentation/opengles1_0/ HTML/glDepthFunc.html)。 – 2011-03-10 10:02:53

+0

那麼,我沒有啓用深度測試。我寫了這個方法,它在glDrawEle之間的drawView中調用。和glBindR。但是我的應用程序吸引奇怪thinggs和崩潰 - (無效){drawLines \t GLfloat顏色[] = { \t \t 0.5F,0.5F,0.5F,0.5F, \t \t 0.5F,0.5F,0.5F, 0.5f \t}; \t GLfloat點[] = { \t \t 0.0F,0.0F, \t \t 1.0F,0.0F \t}; \t glDisable(GL_TEXTURE_2D); \t glLoadIdentity(); \t glVertexPointer(2,GL_FLOAT,0,points); \t glColorPointer(4,GL_UNSIGNED_BYTE,0,colors); \t glEnableClientState(GL_VERTEX_ARRAY); \t glEnableClientState(GL_COLOR_ARRAY); \t glDrawArrays(GL_LINE_STRIP,0,2); \t glEnable(GL_TEXTURE_2D); \t } – peter 2011-03-10 10:14:53

0

嗯,我得到它的工作。我錯過了將我的drawView中的頂點指針設置爲我的三角形。這裏現在的作品:

- (void)drawView 
    { 

    [EAGLContext setCurrentContext:context]; 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glVertexPointer(2, GL_FLOAT, 0, vertices); 
    glDrawElements(GL_TRIANGLES, number_vertices, GL_UNSIGNED_SHORT, indices); 
    [self drawLines]; 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

} 

    - (void) drawLines{ 
    glDisable(GL_TEXTURE_2D); 

    GLfloat points[4]; 
    for (Dataset *data in buttons) { 
     CGPoint s = [data screenPosition]; 
     CGPoint p = [data slot]; 
     points[0] = (GLfloat)(768-s.y); 
     points[1] = (GLfloat)(1024-s.x); 
     points[2] = (GLfloat)(768-p.y); 
     points[3] = (GLfloat)(1024-p.x); 
     glVertexPointer(2, GL_FLOAT, 0, points); 
     glDrawArrays(GL_LINE_STRIP, 0, 2); 

    } 
    glEnable(GL_TEXTURE_2D); 


}