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);
나는 두 가지 질문이 있습니다. 나는 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
}
그런 것. 지금은이 줄을 지 웁니다 제 2의 방법, (그리고 배경)
내 그리기 방법은 다음과 같습니다 갖고 싶어 : 당신이 전자에게 몇 가지 힌트/도움을 줄 수있는 경우
- (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에서 사용되는 인덱스 배열을 확장해야합니까, 아니면 내 코드에서 glVertePointer 및 glDrawArrays를 호출 할 수 있습니까? – peter
@peter에서는 GL_LINE_STRIP, GL_LINE_LOOP 또는 GL_LINES를 glDrawElements()에 전달하여 선을 그릴 수 있습니다. 라인이 삼각형과 같은 평면에 있고 깊이 테스트가 활성화 된 경우 깊이 테스트 옵션을 고려해야합니다. [glDepthFunc()] (http://www.khronos.org/opengles/documentation/opengles1_0/)를 참조하십시오. html/glDepthFunc.html). –
음, 나는 깊이 테스트를 활성화하지 않았습니다. 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, 점); \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