Decompose matrix, functions for GLKit

Иногда появляется необходимость развернуть объект из одного положения в другое. Для этого нужно извлечь угол из текущей матрицы и добавить дельта угол, в результате чего получим угол на который будет повернут объект. Итак, сперва необходимо получить размер по x,y,z и исходя из этих величин и матрицы, получить матрицу вращения из которой мы уже получим угол на который повернут наш объект.

Decompose Scale factor, matrix rotation and translate vector from modelViewMatrix

Translate vector:
vt = (M41, M42, M43)T

Getting scaling factors:
sx = sqrt(M112 + M122 + M132);
sy = sqrt(M212 + M222 + M232);
sz = sqrt(M312 + M322 + M332);

 

 

Now you can work backwards for the rotation matrix:

Mrot = M11/sx   M12/sx   M13/sx   0
M21/sy M22/sy M23/sy 0
M31/sz M32/sz M33/sz 0
0 0 0 1

Additional functions for iOS GLKit
Continue reading

Print OpenGL error and description

Print OpenGL error:

1
2
3
4
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
   NSLog(@"Error glGetError: glError: 0x%04X", err);
}

GL_INVALID_ENUM​, 0×0500: Given when an enumeration parameter is not a legal enumeration for that function. This is given only for local problems; if the spec allows the enumeration in certain circumstances, and other parameters or state dictate those circumstances, then GL_INVALID_OPERATION​ is the result instead.

GL_INVALID_VALUE​, 0×0501: Given when a value parameter is not a legal value for that function. This is only given for local problems; if the spec allows the value in certain circumstances, and other parameters or state dictate those circumstances, then GL_INVALID_OPERATION is the result instead.

GL_INVALID_OPERATION​, 0×0502: Given when the set of state for a command is not legal for the parameters given to that command. It is also given for commands where combinations of parameters define what the legal parameters are.

GL_OUT_OF_MEMORY​, 0×0503: Given when performing an operation that can allocate memory, but the memory cannot be allocated. The results of OpenGL functions that return this error are undefined; it is allowable for partial operations to happen.

GL_INVALID_FRAMEBUFFER_OPERATION​, 0×0506: Given when doing anything that would attempt to read from or write/render to a framebuffer that is not complete, as defined here.

GL_STACK_OVERFLOW​1, 0×0503: Given when a stack pushing operation cannot be done because it would overflow the limit of that stack’s size.

GL_STACK_UNDERFLOW​1, 0×0504: Given when a stack popping operation cannot be done because the stack is already at its lowest point.

GL_TABLE_TOO_LARGE​1, 0×8031: Part of the ARB_imaging extension.

Posted in iOS, OpenGL
Tagged error, OpenGL ES 2.0

GLKit, Opengl ES 2.0 Picking Ray for select object

Solution for picking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
GLKVector4 normalisedVector = GLKVector4Make((2 * position.x / self.view.bounds.size.width - 1),
                                                 (2 * (self.view.bounds.size.height-position.y) / self.view.bounds.size.height - 1),  
                                                   -1,
                                                   1);

GLKMatrix4 inversedMatrix = GLKMatrix4Invert(_modelViewProjectionMatrix, nil);

GLKVector4 near_point = GLKMatrix4MultiplyVector4(inversedMatrix, normalisedVector);

near_point.v[3] = 1.0/near_point.v[3];
near_point = GLKVector4Make(near_point.v[0]*near_point.v[3], near_point.v[1]*near_point.v[3], near_point.v[2]*near_point.v[3], 1);

normalisedVector.z = 1.0;
GLKVector4 far_point = GLKMatrix4MultiplyVector4(inversedMatrix, normalisedVector);

far_point.v[3] = 1.0/far_point.v[3];
far_point = GLKVector4Make(far_point.v[0]*far_point.v[3], far_point.v[1]*far_point.v[3], far_point.v[2]*far_point.v[3], 1);

On

Disable ARC for a single file in a project

It is possible to disable ARC for individual files by adding the

1
-fno-objc-arc

compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.

Continue reading

New Objective-C literal syntax (Apple LLVM 4.0 compiler in the Xcode 4.4 beta)

Copy from blog.ablepear.com.

If you’ve done any Objective-C programming, you’ve seen the NSString literal syntax:

1
2
3
4
5
6
// NSString literal
NSString *name1 = @"Lana Kane";

// creating NSString from a C string literal
NSString *name2 = [NSString stringWithCString:"Sterling Archer"
                                    encoding:NSUTF8StringEncoding];

Continue reading

Download Tools for Xcode 4.3

If you need tools for Xcode 4.3, download tools!

Posted in Apple, Mac OS X
Tagged OpenGL Profiler, OpenGL Shader Builder, Pixie, Quartz Composer, Quartz Composer Visualizer,

AsyncURLConnection with pause/resume and progress +block’s

I rewrite one class with async connection, and add progress block and pause/resume.

Example usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NSString *comboUrl = @"http://support.apple.com/downloads/DL1484/en_US/MacOSXUpdCombo10.7.3.dmg";
AsyncURLConnection *aConnection = [AsyncURLConnection request:comboUrl
                  completeBlock:^(NSData *data, NSString *url) {
                      [data writeToFile:filename atomically:NO];
                  } errorBlock:^(NSError *error) {
                      NSLog(@"Error %f", error);
                  } progress:^(float progress) {
                      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                          /* process downloaded data in Concurrent Queue */
                          dispatch_async(dispatch_get_main_queue(), ^{
                              /* update UI on Main Thread */
                          });
                      });
                      NSLog(@"progress %f", progress);
                  }];

Very easy to use blocks of AsyncURLConnection (aka async URLConnection) and NSProgressIndicator indicator. You can implement progress bar of downloading in your mac os x application like this example.

1
2
3
4
5
6
7
8
NSProgressIndicator *horizontal = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(200, 300, 200, 12)];
    [horizontal setStyle:NSProgressIndicatorBarStyle];
    [horizontal setIndeterminate:NO];
    [horizontal setControlSize:NSSmallControlSize];
    [horizontal setDisplayedWhenStopped:YES];
    [horizontal setMaxValue:1];
    [self addSubview:horizontal];
    [horizontal release];

And just set progress value.

1
2
3
4
5
6
7
8
9
         progress:^(float progress) {
                          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                          /* process downloaded data in Concurrent Queue */
                          dispatch_async(dispatch_get_main_queue(), ^{
                              /* update UI on Main Thread */
                               [horizontal setDoubleValue:progress];
                          });
                      });
                  }];

Continue reading

Posted in iOS, Mac OS X
Tagged AsyncURLConnection, blocks, NSMutableURLRequest, NSProgressIndicator, pause, resume, URLConnection, User-Agent, __block
1 2 3 4 5 6 23 Next »