iphone cgbitmapcontextcreate () function explained

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_3e50cef401019cd2.html

Cgcontextref Cgbitmapcontextcreate (

void *data,

size_t width,

size_t height,

size_t Bitspercomponent,

size_t Bytesperrow,

Cgcolorspaceref ColorSpace,

Cgbitmapinfo Bitmapinfo

);

The parameter data points to the area of memory where the drawing operation is rendered, and the memory area should be (bytesperrow*height) bytes. If there is no particular requirement for the area of memory that the drawing operation is rendered, then NULL is passed to the parameter date.

Parameter width represents the width of the area of memory being rendered.

The parameter height represents the height of the memory area being rendered.

The bitspercomponent parameter is the BITS bit that the component in the rendered memory area needs to use on each pixel of the screen, for example, if you use 32-bit pixel and RGB color format, The BITS bit that each component in the RGBA color format needs to use at each pixel point on the screen is 32/4=8.

The parameter Bytesperrow represents the number of bytes bits used by each row in the rendered memory area.

The parameter colorspace is used for the bitmap context of the area of the rendered memory.

The parameter bitmapinfo specifies whether the view of the rendered memory area contains an alpha (perspective) channel and the corresponding position of each pixel, in addition to specifying if the component is a floating-point value or an integer value.

A copy of the Code on the Web:

@implementation Glview

#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>

#import <OpenGLES/ES2/gl.h>

#import <OpenGLES/ES2/glext.h>

#import <OpenGLES/ES1/gl.h>

#import <OpenGLES/ES1/glext.h>

@interface Glview:uiview {

@private

Caeagllayer *_eagllayer;

Eaglcontext *_context;

Gluint _colorrenderbuffer;

Gluint _position;

Gluint _color;

}

@end

#import "GLView.h"

@implementation Glview

To set the layer class to display OpenGL content, you need to set its default layer to a special layer.

+ (Class) layerclass

{

return [Caeagllayer class];

}

Set layer to opaque, by default, the Calayer is transparent, and the transparent layer hits the performance load, especially the OpenGL layer.

-(void) Setuplayer

{

_eagllayer = (Caeagllayer *) Self.layer;

_eagllayer.opaque = YES;

}

Create content, whatever you need OpenGL to help you do what you need this eaglcontext, eaglcontext management so through

Information about the draw that OpenGL is making.

-(void) Setupcontext

{

Eaglrenderingapi API = kEAGLRenderingAPIOpenGLES1;

_context = [[Eaglcontext alloc] initwithapi:api];

if (!_context)

{

}

if (![ Eaglcontext Setcurrentcontext:_context])

{

NSLog (@ "Failed to set Current context");

}

}

Create render buffer (render buffering)

Renderbuffer for storing rendered images

Glgenrenderbuffers creates a renderbuffer that returns a name that is used to mark Renderbuffer _colorrenderbuffer;

Call Glbindrenderbuffer to tell OpenGL that the object you just created is an object of type Gl_renderbuffer

Finally, allocate space.

-(void) Setuprenderbuffer

{

Glgenrenderbuffers (1, &_colorrenderbuffer);

Glbindrenderbuffer (Gl_renderbuffer, _colorrenderbuffer);

[_context Renderbufferstorage:gl_renderbuffer Fromdrawable:_eagllayer];

}

Create a framebuffer, (frame buffer)

//

-(void) Setupframebuffer

{

Gluint FrameBuffer;

Glgenframebuffers (1, &framebuffer);

Glbindframebuffer (Gl_framebuffer, FRAMEBUFFER);

Attach the newly created render buffer to the gl_color_attachment0 position of frame buffer

Glframebufferrenderbuffer (Gl_framebuffer, Gl_color_attachment0,

Gl_renderbuffer, _colorrenderbuffer);

}

Before dealing with vertextes shader, clean the screen to show another color.

-(void) render

{

Const Glfloat squarevertices[] = {

-0.5f, -0.5f,

0.5f, -0.5f,

-0.5f, 0.5f,

0.5f, 0.5f,

};

The coordinate system of the texture is (0, 0) below the left.

Const Glshort squartexturecoords[] = {

0, 0,//top Left

0, 1,

1, 0,

1, 1

};

[Eaglcontext Setcurrentcontext:context];

Glbindframebufferoes (Gl_framebuffer_oes, Defaultframebuffer);

Glviewport (0, 0, backingwidth, backingheight);

Tell OpenGL we're working in projection mode.

Glmatrixmode (gl_projection);

Resets all states, such as rotation and movement

Glloadidentity ();

Glenable (gl_texture_2d);

Glorthof ( -1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);

Glmatrixmode (Gl_modelview);

Set an RGBA color that will then be applied to the full screen with this color

Glclearcolor (0.5f, 0.5f, 0.5f, 1.0f);

Glclear (Gl_color_buffer_bit);

Glvertexpointer (2, gl_float, 0, squarevertices);

Glenableclientstate (Gl_vertex_array);

Gltexcoordpointer (2, Gl_short, 0, squartexturecoords);

Glenableclientstate (Gl_texture_coord_array);

Gl_triangle_strip the first two vertices, unless, and then traverse each vertex, these vertices will use the first two vertices together to form a triangle

Gl_triangle_fan skips the beginning of the 2 vertices, then iterates through each vertex, and then the vertices together with their previous, and the first vertex of the array, form a triangle

Gldrawarrays (Gl_triangle_strip, 0, 4);

Glbindrenderbufferoes (Gl_renderbuffer_oes, Colorrenderbuffer);

[Context Presentrenderbuffer:gl_renderbuffer_oes];

}

void) Loadtexture

{

This imageref does not contain image data.

Attention:

The size of the loaded picture must be 2 of the n-th square

Cgimageref imageref = [[UIImage imagenamed:@ "Tile_floor.png"] cgimage];

size_t width = cgimagegetwidth (imageref);

size_t height = cgimagegetheight (imageref);

Rgba number, 4 bytes per pixel

Glubyte *texturedata = (Glubyte *) malloc (width * height);

Cgcontextref Textrurecontext = Cgbitmapcontextcreate (

Texturedata,

Width

Height

8,//8 bits per channel

Width * 4,

Cgimagegetcolorspace (Imageref),

Kcgimagealphapremultipliedlast);

Cgcontextdrawimage (Textrurecontext, cgrectmake (0, 0, width, height), imageref);

Only one texture is needed

Glgentextures (1, &_texture[0]);

Activate texture new texture name loaded into the texture unit of the current period

Glbindtexture (gl_texture_2d, _texture[0]);

Send texture data to OpenGL

Target is basically gl_texture_2d.

Level texture detailed program, 0 means to allow all the details of the picture,

Internal_format and format must be the same

Border must always be 0 OPENGL es does not support texture boundaries

Type pixel types 4 bytes per pixel (unsigned shaping)

pixels actual picture data pointer

Glteximage2d (gl_texture_2d, 0, Gl_rgba, width, height, 0, gl_rgba,gl_unsigned_byte, texturedata);

We'll shrink the texture when it's in and out (far away). How to handle (Gl_linear is smooth, glnearest is to select the texture pixel near the mouth)

Gltexparameterf (gl_texture_2d, Gl_texture_min_filter, gl_linear);

Gltexparameterf (gl_texture_2d, Gl_texture_mag_filter, gl_linear);

Glenable (gl_texture_2d);

//

Free (texturedata);

Cgcontextrelease (Textrurecontext);

}

-(ID) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self) {

[Self setuplayer];

[Self setupcontext];

[Self setuprenderbuffer];

[Self setupframebuffer];

[Self loadtexture];

[Self render];

}

return self;

}

-(void) dealloc

{

[_context release];

_context = nil;

[Super Dealloc];

}

@end

iphone cgbitmapcontextcreate () function explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.