在三角形顶部绘制一条线
问题描述:
我在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];
}
将是巨大的,如果你可以给é一些提示/帮助,
欢呼
答
传统的方法是重绘一切每当移动或删除一条线。
答
嗯,我得到它的工作。我错过了将我的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);
}
好吧,好吧,我想过这个。似乎需要重新绘制整个场景。所以我的问题仍然是如何在三角形上方绘制一条线。我是否需要扩展在drawView中使用的索引数组,或者可以在代码中的某处调用glVertePointer和glDrawArrays? – peter 2011-03-10 09:26:45
@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
那么,我没有启用深度测试。我写了这个方法,它在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