Saturday, May 14, 2011

NSString caseInsensitiveCompare example objc

caseInsensitiveCompare method compares two NSString objects case-insensitively. Following example shows how to use caseInsensitiveCompare method with two NSStrings; @"Abc" and "aBC". The result should be "YES" since the two strings are the same  in caseInsensitiveCompare.

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
   NSString *str1 = [NSString stringWithString:@"Abc"];
   NSString *str2 = [NSString stringWithString:@"aBC"];

   if ([str1 caseInsensitiveCompare:str2] == NSOrderedSame)
   {
      NSLog(@"YES");
   }
   else
   {
      NSLog(@"NO");
   }
}

@end

NSString stringWithFormat example objc

Following code fragment shows how to use NSString:stringWithFormat method. stringWithFormat method performs the same work as C's printf() function. The object created by stringWithFormat is auto-released. Please note that you should prefix @ to all Objective-C strings.


NSString *urlString = [NSString stringWithFormat:@"%@/accounts/%d/apps/%d/dev-msgs", ILIME_BASE_URL, ILIME_ACCOUNT_ID, ILIME_APP_ID];
NSString *sendMessage = [NSString stringWithFormat:messageText.text];
NSString *requestBody = [NSString stringWithFormat:@"%@< notificationRequest>", sendMessage]; NSURL* url = [NSURL URLWithString:urlString]; [self postMessage:requestBody toURL:urlString]; 

NSString stringWithUTF8String example

stringWithUTF8String:

Returns a string created by copying the data from a given C array of UTF8-encoded bytes.
+ (id)stringWithUTF8String:(const char *)bytes
Parameters
bytes
NULL-terminated C array of bytes in UTF8 encoding.

Important: Raises an exception if bytes is NULL.
Return Value of stringWithUTF8String
A string created by copying the data from bytes.

Example of [NSString stringWithUTF8String]

Following code fragment shows how to use the method. The proper way to create NSString that contains unicode characters is to use stringWithUTF8String and the \u#### escape sequence, like this:
NSString *aString = [NSString stringWithUTF8String:"To be continued\u2026"];

NSString stringWithCharacters example

[NSString stringWithCharacters example] Following code fragment shows how to use NSString:stringWithCharacters method. It's used to create an NSString object from an C array of unicode characters. [NSString stringWithCharacters example]


const unichar myCharacters[] = {0x03C3,'x','x'};
NSString *myString = [NSString stringWithCharacters: myCharacters
                   length: sizeof myCharacters / sizeof *myCharacters];

NSString pathWithComponents example

[NSString:pathWithComponents example] Following code fragment shows how to use NSString:pathWithComponents method. It's very useful if you want to create a path name dynamically in your program by changing an existing path. [NSString:pathWithComponents example]

NSString *unexpandedPath = [[NSString alloc] initWithString:@"~/Library/YoutFolder/YourImage.png"];
NSString *imagePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];

openGL alpha blending example in c/c++

[openGL alpha blending example] Following code fragment shows a openGL alpha blending example. When drawing GL shapes, you can control how to blend images by calling glBlendFunc() function. To enable blending, you should call glEnable(GL_BLEND) before calling glBlendFunc() function. [openGL alpha blending example]

 glEnable(GL_BLEND);  // Enable blending.

  glBlendFunc(GL_ONE, GL_ZERO);  //  Just the source image.
 
  glBegin(GL_TRIANGLES);  // Drawing Using Triangles
  glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  glVertex4f( 0.0f, 1.2f, 0.0f, 1.0f); 
  glVertex4f(-1.2f,-1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.2f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 

 // Alpha blending.
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
  glBegin(GL_QUADS);  // Draw A Quad
  glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
  glVertex4f(-1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f,-1.0f, 0.0f, 1.0f); 
  glVertex4f(-1.0f,-1.0f, 0.0f, 1.0f); 
  glEnd();

glBlendFunc example - 2

[glBlendFunc example] Following code fragment shows a glBlendFunc example. When drawing GL shapes, you can control how to blend images by calling glBlendFunc() function. To enable blending, you should call glEnable(GL_BLEND) before calling glBlendFunc() function. [glBlendFunc example]

 glEnable(GL_BLEND);  // Enable blending.
  glBlendFunc(GL_SRC_ALPHA,GL_ONE); 
 
  glBegin(GL_TRIANGLES);  // Drawing Using Triangles
  glColor4f(1.0f, 1.0f, 1.0f, 0.33333f);
  glVertex4f( 0.0f, 1.2f, 0.0f, 1.0f); 
  glVertex4f(-1.2f,-1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.2f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 
 
  glBegin(GL_QUADS);  // Draw A Quad
  glColor4f(1.0f, 1.0f, 1.0f, 0.33333f);
  glVertex4f(-1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f,-1.0f, 0.0f, 1.0f); 
  glVertex4f(-1.0f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 

  glColor4f(1.0f, 1.0f, 1.0f, 0.33333f);
  glutSolidSphere(1.0f, 36, 36);

GL_BLEND example c c++ objc

[GL_BLEND example] Following code fragment shows a GL_BLEND example. When drawing GL shapes, you can control how to blend images by calling glBlendFunc() function. To enable blending, you should call glEnable(GL_BLEND) before calling glBlendFunc() function. [GL_BLEND example]

 glEnable(GL_BLEND);  // Enable blending.
  glBlendFunc(GL_ONE, GL_ZERO);  //  Just the source image.
 
  glBegin(GL_TRIANGLES);  // Drawing Using Triangles
  glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  glVertex4f( 0.0f, 1.2f, 0.0f, 1.0f); 
  glVertex4f(-1.2f,-1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.2f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 

 // Alpha blending.
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
  glBegin(GL_QUADS);  // Draw A Quad
  glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
  glVertex4f(-1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f,-1.0f, 0.0f, 1.0f); 
  glVertex4f(-1.0f,-1.0f, 0.0f, 1.0f); 
  glEnd();

glBlendFunc example c c++ objc

[glBlendFunc example] Following code fragment shows a glBlendFunc example. When drawing GL shapes, you can control how to blend images by calling glBlendFunc() function. To enable blending, you should call glEnable(GL_BLEND) before calling glBlendFunc() function. [glBlendFunc example]

 glEnable(GL_BLEND);  // Enable blending.
  glBlendFunc(GL_ONE, GL_ZERO);  //  Just the source image.
 
  glBegin(GL_TRIANGLES);  // Drawing Using Triangles
  glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  glVertex4f( 0.0f, 1.2f, 0.0f, 1.0f); 
  glVertex4f(-1.2f,-1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.2f,-1.0f, 0.0f, 1.0f); 
  glEnd(); 

 // Alpha blending.
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
  glBegin(GL_QUADS);  // Draw A Quad
  glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
  glVertex4f(-1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f, 1.0f, 0.0f, 1.0f); 
  glVertex4f( 1.0f,-1.0f, 0.0f, 1.0f); 
  glVertex4f(-1.0f,-1.0f, 0.0f, 1.0f); 
  glEnd();

GL_LINE_STRIP example c c++ objc

[GL_LINE_STRIP example] Following code fragment shows a GL_LINE_STRIP example. You can specify what to draw in glBegin() ... glEnd() block. GL_LINE_STRIP  indicates that lines shape will be drawn in the block. [GL_LINE_STRIP example]

#include <GL/glut.h>

void init()                                                              
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
}

void display()                    
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 0.0);
    glBegin(GL_LINE_STRIP);
        glVertex2f(100.0,100.0);      //f means floating point or those with decimals
        glVertex2f(400.0,100.0);
        glVertex2f(400.0, 500.0);
    glEnd();
    glFlush ();      //forces previously issued commands to execute
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)     //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100,100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

GL_POLYGON example c c++ objc

[GL_POLYGON example] Following code fragment shows a GL_POLYGON example. You can specify what to draw in glBegin() ... glEnd() block. GL_POLYGON indicates that a polygon shape will be drawn in the block.

Example of GL_POLYGON


 #include <GL/glut.h>                  //Allows access to the glut library

void init()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);        //will discuss this late in course
    glShadeModel (GL_FLAT);                    //will discuss this late in course
}

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 0.0, 0.0);            //This sets the color of the polygon - RGB format (red, green, blue) (covered next chapter)
    glBegin(GL_POLYGON);           ///polygon - many sided figure
        glVertex2f(200.0,200.0);     //f means floating point or those with decimals  - setting the vertices of the polygon
        glVertex2f(400.0,200.0);
        glVertex2f(400.0, 400.0);
    glEnd();
    glFlush ();    //forces previously issued commands to execute
}

void reshape (int w, int h)       //Covered later
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)    //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);              //calling various methods above
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100,100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}


Example of GL_POLYGON

glClearColor example c c++ objc

[glClearColor example] Following code fragment shows a glClearColor example. glClearColor() specifies the color to fill the GL surface when the surface is cleared by glClear() function call. [glClearColor example]

 #include <GL/glut.h>                  //Allows access to the glut library

void init()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);        //will discuss this late in course
    glShadeModel (GL_FLAT);                    //will discuss this late in course
}

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 0.0, 0.0);            //This sets the color of the polygon - RGB format (red, green, blue) (covered next chapter)
    glBegin(GL_POLYGON);           ///polygon - many sided figure
        glVertex2f(200.0,200.0);     //f means floating point or those with decimals  - setting the vertices of the polygon
        glVertex2f(400.0,200.0);
        glVertex2f(400.0, 400.0);
    glEnd();
    glFlush ();    //forces previously issued commands to execute
}

void reshape (int w, int h)       //Covered later
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)    //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);              //calling various methods above
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100,100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

glUseProgram example c c++ objc

[glUseProgram example] Following code fragment shows a glUseProgram example. After a program object is linked successfully, we are ready to load the shader program into the GPU. glUseProgram(id) performs this work. The shader behaviour of the GPU will change immediately. If you want to go back to default shader, call glUseProgram(0).  [glUseProgram example]

Code collected from GLWiki.

/* The vertex shader */
 char *vsSource = file2string("wave.vert");
 char *fsSource = file2string("wave.frag");
 
 /* Compile and load the program */
 
 GLuint vs, /* Vertex Shader */
     fs, /* Fragment Shader */
     sp; /* Shader Program */
 
 
 vs = glCreateShader(GL_VERTEX_SHADER);
 glShaderSource(vs, 1, &vsSource, NULL);
 glCompileShader(vs);
 printLog(vs);
 
 fs = glCreateShader(GL_FRAGMENT_SHADER);
 glShaderSource(fs, 1, &fsSource, NULL);
 glCompileShader(fs);
 printLog(fs);
 
 free(vsSource);
 free(fsSource);
 
 sp = glCreateProgram();
 glAttachShader(sp, vs);
 glAttachShader(sp, fs);
 glLinkProgram(sp);
 printLog(sp);
 
 glUseProgram(sp);

glLinkProgram example c c++ objc

[glLinkProgram example] Following code fragment shows a glLinkProgram example. After attaching the shader objects, the program must be linked by calling glLinkProgram function. It will generate an executable shader program object.

Code collected from GLWiki.

/* The vertex shader */
 char *vsSource = file2string("wave.vert");
 char *fsSource = file2string("wave.frag");
 
 /* Compile and load the program */
 
 GLuint vs, /* Vertex Shader */
     fs, /* Fragment Shader */
     sp; /* Shader Program */
 
 
 vs = glCreateShader(GL_VERTEX_SHADER);
 glShaderSource(vs, 1, &vsSource, NULL);
 glCompileShader(vs);
 printLog(vs);
 
 fs = glCreateShader(GL_FRAGMENT_SHADER);
 glShaderSource(fs, 1, &fsSource, NULL);
 glCompileShader(fs);
 printLog(fs);
 
 free(vsSource);
 free(fsSource);
 
 sp = glCreateProgram();
 glAttachShader(sp, vs);
 glAttachShader(sp, fs);
 glLinkProgram(sp);
 printLog(sp);
 
 glUseProgram(sp);

glAttachShader example c c++ objc

[glAttachShader example] Following code fragment shows a glAttachShader example. The compiled shader objects are registered to the program object by calling glAttachShader function. A program can have only one vertex shader and one fragment shader object. After attaching the shader objects, the program must be linked by calling glLinkProgram function.

Code collected from GLWiki.

/* The vertex shader */
 char *vsSource = file2string("wave.vert");
 char *fsSource = file2string("wave.frag");
 
 /* Compile and load the program */
 
 GLuint vs, /* Vertex Shader */
     fs, /* Fragment Shader */
     sp; /* Shader Program */
 
 
 vs = glCreateShader(GL_VERTEX_SHADER);
 glShaderSource(vs, 1, &vsSource, NULL);
 glCompileShader(vs);
 printLog(vs);
 
 fs = glCreateShader(GL_FRAGMENT_SHADER);
 glShaderSource(fs, 1, &fsSource, NULL);
 glCompileShader(fs);
 printLog(fs);
 
 free(vsSource);
 free(fsSource);
 
 sp = glCreateProgram();
 glAttachShader(sp, vs);
 glAttachShader(sp, fs);
 glLinkProgram(sp);
 printLog(sp);
 
 glUseProgram(sp);

glCreateProgram example

Name

glCreateProgram — Creates a program object

C Specification

GLuint glCreateProgram(void);

Description

glCreateProgram creates an empty program object and returns a non-zero value by which it can be referenced. A program object is an object to which shader objects can be attached. This provides a mechanism to specify the shader objects that will be linked to create a program. It also provides a means for checking the compatibility of the shaders that will be used to create a program (for instance, checking the compatibility between a vertex shader and a fragment shader). When no longer needed as part of a program object, shader objects can be detached. (glCreateProgram)
One or more executables are created in a program object by successfully attaching shader objects to it with glAttachShader, successfully compiling the shader objects withglCompileShader, and successfully linking the program object with glLinkProgram. These executables are made part of current state when glUseProgram is called. Program objects can be deleted by calling glDeleteProgram. The memory associated with the program object will be deleted when it is no longer part of current rendering state for any context.

Notes

Like buffer and texture objects, the name space for program objects may be shared across a set of contexts, as long as the server sides of the contexts share the same address space. If the name space is shared across contexts, any attached objects and the data associated with those attached objects are shared as well.
Applications are responsible for providing the synchronization across API calls when objects are accessed from different execution threads.


Example of glCreateProgram


Following code fragment shows a glCreateProgram example. To use the compiled shader objects (vertex shader and fragment shader), you should create a program object and associate the compiled shader object to the program.


Code collected from GLWiki.

/* The vertex shader */
 char *vsSource = file2string("wave.vert");
 char *fsSource = file2string("wave.frag");
 
 /* Compile and load the program */
 
 GLuint vs, /* Vertex Shader */
     fs, /* Fragment Shader */
     sp; /* Shader Program */
 
 
 vs = glCreateShader(GL_VERTEX_SHADER);
 glShaderSource(vs, 1, &vsSource, NULL);
 glCompileShader(vs);
 printLog(vs);
 
 fs = glCreateShader(GL_FRAGMENT_SHADER);
 glShaderSource(fs, 1, &fsSource, NULL);
 glCompileShader(fs);
 printLog(fs);
 
 free(vsSource);
 free(fsSource);
 
 sp = glCreateProgram();
 glAttachShader(sp, vs);
 glAttachShader(sp, fs);
 glLinkProgram(sp);
 printLog(sp);
 
 glUseProgram(sp);

Example of glCreateProgram