├── .gitignore ├── Classes ├── CC3Math │ ├── CC3Foundation.h │ ├── CC3Foundation.m │ ├── CC3GLMatrix.h │ ├── CC3GLMatrix.m │ ├── CC3Kazmath.c │ ├── CC3Kazmath.h │ ├── CC3Logging.h │ ├── CC3Math.h │ ├── CC3Math.m │ ├── CC3MathInclude.h │ ├── NSValue+CC3Types.h │ ├── NSValue+CC3Types.m │ └── ccTypes.h ├── REBuffer.h ├── REBuffer.m ├── RECache.h ├── RECache.m ├── RECamera.h ├── RECamera.m ├── REDirector.h ├── REDirector.m ├── REGLStateManager.h ├── REGLStateManager.m ├── REGLTypes.h ├── REGLTypes.m ├── REGLView.h ├── REGLView.m ├── REKeyframedMeshNode.h ├── REKeyframedMeshNode.m ├── RELight.h ├── RELight.m ├── REMaterial.h ├── REMaterial.m ├── RENSArrayAdditions.h ├── RENSArrayAdditions.m ├── RENSValueAdditions.h ├── RENSValueAdditions.m ├── RENode.h ├── RENode.m ├── REProgram.h ├── REProgram.m ├── RERotator.h ├── RERotator.m ├── REScene.h ├── REScene.m ├── REScheduler.h ├── REScheduler.m ├── REShader.h ├── REShader.m ├── RESprite.h ├── RESprite.m ├── RESpriteBatchNode.h ├── RESpriteBatchNode.m ├── RETexture.h ├── RETexture.m ├── REVertexArrayObject.h ├── REVertexArrayObject.m ├── REWavefrontMesh.h ├── REWavefrontMesh.m ├── REWavefrontMeshElementIndexBatch.h ├── REWavefrontMeshElementIndexBatch.m ├── REWavefrontMeshGroupNode.h ├── REWavefrontMeshGroupNode.m ├── REWavefrontMeshNode.h ├── REWavefrontMeshNode.m ├── REWorld.h ├── REWorld.m └── Rend.h ├── Examples └── Rend Example Collection │ ├── BumpSphere Example.plist │ ├── Rend Example Collection.xcodeproj │ └── project.pbxproj │ ├── Rend Example Collection │ ├── AngleUtil.h │ ├── AngleUtil.m │ ├── BumpSphereAppDelegate.h │ ├── BumpSphereAppDelegate.m │ ├── GLViewController.h │ ├── GLViewController.m │ ├── Rend Example Collection-Info.plist │ ├── Rend Example Collection-Prefix.pch │ ├── Resources │ │ ├── teapot.mtl │ │ └── teapot.obj │ ├── SphereNode.h │ ├── SphereNode.m │ ├── SphereViewController.h │ ├── SphereViewController.m │ ├── SphereViewController.xib │ ├── TeapotAppDelegate.h │ ├── TeapotAppDelegate.m │ ├── TeapotController.h │ ├── TeapotController.m │ ├── TeapotNode.h │ ├── TeapotNode.m │ ├── UIImageAdditions.h │ ├── UIImageAdditions.m │ ├── UIViewAdditions.h │ ├── UIViewAdditions.m │ ├── en.lproj │ │ └── InfoPlist.strings │ ├── main.m │ ├── sBumpSphere.fsh │ ├── sBumpSphere.vsh │ ├── sFragmentLighting.fsh │ ├── sFragmentLighting.vsh │ ├── sVertexLighting.fsh │ └── sVertexLighting.vsh │ └── TeapotExample copy-Info.plist ├── LICENSE.cocos2d ├── LICENSE.cocos3d ├── LICENSE.rend ├── README.md └── Shaders ├── sRESprite.fsh ├── sRESprite.vsh ├── sRESpriteBatch.fsh └── sRESpriteBatch.vsh /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac OS X 2 | *.DS_Store 3 | 4 | # Xcode 5 | *.pbxuser 6 | *.mode1v3 7 | *.mode2v3 8 | *.perspectivev3 9 | *.xcuserstate 10 | project.xcworkspace/ 11 | xcuserdata/ 12 | 13 | # Generated files 14 | *.o 15 | *.pyc 16 | *.hi 17 | *.swp 18 | 19 | #Python modules 20 | MANIFEST 21 | dist/ 22 | build/ 23 | 24 | # Backup files 25 | *~.nib 26 | \#*# 27 | .#* 28 | -------------------------------------------------------------------------------- /Classes/CC3Math/CC3Kazmath.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CC3Kazmath.h 3 | * 4 | * $Version: cocos3d 0.5.3 (dc0d1961822d) on 2011-04-05 $ 5 | * 6 | * Copyright (c) 2008, Luke Benstead. 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without modification, 10 | * are permitted provided that the following conditions are met: 11 | * 12 | * Redistributions of source code must retain the above copyright notice, 13 | * this list of conditions and the following disclaimer. 14 | * Redistributions in binary form must reproduce the above copyright notice, 15 | * this list of conditions and the following disclaimer in the documentation 16 | * and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | * 29 | * http://www.kazade.co.uk/kazmath/ 30 | * 31 | * Augmented and modified for use with Objective-C in cocos3D by Bill Hollings 32 | * Additions and modifications copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved. 33 | * http://www.brenwill.com 34 | */ 35 | 36 | /** @file */ // Doxygen marker 37 | 38 | #define KM_FALSE 0 39 | #define KM_TRUE 1 40 | #define kmScalar float 41 | 42 | /** A three-dimensional vector. */ 43 | typedef struct kmVec3 { 44 | kmScalar x; 45 | kmScalar y; 46 | kmScalar z; 47 | } kmVec3; 48 | 49 | /** A homogeneous four-dimensional vector. */ 50 | typedef struct kmVec4 { 51 | kmScalar x; 52 | kmScalar y; 53 | kmScalar z; 54 | kmScalar w; 55 | } kmVec4; 56 | 57 | /** A rotational quaternion */ 58 | typedef struct kmQuaternion { 59 | kmScalar x; 60 | kmScalar y; 61 | kmScalar z; 62 | kmScalar w; 63 | } kmQuaternion; 64 | 65 | /** A standard 4x4 matrix */ 66 | typedef struct { 67 | kmScalar mat[16]; 68 | } kmMat4; 69 | 70 | 71 | /** Returns a kmVec3 structure constructed from the vector components. */ 72 | kmVec3 kmVec3Make(kmScalar x, kmScalar y, kmScalar z); 73 | 74 | /** Returns the length of the vector. */ 75 | kmScalar kmVec3Length(const kmVec3* pIn); 76 | 77 | /** Normalizes the vector to unit length, stores the result in pOut and returns the result. */ 78 | kmVec3* kmVec3Normalize(kmVec3* pOut, const kmVec3* pIn); 79 | 80 | /** Multiplies pM1 with pM2, stores the result in pOut, returns pOut. */ 81 | kmMat4* kmMat4Multiply(kmMat4* pOut, const kmMat4* pM1, const kmMat4* pM2); 82 | 83 | /** 84 | * Builds a rotation matrix that rotates around all three axes, y (yaw), x (pitch) and z (roll), 85 | * in that order, stores the result in pOut and returns the result. 86 | * This algorithm matches up along the positive Y axis, which is the OpenGL ES default. 87 | */ 88 | kmMat4* kmMat4RotationYXZ(kmMat4* pOut, const kmScalar xRadians, const kmScalar yRadians, const kmScalar zRadians); 89 | 90 | /** 91 | * Builds a rotation matrix that rotates around all three axes z (roll), y (yaw), and x (pitch), 92 | * in that order, stores the result in pOut and returns the result 93 | * This algorithm matches up along the positive Z axis, which is used by some commercial 3D worlds. 94 | */ 95 | kmMat4* kmMat4RotationZYX(kmMat4* pOut, const kmScalar xRadians, const kmScalar yRadians, const kmScalar zRadians); 96 | 97 | /** Builds a rotation matrix around the X-axis, stores the result in pOut and returns the result */ 98 | kmMat4* kmMat4RotationX(kmMat4* pOut, const float radians); 99 | 100 | /** Builds a rotation matrix around the Y-axis, stores the result in pOut and returns the result */ 101 | kmMat4* kmMat4RotationY(kmMat4* pOut, const float radians); 102 | 103 | /** Builds a rotation matrix around the Z-axis, stores the result in pOut and returns the result */ 104 | kmMat4* kmMat4RotationZ(kmMat4* pOut, const float radians); 105 | 106 | /** 107 | * Build a rotation matrix from an axis and an angle, 108 | * stores the result in pOut and returns the result. 109 | */ 110 | kmMat4* kmMat4RotationAxisAngle(kmMat4* pOut, const kmVec3* axis, kmScalar radians); 111 | 112 | /** 113 | * Builds a rotation matrix from a quaternion to a rotation matrix, 114 | * stores the result in pOut and returns the result. 115 | */ 116 | kmMat4* kmMat4RotationQuaternion(kmMat4* pOut, const kmQuaternion* pQ); 117 | 118 | /** Extracts a quaternion from a rotation matrix, stores the result in quat and returns the result */ 119 | kmQuaternion* kmQuaternionRotationMatrix(kmQuaternion* quat, const kmMat4* pIn); 120 | 121 | /** 122 | * Builds a transformation matrix that translates, rotates and scales according to the specified vectors, 123 | * stores the result in pOut and returns the result. 124 | */ 125 | kmMat4* kmMat4Transformation(kmMat4* pOut, const kmVec3 translation, const kmVec3 rotation, const kmVec3 scale); 126 | 127 | /** Gauss-Jordan matrix inversion function */ 128 | int kmGaussJordan(kmMat4 *a, kmMat4 *b); 129 | 130 | /** Get the value from the matrix at the specfied row and column. */ 131 | float kmMatGet(const kmMat4* pIn, int row, int col); 132 | 133 | /** Set the value into the matrix at the specfied row and column. */ 134 | void kmMatSet(kmMat4 * pIn, int row, int col, float value); 135 | 136 | /** Swap the elements in the matrix at the specfied row and column coordinates. */ 137 | void kmMatSwap(kmMat4 * pIn, int r1, int c1, int r2, int c2); 138 | -------------------------------------------------------------------------------- /Classes/CC3Math/CC3Logging.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CC3Logging.h 3 | * 4 | * $Version: cocos3d 0.5-beta (eab7e651f462) on 2011-01-31 $ 5 | * Author: Bill Hollings 6 | * Copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved. 7 | * http://www.brenwill.com 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | * 27 | * http://en.wikipedia.org/wiki/MIT_License 28 | * 29 | * Thanks to Nick Dalton for providing the underlying ideas for using variadic macros as 30 | * well as for outputting the code location as part of the log entry. For his ideas, see: 31 | * http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog 32 | */ 33 | 34 | /** @file */ // Doxygen marker 35 | 36 | /** 37 | * For Objective-C code, this library adds flexible, non-intrusive logging capabilities 38 | * that can be efficiently enabled or disabled via compile switches. 39 | * 40 | * There are four levels of logging: Trace, Info, Error and Debug, and each can be enabled 41 | * independently via the LOGGING_LEVEL_TRACE, LOGGING_LEVEL_INFO, LOGGING_LEVEL_ERROR and 42 | * LOGGING_LEVEL_DEBUG switches, respectively. 43 | * 44 | * In addition, ALL logging can be enabled or disabled via the LOGGING_ENABLED switch. 45 | * 46 | * Logging functions are implemented here via macros. Disabling logging, either entirely, or 47 | * at a specific level, completely removes the corresponding log invocations from the compiled 48 | * code, thus eliminating both the memory and CPU overhead that the logging calls would add. 49 | * You might choose, for example, to completely remove all logging from production release code, 50 | * by setting LOGGING_ENABLED off in your production builds settings. Or, as another example, 51 | * you might choose to include Error logging in your production builds by turning only 52 | * LOGGING_ENABLED and LOGGING_LEVEL_ERROR on, and turning the others off. 53 | * 54 | * To perform logging, use any of the following function calls in your code: 55 | * 56 | * LogTrace(fmt, ...) - recommended for detailed tracing of program flow 57 | * - will print if LOGGING_LEVEL_TRACE is set on. 58 | * 59 | * LogInfo(fmt, ...) - recommended for general, infrequent, information messages 60 | * - will print if LOGGING_LEVEL_INFO is set on. 61 | * 62 | * LogError(fmt, ...) - recommended for use only when there is an error to be logged 63 | * - will print if LOGGING_LEVEL_ERROR is set on. 64 | * 65 | * LogDebug(fmt, ...) - recommended for temporary use during debugging 66 | * - will print if LOGGING_LEVEL_DEBUG is set on. 67 | * 68 | * In each case, the functions follow the general NSLog/printf template, where the first argument 69 | * "fmt" is an NSString that optionally includes embedded Format Specifiers, and subsequent optional 70 | * arguments indicate data to be formatted and inserted into the string. As with NSLog, the number 71 | * of optional arguments must match the number of embedded Format Specifiers. For more info, see the 72 | * core documentation for NSLog and String Format Specifiers. 73 | * 74 | * You can choose to have each logging entry automatically include class, method and line information 75 | * by enabling the LOGGING_INCLUDE_CODE_LOCATION switch. 76 | * 77 | * Although you can directly edit this file to turn on or off the switches below, the preferred 78 | * technique is to set these switches via the compiler build setting GCC_PREPROCESSOR_DEFINITIONS 79 | * in your build configuration. 80 | */ 81 | 82 | /** 83 | * Set this switch to enable or disable logging capabilities. 84 | * This can be set either here or via the compiler build setting GCC_PREPROCESSOR_DEFINITIONS 85 | * in your build configuration. Using the compiler build setting is preferred for this to 86 | * ensure that logging is not accidentally left enabled by accident in release builds. 87 | */ 88 | #ifndef LOGGING_ENABLED 89 | # define LOGGING_ENABLED 0 90 | #endif 91 | 92 | /** 93 | * Set any or all of these switches to enable or disable logging at specific levels. 94 | * These can be set either here or as a compiler build settings. 95 | * For these settings to be effective, LOGGING_ENABLED must also be defined and non-zero. 96 | */ 97 | #ifndef LOGGING_LEVEL_TRACE 98 | # define LOGGING_LEVEL_TRACE 0 99 | #endif 100 | #ifndef LOGGING_LEVEL_INFO 101 | # define LOGGING_LEVEL_INFO 1 102 | #endif 103 | #ifndef LOGGING_LEVEL_ERROR 104 | # define LOGGING_LEVEL_ERROR 1 105 | #endif 106 | #ifndef LOGGING_LEVEL_DEBUG 107 | # define LOGGING_LEVEL_DEBUG 1 108 | #endif 109 | 110 | /** 111 | * Set this switch to indicate whether or not to include class, method and line information 112 | * in the log entries. This can be set either here or as a compiler build setting. 113 | */ 114 | #ifndef LOGGING_INCLUDE_CODE_LOCATION 115 | #define LOGGING_INCLUDE_CODE_LOCATION 0 116 | #endif 117 | 118 | // *********** END OF USER SETTINGS - Do not change anything below this line *********** 119 | 120 | 121 | #if !(defined(LOGGING_ENABLED) && LOGGING_ENABLED) 122 | #undef LOGGING_LEVEL_TRACE 123 | #undef LOGGING_LEVEL_INFO 124 | #undef LOGGING_LEVEL_ERROR 125 | #undef LOGGING_LEVEL_DEBUG 126 | #endif 127 | 128 | // Logging format 129 | #define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__) 130 | #define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) NSLog((@"%s[Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__) 131 | 132 | #if defined(LOGGING_INCLUDE_CODE_LOCATION) && LOGGING_INCLUDE_CODE_LOCATION 133 | #define LOG_FORMAT(fmt, lvl, ...) LOG_FORMAT_WITH_LOCATION(fmt, lvl, ##__VA_ARGS__) 134 | #else 135 | #define LOG_FORMAT(fmt, lvl, ...) LOG_FORMAT_NO_LOCATION(fmt, lvl, ##__VA_ARGS__) 136 | #endif 137 | 138 | // Trace logging - for detailed tracing 139 | #if defined(LOGGING_LEVEL_TRACE) && LOGGING_LEVEL_TRACE 140 | #define LogTrace(fmt, ...) LOG_FORMAT(fmt, @"trace", ##__VA_ARGS__) 141 | #else 142 | #define LogTrace(...) 143 | #endif 144 | 145 | // Info logging - for general, non-performance affecting information messages 146 | #if defined(LOGGING_LEVEL_INFO) && LOGGING_LEVEL_INFO 147 | #define LogInfo(fmt, ...) LOG_FORMAT(fmt, @"info", ##__VA_ARGS__) 148 | #else 149 | #define LogInfo(...) 150 | #endif 151 | 152 | // Error logging - only when there is an error to be logged 153 | #if defined(LOGGING_LEVEL_ERROR) && LOGGING_LEVEL_ERROR 154 | #define LogError(fmt, ...) LOG_FORMAT(fmt, @"***ERROR***", ##__VA_ARGS__) 155 | #else 156 | #define LogError(...) 157 | #endif 158 | 159 | // Debug logging - use only temporarily for highlighting and tracking down problems 160 | #if defined(LOGGING_LEVEL_DEBUG) && LOGGING_LEVEL_DEBUG 161 | #define LogDebug(fmt, ...) LOG_FORMAT(fmt, @"DEBUG", ##__VA_ARGS__) 162 | #else 163 | #define LogDebug(...) 164 | #endif 165 | 166 | -------------------------------------------------------------------------------- /Classes/CC3Math/CC3Math.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CC3Math.h 3 | * 4 | * $Version: cocos3d 0.5-beta (eab7e651f462) on 2011-01-31 $ 5 | * Author: Bill Hollings 6 | * Copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved. 7 | * http://www.brenwill.com 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | * 27 | * http://en.wikipedia.org/wiki/MIT_License 28 | */ 29 | 30 | /** @file */ // Doxygen marker 31 | 32 | /* Base library of definitions and functions for operating in a 3D world. */ 33 | 34 | #import 35 | 36 | 37 | #pragma mark Basic math support 38 | 39 | #define M_SQRT3 1.732050807568877f /* sqrt(3) */ 40 | #define kCircleDegreesPeriod 360.0f 41 | 42 | /** Conversion between degrees and radians. */ 43 | #define DegreesToRadiansFactor 0.017453292519943f // PI / 180 44 | #define RadiansToDegreesFactor 57.29577951308232f // 180 / PI 45 | #define DegreesToRadians(D) ((D) * DegreesToRadiansFactor) 46 | #define RadiansToDegrees(R) ((R) * RadiansToDegreesFactor) 47 | 48 | /** Returns -1, 0 or +1 if the arguement is negative, zero or positive respectively. */ 49 | #define SIGN(A) ((A) < 0 ? -1 :((A) > 0 ? 1 : 0)) 50 | 51 | /** Returns the value clamped to be between the min and max values */ 52 | #define CLAMP(val, min, max) (MIN(MAX((val), (min)), (max))) 53 | 54 | /** Returns a weighted average of the two values, where weight is between zero and one, inclusive. */ 55 | #define WAVG(val1, val2, weight) ((val1) + (((val2) - (val1)) * CLAMP(weight, 0.0, 1.0))) 56 | 57 | /** 58 | * Returns the positive modulo remainder of value divided by period. 59 | * 60 | * This function is similar to fmod() (and uses fmod()), but converts a negative result 61 | * into a positive value that is the same distance away from the end of the cycle as the 62 | * result was below zero. In this sense, this function behaves like the numbers on a clock, 63 | * and Cyclic(-2.0, 12.0) will return 10.0 rather than -2.0. 64 | */ 65 | float Cyclic(float value, float period); 66 | 67 | /** 68 | * Returns the difference between the specified minuend and subtrahend, in terms of the 69 | * minimum difference within the specified periodic cycle. Therefore, the result may be 70 | * positive or negative, but will always be between (+period/2) and (-period/2). 71 | * 72 | * For example, for the numbers on a compass, the period is 360, and 73 | * CyclicDifference(350, 10, 360) will yield -20 (ie- the smallest change from 10 degrees 74 | * to 350 degrees is -20 degrees) rather than +340 (from simple subtraction). Similarly, 75 | * CyclicDifference(10, 350, 360) will yield +20 (ie- the smallest change from 350 degrees 76 | * to 10 degrees is +20 degrees) rather than -340 (from simple subtraction). 77 | */ 78 | float CyclicDifference(float minuend, float subtrahend, float period); 79 | 80 | 81 | #pragma mark Random number generation 82 | 83 | /** Returns a random unsigned integer over the full unsigned interger range (between 0 and 0xFFFFFFFF). */ 84 | static unsigned int RandomUInt(); 85 | 86 | /** Returns a random unsigned integer between 0 inclusive and the specified max exclusive. */ 87 | unsigned int RandomUIntBelow(unsigned int max); 88 | 89 | /** Returns a random double between 0.0 inclusive and 1.0 exclusive. */ 90 | static double RandomDouble(); 91 | 92 | /** Returns a random double between the specified min inclusive and the specified max exclusive. */ 93 | double RandomDoubleBetween(double min, double max); 94 | 95 | /** Returns a random float between 0.0 inclusive and 1.0 exclusive. */ 96 | static float RandomFloat(); 97 | 98 | /** Returns a random float between the specified min inclusive and the specified max exclusive. */ 99 | float RandomFloatBetween(float min, float max); 100 | 101 | 102 | // ANTON: Added from ccMacros 103 | 104 | #define CCRANDOM_MINUS1_1() ((random() / (float)0x3fffffff )-1.0f) 105 | 106 | /** @def CCRANDOM_0_1 107 | returns a random float between 0 and 1 108 | */ 109 | #define CCRANDOM_0_1() ((random() / (float)0x7fffffff )) 110 | -------------------------------------------------------------------------------- /Classes/CC3Math/CC3Math.m: -------------------------------------------------------------------------------- 1 | /* 2 | * CC3Math.m 3 | * 4 | * $Version: cocos3d 0.5-beta (eab7e651f462) on 2011-01-31 $ 5 | * Author: Bill Hollings 6 | * Copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved. 7 | * http://www.brenwill.com 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | * 27 | * http://en.wikipedia.org/wiki/MIT_License 28 | * 29 | * See header file CC3Math.h for full API documentation. 30 | */ 31 | 32 | #import "CC3Foundation.h" 33 | #import 34 | 35 | 36 | #pragma mark Basic math support 37 | 38 | float Cyclic(float value, float period) { 39 | float modVal = fmod(value, period); 40 | return (modVal < 0.0) ? (modVal + period) : modVal; 41 | } 42 | 43 | float CyclicDifference(float minuend, float subtrahend, float period) { 44 | float semiPeriod = period / 2.0; 45 | float diff = minuend - subtrahend; 46 | // If the difference is outside the range (period/2 >= diff >= -period/2), 47 | // adjust it so that it takes the difference in the other direction to 48 | // arrive at a smaller change. 49 | if(diff > semiPeriod) { 50 | diff -= period; 51 | } else if(diff < -semiPeriod) { 52 | diff += period; 53 | } 54 | return diff; 55 | } 56 | 57 | 58 | #pragma mark Random number generation 59 | 60 | #define kRandomUIntMax 0x100000000LL 61 | 62 | 63 | static unsigned int RandomUInt() { 64 | return arc4random(); 65 | } 66 | 67 | unsigned int RandomUIntBelow(unsigned int max) { 68 | return RandomUInt() % max; 69 | } 70 | 71 | static double RandomDouble() { 72 | return (double)RandomUInt() / (double)kRandomUIntMax; 73 | } 74 | 75 | double RandomDoubleBetween(double min, double max) { 76 | return min + (RandomDouble() * (max - min)); 77 | } 78 | 79 | static float RandomFloat() { 80 | return (float)RandomDouble(); 81 | } 82 | 83 | float RandomFloatBetween(float min, float max) { 84 | return (float)RandomDoubleBetween(min, max); 85 | } 86 | 87 | -------------------------------------------------------------------------------- /Classes/CC3Math/CC3MathInclude.h: -------------------------------------------------------------------------------- 1 | // 2 | // CC3MathInclude.h 3 | // RenderEngine 4 | // 5 | // Created by Anton Holmquist on 9/14/11. 6 | // Copyright 2011 Monterosa. All rights reserved. 7 | // 8 | 9 | // Anton includes 10 | 11 | #import "CC3Foundation.h" 12 | #import "CC3GLMatrix.h" 13 | #import "CC3Kazmath.h" 14 | #import "CC3Math.h" 15 | 16 | #import "NSValue+CC3Types.h" -------------------------------------------------------------------------------- /Classes/CC3Math/NSValue+CC3Types.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSValue+CC3Types.h 3 | // Transformers 4 | // 5 | // Created by Anton Holmquist on 9/30/11. 6 | // Copyright 2011 Monterosa. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "CC3Foundation.h" 11 | 12 | @interface NSValue (CC3Types) 13 | 14 | + (NSValue*)valueWithCC3Vector:(CC3Vector)v; 15 | - (CC3Vector)CC3VectorValue; 16 | 17 | + (NSValue*)valueWithCC3Vector4:(CC3Vector4)v; 18 | - (CC3Vector4)CC3Vector4Value; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /Classes/CC3Math/NSValue+CC3Types.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSValue+CC3Types.m 3 | // Transformers 4 | // 5 | // Created by Anton Holmquist on 9/30/11. 6 | // Copyright 2011 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "NSValue+CC3Types.h" 10 | 11 | @implementation NSValue (CC3Types) 12 | 13 | + (NSValue*)valueWithCC3Vector:(CC3Vector)v { 14 | return [NSValue valueWithBytes:&v objCType:@encode(CC3Vector)]; 15 | } 16 | 17 | - (CC3Vector)CC3VectorValue { 18 | CC3Vector v; 19 | [self getValue:&v]; 20 | return v; 21 | } 22 | 23 | + (NSValue*)valueWithCC3Vector4:(CC3Vector4)v { 24 | return [NSValue valueWithBytes:&v objCType:@encode(CC3Vector4)]; 25 | } 26 | 27 | - (CC3Vector4)CC3Vector4Value { 28 | CC3Vector4 v; 29 | [self getValue:&v]; 30 | return v; 31 | } 32 | 33 | @end 34 | 35 | 36 | -------------------------------------------------------------------------------- /Classes/CC3Math/ccTypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org 3 | * 4 | * Copyright (c) 2008-2010 Ricardo Quesada 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | 26 | /** 27 | @file 28 | cocos2d (cc) types 29 | */ 30 | 31 | #import 32 | #import 33 | 34 | #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 35 | #import // CGPoint 36 | #endif 37 | 38 | //#import "Platforms/CCGL.h" 39 | #import 40 | #include 41 | #include 42 | 43 | /** RGB color composed of bytes 3 bytes 44 | @since v0.8 45 | */ 46 | typedef struct _ccColor3B 47 | { 48 | GLubyte r; 49 | GLubyte g; 50 | GLubyte b; 51 | } ccColor3B; 52 | 53 | //! helper macro that creates an ccColor3B type 54 | static inline ccColor3B 55 | ccc3(const GLubyte r, const GLubyte g, const GLubyte b) 56 | { 57 | ccColor3B c = {r, g, b}; 58 | return c; 59 | } 60 | //ccColor3B predefined colors 61 | //! White color (255,255,255) 62 | static const ccColor3B ccWHITE = {255,255,255}; 63 | //! Yellow color (255,255,0) 64 | static const ccColor3B ccYELLOW = {255,255,0}; 65 | //! Blue color (0,0,255) 66 | static const ccColor3B ccBLUE = {0,0,255}; 67 | //! Green Color (0,255,0) 68 | static const ccColor3B ccGREEN = {0,255,0}; 69 | //! Red Color (255,0,0,) 70 | static const ccColor3B ccRED = {255,0,0}; 71 | //! Magenta Color (255,0,255) 72 | static const ccColor3B ccMAGENTA = {255,0,255}; 73 | //! Black Color (0,0,0) 74 | static const ccColor3B ccBLACK = {0,0,0}; 75 | //! Orange Color (255,127,0) 76 | static const ccColor3B ccORANGE = {255,127,0}; 77 | //! Gray Color (166,166,166) 78 | static const ccColor3B ccGRAY = {166,166,166}; 79 | 80 | /** RGBA color composed of 4 bytes 81 | @since v0.8 82 | */ 83 | typedef struct _ccColor4B 84 | { 85 | GLubyte r; 86 | GLubyte g; 87 | GLubyte b; 88 | GLubyte a; 89 | } ccColor4B; 90 | //! helper macro that creates an ccColor4B type 91 | static inline ccColor4B 92 | ccc4(const GLubyte r, const GLubyte g, const GLubyte b, const GLubyte o) 93 | { 94 | ccColor4B c = {r, g, b, o}; 95 | return c; 96 | } 97 | 98 | 99 | /** RGBA color composed of 4 floats 100 | @since v0.8 101 | */ 102 | typedef struct _ccColor4F { 103 | GLfloat r; 104 | GLfloat g; 105 | GLfloat b; 106 | GLfloat a; 107 | } ccColor4F; 108 | 109 | /** Returns a ccColor4F from a ccColor3B. Alpha will be 1. 110 | @since v0.99.1 111 | */ 112 | static inline ccColor4F ccc4FFromccc3B(ccColor3B c) 113 | { 114 | return (ccColor4F){c.r/255.f, c.g/255.f, c.b/255.f, 1.f}; 115 | } 116 | 117 | /** Returns a ccColor4F from a ccColor4B. 118 | @since v0.99.1 119 | */ 120 | static inline ccColor4F ccc4FFromccc4B(ccColor4B c) 121 | { 122 | return (ccColor4F){c.r/255.f, c.g/255.f, c.b/255.f, c.a/255.f}; 123 | } 124 | 125 | /** returns YES if both ccColor4F are equal. Otherwise it returns NO. 126 | @since v0.99.1 127 | */ 128 | static inline BOOL ccc4FEqual(ccColor4F a, ccColor4F b) 129 | { 130 | return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; 131 | } 132 | 133 | /** A vertex composed of 2 GLfloats: x, y 134 | @since v0.8 135 | */ 136 | typedef struct _ccVertex2F 137 | { 138 | GLfloat x; 139 | GLfloat y; 140 | } ccVertex2F; 141 | 142 | /** A vertex composed of 2 floats: x, y 143 | @since v0.8 144 | */ 145 | typedef struct _ccVertex3F 146 | { 147 | GLfloat x; 148 | GLfloat y; 149 | GLfloat z; 150 | } ccVertex3F; 151 | 152 | /** A texcoord composed of 2 floats: u, y 153 | @since v0.8 154 | */ 155 | typedef struct _ccTex2F { 156 | GLfloat u; 157 | GLfloat v; 158 | } ccTex2F; 159 | 160 | 161 | //! Point Sprite component 162 | typedef struct _ccPointSprite 163 | { 164 | ccVertex2F pos; // 8 bytes 165 | ccColor4B color; // 4 bytes 166 | GLfloat size; // 4 bytes 167 | } ccPointSprite; 168 | 169 | //! A 2D Quad. 4 * 2 floats 170 | typedef struct _ccQuad2 { 171 | ccVertex2F tl; 172 | ccVertex2F tr; 173 | ccVertex2F bl; 174 | ccVertex2F br; 175 | } ccQuad2; 176 | 177 | 178 | //! A 3D Quad. 4 * 3 floats 179 | typedef struct _ccQuad3 { 180 | ccVertex3F bl; 181 | ccVertex3F br; 182 | ccVertex3F tl; 183 | ccVertex3F tr; 184 | } ccQuad3; 185 | 186 | //! A 2D grid size 187 | typedef struct _ccGridSize 188 | { 189 | NSInteger x; 190 | NSInteger y; 191 | } ccGridSize; 192 | 193 | //! helper function to create a ccGridSize 194 | static inline ccGridSize 195 | ccg(const NSInteger x, const NSInteger y) 196 | { 197 | ccGridSize v = {x, y}; 198 | return v; 199 | } 200 | 201 | //! a Point with a vertex point, a tex coord point and a color 4B 202 | typedef struct _ccV2F_C4B_T2F 203 | { 204 | //! vertices (2F) 205 | ccVertex2F vertices; 206 | //! colors (4B) 207 | ccColor4B colors; 208 | //! tex coords (2F) 209 | ccTex2F texCoords; 210 | } ccV2F_C4B_T2F; 211 | 212 | //! a Point with a vertex point, a tex coord point and a color 4F 213 | typedef struct _ccV2F_C4F_T2F 214 | { 215 | //! vertices (2F) 216 | ccVertex2F vertices; 217 | //! colors (4F) 218 | ccColor4F colors; 219 | //! tex coords (2F) 220 | ccTex2F texCoords; 221 | } ccV2F_C4F_T2F; 222 | 223 | //! a Point with a vertex point, a tex coord point and a color 4B 224 | typedef struct _ccV3F_C4B_T2F 225 | { 226 | //! vertices (3F) 227 | ccVertex3F vertices; // 12 bytes 228 | // char __padding__[4]; 229 | 230 | //! colors (4B) 231 | ccColor4B colors; // 4 bytes 232 | // char __padding2__[4]; 233 | 234 | // tex coords (2F) 235 | ccTex2F texCoords; // 8 byts 236 | } ccV3F_C4B_T2F; 237 | 238 | //! 4 ccVertex2FTex2FColor4B Quad 239 | typedef struct _ccV2F_C4B_T2F_Quad 240 | { 241 | //! bottom left 242 | ccV2F_C4B_T2F bl; 243 | //! bottom right 244 | ccV2F_C4B_T2F br; 245 | //! top left 246 | ccV2F_C4B_T2F tl; 247 | //! top right 248 | ccV2F_C4B_T2F tr; 249 | } ccV2F_C4B_T2F_Quad; 250 | 251 | //! 4 ccVertex3FTex2FColor4B 252 | typedef struct _ccV3F_C4B_T2F_Quad 253 | { 254 | //! top left 255 | ccV3F_C4B_T2F tl; 256 | //! bottom left 257 | ccV3F_C4B_T2F bl; 258 | //! top right 259 | ccV3F_C4B_T2F tr; 260 | //! bottom right 261 | ccV3F_C4B_T2F br; 262 | } ccV3F_C4B_T2F_Quad; 263 | 264 | //! 4 ccVertex2FTex2FColor4F Quad 265 | typedef struct _ccV2F_C4F_T2F_Quad 266 | { 267 | //! bottom left 268 | ccV2F_C4F_T2F bl; 269 | //! bottom right 270 | ccV2F_C4F_T2F br; 271 | //! top left 272 | ccV2F_C4F_T2F tl; 273 | //! top right 274 | ccV2F_C4F_T2F tr; 275 | } ccV2F_C4F_T2F_Quad; 276 | 277 | //! Blend Function used for textures 278 | typedef struct _ccBlendFunc 279 | { 280 | //! source blend function 281 | GLenum src; 282 | //! destination blend function 283 | GLenum dst; 284 | } ccBlendFunc; 285 | 286 | //! delta time type 287 | //! if you want more resolution redefine it as a double 288 | typedef float ccTime; 289 | //typedef double ccTime; 290 | -------------------------------------------------------------------------------- /Classes/REBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | 29 | /* REBuffer (VBO) 30 | 31 | Immutable. Possible to create a mutable version later. 32 | 33 | target options: 34 | GL_ARRAY_BUFFER 35 | GL_ELEMENT_ARRAY_BUFFER 36 | 37 | */ 38 | 39 | @interface REBuffer : NSObject { 40 | GLuint buffer; 41 | GLenum target; 42 | 43 | int length; 44 | } 45 | 46 | @property (nonatomic, readonly) GLuint buffer; 47 | @property (nonatomic, readonly) int length; 48 | 49 | - (id)initWithTarget:(GLenum)t data:(void*)data length:(int)length; // STATIC_DRAW is default usage 50 | - (id)initWithTarget:(GLenum)t data:(void*)data length:(int)length usage:(GLenum)usage; 51 | 52 | - (void)setSubData:(void*)data offset:(int)offset length:(int)length; // Writes data into buffer 53 | 54 | - (void)bind; 55 | 56 | + (void)unbind; // Unbinds all targets 57 | + (void)unbindArrayBuffer; 58 | + (void)unbindElementArrayBuffer; 59 | 60 | /* 61 | STATIC_DRAW The data store contents will be specified once by the application, and used many times as the source for GL drawing commands. 62 | DYNAMIC_DRAW The data store contents will be respecified repeatedly by the ap- plication, and used many times as the source for GL drawing commands. 63 | STREAM_DRAW The data store contents will be specified once by the application, and used at most a few times as the source of a GL drawing command. 64 | */ 65 | 66 | @end 67 | 68 | 69 | /* 70 | @interface REMutableBuffer 71 | 72 | @end 73 | */ -------------------------------------------------------------------------------- /Classes/REBuffer.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REBuffer.h" 28 | #import "REGLStateManager.h" 29 | 30 | @implementation REBuffer 31 | 32 | @synthesize buffer, length; 33 | 34 | - (id)initWithTarget:(GLenum)t data:(void*)d length:(int)l { 35 | return [self initWithTarget:t data:d length:l usage:GL_STATIC_DRAW]; 36 | } 37 | 38 | - (id)initWithTarget:(GLenum)t data:(void*)data length:(int)l usage:(GLenum)usage { 39 | if ((self = [super init])) { 40 | target = t; 41 | length = l; 42 | glGenBuffers(1, &buffer); 43 | //glBindBuffer(target, buffer); 44 | [self bind]; 45 | glBufferData(target, length, (data ? data : NULL), usage); 46 | } return self; 47 | } 48 | 49 | - (void)dealloc { 50 | 51 | // If buffer is bound, it will be unbound when deleted. Therefore, we set state to correspond before 52 | if (target == GL_ARRAY_BUFFER && [[REGLStateManager sharedManager] arrayBufferBinding] == buffer) { 53 | [[REGLStateManager sharedManager] setArrayBufferBinding:0]; 54 | } else if (target == GL_ELEMENT_ARRAY_BUFFER && [[REGLStateManager sharedManager] elementArrayBufferBinding] == buffer) { 55 | [[REGLStateManager sharedManager] setElementArrayBufferBinding:0]; 56 | } 57 | 58 | glDeleteBuffers(1, &buffer); 59 | 60 | [super dealloc]; 61 | } 62 | 63 | - (void)setSubData:(void*)data offset:(int)offset length:(int)theLength { 64 | [self bind]; 65 | glBufferSubData(target, offset, theLength, data); 66 | } 67 | 68 | #pragma mark - Binding 69 | 70 | - (void)bind { 71 | 72 | //glBindBuffer(target, buffer); 73 | if (target == GL_ARRAY_BUFFER) { 74 | [[REGLStateManager sharedManager] setArrayBufferBinding:buffer]; 75 | } else if (target == GL_ELEMENT_ARRAY_BUFFER) { 76 | [[REGLStateManager sharedManager] setElementArrayBufferBinding:buffer]; 77 | } 78 | } 79 | 80 | 81 | 82 | + (void)unbind { 83 | 84 | [self unbindArrayBuffer]; 85 | [self unbindElementArrayBuffer]; 86 | 87 | // glBindBuffer(GL_ARRAY_BUFFER, 0); 88 | // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 89 | } 90 | 91 | + (void)unbindArrayBuffer { 92 | [[REGLStateManager sharedManager] setArrayBufferBinding:0]; 93 | } 94 | 95 | + (void)unbindElementArrayBuffer { 96 | [[REGLStateManager sharedManager] setElementArrayBufferBinding:0]; 97 | } 98 | 99 | @end 100 | -------------------------------------------------------------------------------- /Classes/RECache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | 28 | #import 29 | 30 | @class REWavefrontMesh, RETexture, RETexture2D, RETextureCubeMap; 31 | 32 | @interface REMeshCache : NSObject 33 | 34 | + (REWavefrontMesh*)meshNamed:(NSString*)filename; // Will cause the mesh to load into memory. Supports both .obj and .reobj. 35 | 36 | @end 37 | 38 | @interface RETextureCache : NSObject 39 | 40 | + (RETexture2D*)textureNamed:(NSString*)filename; 41 | + (RETextureCubeMap*)cubeTextureNamed:(NSString*)filename; 42 | 43 | @end 44 | 45 | @class REProgram; 46 | 47 | @interface REProgramCache : NSObject { 48 | NSMutableDictionary *dictionary; 49 | } 50 | 51 | + (REProgramCache*)sharedCache; 52 | 53 | - (REProgram*)programForKey:(id)key; 54 | - (void)setProgram:(REProgram*)program forKey:(id)key; 55 | 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /Classes/RECache.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RECache.h" 28 | #import "RETexture.h" 29 | #import "REWavefrontMesh.h" 30 | 31 | 32 | 33 | @implementation REMeshCache 34 | 35 | + (REWavefrontMesh*)meshNamed:(NSString*)filename { 36 | 37 | REWavefrontMesh *mesh = nil; 38 | 39 | static NSMutableDictionary *meshLoaderMeshes = nil; 40 | if (!meshLoaderMeshes) meshLoaderMeshes = [[NSMutableDictionary alloc] init]; 41 | 42 | mesh = [meshLoaderMeshes objectForKey:filename]; 43 | 44 | if (!mesh) { 45 | if ([[filename pathExtension] isEqual:@"obj"]) { 46 | mesh = [[[REWavefrontMesh alloc] initWithMeshNamed:filename] autorelease]; 47 | //NSAssert(YES, @"MeshCache: You shouldn't use this for .obj files. Convert them to .reobj first!"); 48 | } else if ([[filename pathExtension] isEqual:@"reobj"]) { 49 | NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:nil]]; 50 | mesh = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 51 | } 52 | 53 | [mesh createBuffers]; 54 | 55 | //NSAssert(mesh, @"MeshLoader: Can't load mesh: %@", filename); 56 | 57 | [meshLoaderMeshes setObject:mesh forKey:filename]; 58 | } 59 | 60 | return mesh; 61 | } 62 | 63 | @end 64 | 65 | @implementation RETextureCache 66 | 67 | + (RETexture2D*)textureNamed:(NSString*)filename { 68 | 69 | RETexture2D *texture = nil; 70 | 71 | static NSMutableDictionary *textures = nil; 72 | if (!textures) textures = [[NSMutableDictionary alloc] init]; 73 | 74 | texture = [textures objectForKey:filename]; 75 | 76 | if (!texture) { 77 | if ([[filename pathExtension] isEqual:@"png"] || [[filename pathExtension] isEqual:@"jpg"] ) { 78 | texture = [[[RETexture2D alloc] initWithImage:[UIImage imageNamed:filename]] autorelease]; 79 | } else if ([[filename pathExtension] isEqual:@"pvr"]) { 80 | NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil]; 81 | 82 | NSString *errorMessage = [NSString stringWithFormat:@"RETextureCache: Can't find file: %@ (%@)", filename, path]; 83 | NSAssert(path, errorMessage); 84 | 85 | NSData *data = [NSData dataWithContentsOfFile:path]; 86 | texture = [[[RETexture2D alloc] initWithPVRData:data] autorelease]; 87 | } 88 | 89 | 90 | [textures setObject:texture forKey:filename]; 91 | } 92 | 93 | return texture; 94 | } 95 | 96 | + (RETextureCubeMap*)cubeTextureNamed:(NSString*)filename { 97 | RETextureCubeMap *texture = nil; 98 | 99 | static NSMutableDictionary *textures = nil; 100 | if (!textures) textures = [[NSMutableDictionary alloc] init]; 101 | 102 | texture = [textures objectForKey:filename]; 103 | 104 | if (!texture) { 105 | if ([[filename pathExtension] isEqual:@"png"]) { 106 | // texture = [[[RETexture2D alloc] initWithImage:[UIImage imageNamed:filename]] autorelease]; 107 | 108 | UIImage *image = [UIImage imageNamed:filename]; 109 | 110 | texture = [[[RETextureCubeMap alloc] init] autorelease]; 111 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_NEGATIVE_X]; 112 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_POSITIVE_X]; 113 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_NEGATIVE_Y]; 114 | 115 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_POSITIVE_Y]; 116 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_NEGATIVE_Z]; 117 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_NEGATIVE_Z]; 118 | [texture setImage:image forTarget:GL_TEXTURE_CUBE_MAP_POSITIVE_Z]; 119 | 120 | 121 | } else if ([[filename pathExtension] isEqual:@"pvr"]) { 122 | /* 123 | NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil]; 124 | 125 | NSString *errorMessage = [NSString stringWithFormat:@"RETextureCache: Can't find file: %@ (%@)", filename, path]; 126 | NSAssert(path, errorMessage); 127 | 128 | NSData *data = [NSData dataWithContentsOfFile:path]; 129 | texture = [[[RETexture2D alloc] initWithPVRData:data] autorelease]; 130 | */ 131 | } 132 | 133 | 134 | [textures setObject:texture forKey:filename]; 135 | } 136 | 137 | return texture; 138 | } 139 | 140 | @end 141 | 142 | 143 | 144 | @implementation REProgramCache 145 | 146 | + (REProgramCache*)sharedCache { 147 | static REProgramCache *shared = nil; 148 | 149 | if (shared == nil) { 150 | shared = [[self alloc] init]; 151 | } return shared; 152 | } 153 | 154 | - (id)init { 155 | if (([super init])) { 156 | dictionary = [[NSMutableDictionary alloc] init]; 157 | } return self; 158 | } 159 | 160 | 161 | - (REProgram*)programForKey:(id)key { 162 | return [dictionary objectForKey:key]; 163 | } 164 | 165 | - (void)setProgram:(REProgram*)program forKey:(id)key { 166 | [dictionary setObject:program forKey:key]; 167 | } 168 | 169 | @end 170 | 171 | -------------------------------------------------------------------------------- /Classes/RECamera.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | 29 | @class REGLView; 30 | 31 | typedef enum { 32 | kRECameraProjectionOrthographic, 33 | kRECameraProjectionPerspective, 34 | } kRECameraProjection; 35 | 36 | /** RECamera is defines the OpenGL camera properties. 37 | */ 38 | 39 | @interface RECamera : RENode { 40 | kRECameraProjection projection; 41 | 42 | float frustumLeft, frustumRight; 43 | float frustumBottom, frustumTop; 44 | float frustumNear, frustumFar; 45 | CC3Vector upDirection; 46 | CC3Vector lookDirection_; 47 | 48 | CC3GLMatrix *viewMatrix; 49 | CC3GLMatrix *projectionMatrix; 50 | 51 | 52 | BOOL isViewMatrixDirty; 53 | BOOL isProjectionMatrixDirty; 54 | 55 | BOOL areFrustumPlanesDirty_; 56 | CC3Plane frustumNearPlane_; 57 | CC3Plane frustumFarPlane_; 58 | CC3Plane frustumTopPlane_; 59 | CC3Plane frustumBottomPlane_; 60 | CC3Plane frustumLeftPlane_; 61 | CC3Plane frustumRightPlane_; 62 | } 63 | 64 | /** Look direction of the camera */ 65 | @property (nonatomic, assign) CC3Vector lookDirection; 66 | 67 | /** Up direction of the camera */ 68 | @property (nonatomic, assign) CC3Vector upDirection; 69 | 70 | /** View frustum values */ 71 | @property (nonatomic, assign) float frustumLeft, frustumRight, frustumBottom, frustumTop, frustumNear, frustumFar; 72 | 73 | /** The view matrix transforms from world space to eye space. It is calculated from position, lookDirection and upDirection. */ 74 | @property (nonatomic, readonly) CC3GLMatrix *viewMatrix; 75 | 76 | /** The projection matrix transforms eye space to normal space. It is calculacted from the frustom values. */ 77 | @property (nonatomic, retain) CC3GLMatrix *projectionMatrix; 78 | 79 | /** Designated initializer. Select between perspective and orthographic projection. */ 80 | - (id)initWithProjection:(kRECameraProjection)p; 81 | 82 | - (void)setOrientationWithHorizontalAngle:(float)horizontalAngle verticalAngle:(float)verticalAngle sideTiltAngle:(float)sideTiltAngle; 83 | 84 | // Returns the near field point 85 | //- (CC3Vector)convert:(CGPoint)point fromView:(REGLView*)view; 86 | 87 | //-(CC3Ray)tmp_unprojectPoint:(CGPoint)cc2Point size:(CGSize)size; 88 | 89 | // Convert a 2d point in a view to a 3D ray 90 | -(CC3Ray)unprojectPoint:(CGPoint)cc2Point inView:(REGLView*)view; 91 | 92 | // If it's inside or intersects 93 | - (BOOL)boundingBoxIntersectsFrustum:(CC3BoundingBox)boundingBox; // Only reliable for orthographic projections for now 94 | 95 | /** Simple cull test for bounding boxes */ 96 | - (BOOL)globalBoundingBoxCanBeCulled:(CC3BoundingBox)globalBoundingBox; 97 | 98 | @end 99 | 100 | -------------------------------------------------------------------------------- /Classes/REDirector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | @class REScene; 28 | @class REGLView; 29 | 30 | /** REDirector manages draw cycles. It's responsible for preparing the view for drawing, traversing the node hierarchy and presenting the renderbuffer. 31 | */ 32 | 33 | 34 | @interface REDirector : NSObject { 35 | CADisplayLink *displayLink; 36 | NSString *displayLinkRunLoopMode; // Defaults to NSDefaultRunLoopMode. If using scroll views, you may want to change it 37 | 38 | REScene *scene; 39 | REGLView *view; 40 | 41 | BOOL running; 42 | 43 | BOOL showFPS; 44 | UIView *statsView; 45 | UILabel *statsLabel; 46 | 47 | NSMutableArray *drawDates; 48 | 49 | NSUInteger frameInterval; 50 | 51 | float dt; // Time since previous update 52 | NSTimeInterval lastDrawTime; 53 | 54 | BOOL isAppInBackground; // Keep track of that the app has entered background to prevent drawing. 55 | } 56 | 57 | /** The view we want to use for drawing. */ 58 | @property (nonatomic, retain) REGLView *view; 59 | 60 | /** The scene which acts as the root node of the scene grahp hierarchy. */ 61 | @property (nonatomic, retain) REScene *scene; 62 | 63 | /** When running is set to YES, drawing will be done regurarly according to frameInterval. */ 64 | @property (nonatomic, assign) BOOL running; 65 | 66 | /** Default to 2 which means 30 frame per second. */ 67 | @property (nonatomic, assign) NSUInteger frameInterval; 68 | 69 | /** showFPS is not implemented. */ 70 | @property (nonatomic, assign) BOOL showFPS; 71 | 72 | /** The run loop mode of the display link. */ 73 | @property (nonatomic, retain) NSString *displayLinkRunLoopMode; 74 | 75 | @property (nonatomic, readonly) float dt; 76 | 77 | /** Draws the content of scene to the view. */ 78 | - (void)draw; 79 | 80 | /** Returns snapshot of view content. */ 81 | - (UIImage*)snapshot; 82 | 83 | @end 84 | -------------------------------------------------------------------------------- /Classes/REGLStateManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | #import "REGLTypes.h" 29 | 30 | /* REGLState 31 | * ALWAYS use this to change supported state changes. 32 | */ 33 | 34 | 35 | 36 | @interface REGLStateManager : NSObject { 37 | GLint *activeTexture; // 1 38 | GLint *viewport; // 4 39 | //GLint *maxVertexAttribs; // 1 40 | GLint *maxVertexUniformVectors; 41 | 42 | GLboolean depthTestEnabled; 43 | GLboolean depthMask; 44 | GLboolean blendEnabled; 45 | GLboolean cullFaceEnabled; 46 | GLboolean stencilTestEnabled_; 47 | 48 | REGLColorMask colorMask_; 49 | REGLBlendFunc blendFunc; 50 | REGLStencilOp stencilOp_; 51 | REGLStencilFunc stencilFunc_; 52 | 53 | GLint arrayBufferBinding; 54 | GLint elementArrayBufferBinding; 55 | 56 | GLint currentProgram; 57 | GLint maxVertexAttribs; 58 | //GLboolean *vertexAttribArrayEnabled; 59 | 60 | GLint vertexArrayObjectBinding; 61 | 62 | GLint cullFace_; 63 | } 64 | 65 | + (REGLStateManager*)sharedManager; 66 | 67 | //@property (nonatomic, assign) GLint activeTexture; 68 | 69 | - (GLint)activeTexture; 70 | - (void)setActiveTexture:(GLint)v; 71 | 72 | - (GLint*)viewport; 73 | - (void)setViewport:(GLint)x y:(GLint)y width:(GLint)width height:(GLint)height; 74 | 75 | - (GLint)maxVertexAttribs; 76 | - (GLint)maxVertexUniformVectors; 77 | 78 | - (GLboolean)depthTestEnabled; 79 | - (void)setDepthTestEnabled:(GLboolean)yesOrNo; 80 | 81 | - (GLboolean)depthMask; 82 | - (void)setDepthMask:(GLboolean)yesOrNo; 83 | 84 | - (REGLColorMask)colorMask; 85 | - (void)setColorMask:(REGLColorMask)colorMask; 86 | 87 | - (GLboolean)blendEnabled; 88 | - (void)setBlendEnabled:(GLboolean)yesOrNo; 89 | 90 | - (GLboolean)cullFaceEnabled; 91 | - (void)setCullFaceEnabled:(GLboolean)yesOrNo; 92 | 93 | - (GLint)cullFace; 94 | - (void)setCullFace:(GLint)cullFace; 95 | 96 | - (GLboolean)stencilTestEnabled; 97 | - (void)setStencilTestEnabled:(GLboolean)yesOrNo; 98 | 99 | - (REGLStencilOp)stencilOp; 100 | - (void)setStencilOp:(REGLStencilOp)stencilOp; 101 | 102 | - (REGLStencilFunc)stencilFunc; 103 | - (void)setStencilFunc:(REGLStencilFunc)stencilFunc; 104 | 105 | /* 106 | stencilOp_ = REGLStencilOpMake(GL_KEEP, GL_KEEP, GL_KEEP); 107 | stencilFunc_ = REGLStencilFuncMake(GL_ALWAYS, 0, 0xFFFFFFFF); // 16 ones ~(~0 << 32) 108 | */ 109 | 110 | - (REGLBlendFunc)blendFunc; 111 | - (void)setBlendFunc:(REGLBlendFunc)func; 112 | 113 | - (void)setCurrentProgram:(GLint)program; 114 | 115 | - (GLint)arrayBufferBinding; 116 | - (GLint)elementArrayBufferBinding; 117 | - (void)setArrayBufferBinding:(GLint)buffer; 118 | - (void)setElementArrayBufferBinding:(GLint)buffer; 119 | 120 | //- (void)setVertexAttribArray:(GLuint)index enabled:(GLboolean)enabled; DEPRECATED? 121 | 122 | - (GLint)vertexArrayObjectBinding; 123 | - (void)setVertexArrayObjectBinding:(GLint)object; 124 | 125 | @end 126 | -------------------------------------------------------------------------------- /Classes/REGLTypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | typedef GLboolean REGLboolean; 28 | 29 | typedef struct { 30 | GLboolean red, green, blue, alpha; 31 | } REGLColorMask; 32 | 33 | typedef struct { 34 | GLenum sfactor, dfactor; 35 | } REGLBlendFunc; 36 | 37 | typedef struct { 38 | GLenum func; 39 | GLint ref; 40 | GLuint mask; 41 | } REGLStencilFunc; 42 | 43 | typedef struct { 44 | GLenum sfail, dpfail, dppass; 45 | } REGLStencilOp; 46 | 47 | REGLColorMask REGLColorMaskMake(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); 48 | BOOL REGLColorMaskEqualToColorMask(REGLColorMask colorMask1, REGLColorMask colorMask2); 49 | 50 | REGLBlendFunc REGLBlendFuncMake(GLenum src, GLenum dst); 51 | REGLStencilFunc REGLStencilFuncMake(GLenum func, GLint ref, GLuint mask); 52 | BOOL REGLStencilFuncEqualToStencilFunc(REGLStencilFunc stencilFunc1, REGLStencilFunc stencilFunc2); 53 | 54 | REGLStencilOp REGLStencilOpMake(GLenum sfail, GLenum dpfail, GLenum dppass); 55 | BOOL REGLStencilOpEqualToStencilOp(REGLStencilOp stencilOp1, REGLStencilOp stencilOp2); 56 | -------------------------------------------------------------------------------- /Classes/REGLTypes.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REGLTypes.h" 28 | 29 | REGLColorMask REGLColorMaskMake(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { 30 | REGLColorMask a; 31 | a.red = red; 32 | a.green = green; 33 | a.blue = blue; 34 | a.alpha = alpha; 35 | return a; 36 | } 37 | 38 | BOOL REGLColorMaskEqualToColorMask(REGLColorMask colorMask1, REGLColorMask colorMask2) { 39 | return 40 | colorMask1.red == colorMask2.red && 41 | colorMask1.green == colorMask2.green && 42 | colorMask1.blue == colorMask2.blue && 43 | colorMask1.alpha == colorMask2.alpha; 44 | } 45 | 46 | REGLBlendFunc REGLBlendFuncMake(GLenum sfactor, GLenum dfactor) { 47 | REGLBlendFunc a; 48 | a.sfactor = sfactor; 49 | a.dfactor = dfactor; 50 | return a; 51 | } 52 | 53 | REGLStencilFunc REGLStencilFuncMake(GLenum func, GLint ref, GLuint mask) { 54 | REGLStencilFunc a; 55 | a.func = func; 56 | a.ref = ref; 57 | a.mask = mask; 58 | return a; 59 | } 60 | 61 | BOOL REGLStencilFuncEqualToStencilFunc(REGLStencilFunc stencilFunc1, REGLStencilFunc stencilFunc2) { 62 | return stencilFunc1.func == stencilFunc2.func && stencilFunc1.ref == stencilFunc2.ref && stencilFunc1.mask == stencilFunc2.mask; 63 | } 64 | 65 | REGLStencilOp REGLStencilOpMake(GLenum sfail, GLenum dpfail, GLenum dppass) { 66 | REGLStencilOp a; 67 | a.sfail = sfail; 68 | a.dpfail = dpfail; 69 | a.dppass = dppass; 70 | return a; 71 | } 72 | 73 | BOOL REGLStencilOpEqualToStencilOp(REGLStencilOp stencilOp1, REGLStencilOp stencilOp2) { 74 | return stencilOp1.sfail == stencilOp2.sfail && stencilOp1.dpfail == stencilOp2.dpfail && stencilOp1.dppass == stencilOp2.dppass; 75 | } 76 | -------------------------------------------------------------------------------- /Classes/REGLView.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | 29 | @interface REGLView : UIView { 30 | 31 | // Normal framebuffer 32 | GLuint framebuffer; 33 | GLuint colorRenderbuffer; 34 | //GLuint depthRenderbuffer; 35 | GLuint depthStencilRenderbuffer_; // Combined depth and stencil render buffer 36 | 37 | // Multisample framebuffers 38 | GLuint multisampleFramebuffer; // This is used for sampling 39 | GLuint multisampleColorRenderbuffer; 40 | //GLuint multisampleDepthRenderbuffer; 41 | GLuint multisampleDepthStencilRenderbuffer_; 42 | 43 | CGRect viewport; 44 | 45 | BOOL multisampling_; 46 | } 47 | 48 | /** The framebuffer associated with the view */ 49 | @property (nonatomic, readonly) GLuint framebuffer; 50 | 51 | /** If multisampling is used, this is the framebuffer associated with the view */ 52 | @property (nonatomic, readonly) GLuint multisampleFramebuffer; // Used for multisampling 53 | 54 | /** The color renderbuffer associated with the view */ 55 | @property (nonatomic, readonly) GLuint colorRenderbuffer; 56 | @property (nonatomic, readonly) GLuint multisampleColorRenderbuffer; 57 | 58 | @property (nonatomic, readonly) CGRect viewport; 59 | @property (nonatomic, readonly) BOOL multisampling; 60 | 61 | - (id)initWithFrame:(CGRect)frame; 62 | - (id)initWithFrame:(CGRect)frame colorFormat:(NSString*)colorFormat; // kEAGLColorFormatRGBA8, kEAGLColorFormatRGB565 63 | - (id)initWithFrame:(CGRect)frame colorFormat:(NSString*)colorFormat multisampling:(BOOL)multisampling; 64 | - (id)initWithFrame:(CGRect)frame colorFormat:(NSString*)colorFormat multisampling:(BOOL)multisampling scale:(float)scale; 65 | 66 | - (void)bindFramebuffer; 67 | 68 | @end 69 | -------------------------------------------------------------------------------- /Classes/REGLView.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REGLView.h" 28 | #import 29 | 30 | @interface REGLView () 31 | 32 | @end 33 | 34 | @implementation REGLView 35 | 36 | @synthesize framebuffer, colorRenderbuffer; 37 | @synthesize viewport; 38 | @synthesize multisampleFramebuffer; 39 | 40 | @synthesize multisampling = multisampling_; 41 | @synthesize multisampleColorRenderbuffer = multisampleColorRenderbuffer_; 42 | //@synthesize stencilRenderBuffer = stencilRenderBuffer_; 43 | //@synthesize depthStencilRenderbuffer = depthStencilRenderbuffer_; 44 | 45 | + (Class)layerClass { 46 | return [CAEAGLLayer class]; 47 | } 48 | 49 | - (id)init { 50 | NSAssert(YES, @"REGLView: Use initWithFrame instead!"); 51 | return nil; 52 | } 53 | 54 | - (id)initWithFrame:(CGRect)frame { 55 | return [self initWithFrame:frame colorFormat:kEAGLColorFormatRGBA8]; 56 | } 57 | 58 | - (id)initWithFrame:(CGRect)frame colorFormat:(NSString*)colorFormat { 59 | return [self initWithFrame:frame colorFormat:colorFormat multisampling:NO]; 60 | } 61 | 62 | - (id)initWithFrame:(CGRect)frame colorFormat:(NSString*)colorFormat multisampling:(BOOL)multisampling { 63 | return [self initWithFrame:frame colorFormat:colorFormat multisampling:multisampling scale:[[UIScreen mainScreen] scale]]; 64 | } 65 | 66 | - (id)initWithFrame:(CGRect)frame colorFormat:(NSString*)colorFormat multisampling:(BOOL)multisampling scale:(float)scale { 67 | if ((self = [super initWithFrame:frame])) { 68 | 69 | //multisampling_ = TARGET_IPHONE_SIMULATOR ? NO : multisampling; // Never use multisampling on simulator (it's slow, but works) 70 | multisampling_ = multisampling; 71 | 72 | self.opaque = YES; // Should depend on colorFormat? 73 | 74 | if (scale == 0) { 75 | scale = [[UIScreen mainScreen] scale]; 76 | } 77 | 78 | CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 79 | eaglLayer.opaque = YES; // Should depend on colorFormat? 80 | self.contentScaleFactor = scale; 81 | 82 | // Select color format 83 | [(CAEAGLLayer *)self.layer setDrawableProperties: 84 | [NSDictionary dictionaryWithObject:colorFormat forKey:kEAGLDrawablePropertyColorFormat]]; 85 | 86 | glGenFramebuffers(1, &framebuffer); 87 | glGenRenderbuffers(1, &colorRenderbuffer); 88 | 89 | 90 | 91 | glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 92 | glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); 93 | 94 | // Create renderbuffer storage. 95 | [[EAGLContext currentContext] renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer]; 96 | 97 | // Set framebuffer color renderbuffer to the render buffer created by the eaglcontext above. 98 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); 99 | 100 | // With and height in pixels 101 | GLint width, height; 102 | 103 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width); 104 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height); 105 | 106 | // http://stackoverflow.com/questions/8149944/does-ios5-support-both-gl-stencil-index-and-gl-stencil-index8 107 | // In iOS 4.0 and later, separate stencil buffers are not supported. Use a combined depth/stencil buffer. 108 | //glBindBuffer(GL_RENDERBUFFER, stencilRenderBuffer_); 109 | //glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height); 110 | //glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilRenderBuffer_); 111 | 112 | if (multisampling_) { 113 | 114 | glGenFramebuffers(1, &multisampleFramebuffer); 115 | glBindFramebuffer(GL_FRAMEBUFFER, multisampleFramebuffer); 116 | 117 | glGenRenderbuffers(1, &multisampleColorRenderbuffer); 118 | glBindRenderbuffer(GL_RENDERBUFFER, multisampleColorRenderbuffer); 119 | glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA8_OES, width, height); 120 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, multisampleColorRenderbuffer); 121 | 122 | glGenRenderbuffers(1, &multisampleDepthStencilRenderbuffer_); 123 | glBindRenderbuffer(GL_RENDERBUFFER, multisampleDepthStencilRenderbuffer_); 124 | glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8_OES, width, height); 125 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, multisampleDepthStencilRenderbuffer_); 126 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, multisampleDepthStencilRenderbuffer_); 127 | 128 | 129 | 130 | viewport = CGRectMake(0, 0, 1 * width, 1 * height); // Hardcoded. Should we get these values from somewhere instead? 131 | } else { 132 | /* 133 | glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); 134 | glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); 135 | */ 136 | 137 | // If multisampling is used. DepthRenderbuffer will be in multisampleFramebuffer instead 138 | glGenRenderbuffers(1, &depthStencilRenderbuffer_); 139 | 140 | glBindRenderbuffer(GL_RENDERBUFFER, depthStencilRenderbuffer_); 141 | glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, width, height); 142 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthStencilRenderbuffer_); 143 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilRenderbuffer_); 144 | viewport = CGRectMake(0, 0, width, height); 145 | 146 | } 147 | 148 | 149 | //NSLog(@"error: %d", glGetError()); 150 | 151 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 152 | NSLog(@"GLView: Failed to make complete framebuffer object %x. This happens when we don't have current EAGLContext or if view size is zero.", glCheckFramebufferStatus(GL_FRAMEBUFFER)); 153 | } 154 | 155 | 156 | } return self; 157 | } 158 | 159 | - (void)dealloc { 160 | 161 | glDeleteFramebuffers(1, &framebuffer); 162 | glDeleteRenderbuffers(1, &colorRenderbuffer); 163 | //if (depthRenderbuffer) glDeleteRenderbuffers(1, &depthRenderbuffer); 164 | if (depthStencilRenderbuffer_) glDeleteRenderbuffers(1, &depthStencilRenderbuffer_); 165 | 166 | if (multisampleColorRenderbuffer) glDeleteRenderbuffers(1, &multisampleColorRenderbuffer); 167 | if (multisampleDepthStencilRenderbuffer_) glDeleteRenderbuffers(1, &multisampleDepthStencilRenderbuffer_); 168 | 169 | [super dealloc]; 170 | } 171 | 172 | - (void)bindFramebuffer { 173 | if (multisampling_) { 174 | glBindFramebuffer(GL_FRAMEBUFFER, multisampleFramebuffer); 175 | } else { 176 | glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 177 | } 178 | } 179 | 180 | @end 181 | -------------------------------------------------------------------------------- /Classes/REKeyframedMeshNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | 29 | @class REWavefrontMesh; 30 | @class RETexture2D; 31 | @class RETextureCubeMap; 32 | @class REMaterial; 33 | 34 | /* 35 | Mesh node that is specialized to use with keyframe animations, since it has the ability to tween vertex positions and normals. 36 | 37 | Works very tightly with REWavefrontMesh. Associated actions also works very closely with this. 38 | 39 | Responsibility: Draw specified wavefrontMesh, with additional tween. 40 | Animations are responsible for setting the meshes and tween 41 | */ 42 | 43 | @interface REKeyframedMeshNode : RENode { 44 | REWavefrontMesh *wavefrontMeshA_; 45 | REWavefrontMesh *wavefrontMeshB_; 46 | 47 | float meshTween; 48 | 49 | RETexture2D *texture; 50 | REMaterial *material_; 51 | 52 | RETextureCubeMap *environmentMap_; 53 | float environmentMixRatio_; 54 | 55 | RETexture2D *normalMap_; 56 | } 57 | 58 | @property (nonatomic, retain) REWavefrontMesh *wavefrontMeshA; 59 | @property (nonatomic, retain) REWavefrontMesh *wavefrontMeshB; // Used for keyframe animation 60 | 61 | @property (nonatomic, assign) float meshTween; 62 | 63 | @property (nonatomic, retain) RETexture2D *texture; 64 | @property (nonatomic, retain) RETexture2D *normalMap; // Converted bump map 65 | @property (nonatomic, retain) REMaterial *material; 66 | 67 | // Set this to use environment mapping 68 | @property (nonatomic, retain) RETextureCubeMap *environmentMap; 69 | 70 | // How much of the environment that is blended onto the object [0,1] 71 | @property (nonatomic, assign) float environmentMixRatio; 72 | 73 | // The default mesh sets bounding box and anchor coordinate. 74 | - (id)initWithDefaultMesh:(REWavefrontMesh*)mesh; 75 | 76 | 77 | @end 78 | -------------------------------------------------------------------------------- /Classes/RELight.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | #import "CC3Foundation.h" 29 | 30 | @interface RELight : NSObject { 31 | CC3Vector position_; 32 | CC3Vector4 ambientColor; 33 | CC3Vector4 diffuseColor; 34 | CC3Vector4 specularColor; 35 | } 36 | 37 | @property (nonatomic, assign) CC3Vector position; 38 | @property (nonatomic, assign) CC3Vector4 ambientColor; 39 | @property (nonatomic, assign) CC3Vector4 diffuseColor; 40 | @property (nonatomic, assign) CC3Vector4 specularColor; 41 | 42 | + (id)light; 43 | 44 | @end 45 | 46 | @interface REDirectionalLight : RELight { 47 | CC3Vector direction_; 48 | } 49 | 50 | @property (nonatomic, assign) CC3Vector direction; 51 | 52 | @end 53 | 54 | 55 | @interface REPointLight : RELight { 56 | 57 | CC3Vector attenuation; // constant, linear, quadratic 58 | } 59 | 60 | 61 | @property (nonatomic, assign) CC3Vector attenuation; 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /Classes/RELight.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RELight.h" 28 | 29 | @implementation RELight 30 | 31 | @synthesize ambientColor, diffuseColor, specularColor; 32 | 33 | @synthesize position = position_; 34 | 35 | + (id)light { 36 | return [[[self alloc] init] autorelease]; 37 | } 38 | 39 | - (id)init { 40 | if ((self = [super init])) { 41 | ambientColor = CC3Vector4Make(1, 1, 1, 1); 42 | diffuseColor = CC3Vector4Make(1, 1, 1, 1); 43 | specularColor = CC3Vector4Make(1, 1, 1, 1); 44 | } return self; 45 | } 46 | 47 | @end 48 | 49 | @implementation REDirectionalLight 50 | 51 | @synthesize direction = direction_; 52 | 53 | - (id)init { 54 | if ((self = [super init])) { 55 | direction_ = CC3VectorMake(1, 1, 1); 56 | } return self; 57 | } 58 | 59 | @end 60 | 61 | @implementation REPointLight 62 | 63 | @synthesize attenuation; 64 | 65 | @end -------------------------------------------------------------------------------- /Classes/REMaterial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #import 27 | #import "CC3Foundation.h" 28 | 29 | @interface REMaterial : NSObject { 30 | CC3Vector4 ambient_; 31 | CC3Vector4 diffuse_; 32 | CC3Vector4 specular_; 33 | float shininess_; 34 | } 35 | 36 | @property (nonatomic, assign) CC3Vector4 ambient; 37 | @property (nonatomic, assign) CC3Vector4 diffuse; 38 | @property (nonatomic, assign) CC3Vector4 specular; 39 | @property (nonatomic, assign) float shininess; 40 | 41 | + (id)material; 42 | + (id)materialWithAmbient:(float)ambient diffuse:(float)diffuse specular:(float)specular shininess:(float)shininess; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /Classes/REMaterial.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REMaterial.h" 28 | 29 | @implementation REMaterial 30 | 31 | @synthesize ambient = ambient_; 32 | @synthesize diffuse = diffuse_; 33 | @synthesize specular = specular_; 34 | @synthesize shininess = shininess_; 35 | 36 | + (id)material { 37 | return [[[[self class] alloc] init] autorelease]; 38 | } 39 | 40 | + (id)materialWithAmbient:(float)ambient diffuse:(float)diffuse specular:(float)specular shininess:(float)shininess { 41 | REMaterial *instance = [self material]; 42 | 43 | instance.ambient = CC3Vector4Make(ambient, ambient, ambient, 1); 44 | instance.diffuse = CC3Vector4Make(diffuse, diffuse, diffuse, 1); 45 | instance.specular = CC3Vector4Make(specular, specular, specular, 1); 46 | instance.shininess = shininess; 47 | 48 | return instance; 49 | } 50 | 51 | - (id)init { 52 | if ((self = [super init])) { 53 | ambient_ = CC3Vector4Make(0.2, 0.2, 0.2, 1.0); 54 | diffuse_ = CC3Vector4Make(0.8, 0.8, 0.8, 1.0); 55 | specular_ = CC3Vector4Make(0, 0, 0, 1); 56 | shininess_ = 0; 57 | } return self; 58 | } 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /Classes/RENSArrayAdditions.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | 29 | @interface NSArray (REAdditions) 30 | 31 | @end 32 | 33 | @interface NSMutableArray (REAdditions) 34 | 35 | + (id)RE_arrayUsingWeakReferences; 36 | + (id)RE_arrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity; 37 | 38 | @end 39 | 40 | -------------------------------------------------------------------------------- /Classes/RENSArrayAdditions.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENSArrayAdditions.h" 28 | 29 | @implementation NSArray (REAdditions) 30 | 31 | @end 32 | 33 | 34 | @implementation NSMutableArray (REAdditions) 35 | 36 | + (id)RE_arrayUsingWeakReferences { 37 | return [self RE_arrayUsingWeakReferencesWithCapacity:0]; 38 | } 39 | 40 | + (id)RE_arrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity { 41 | CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual}; 42 | // We create a weak reference array 43 | return (id)(CFArrayCreateMutable(0, capacity, &callbacks)); 44 | 45 | 46 | } 47 | 48 | @end -------------------------------------------------------------------------------- /Classes/RENSValueAdditions.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | #import "REGLTypes.h" 29 | 30 | @interface NSValue (REAdditions) 31 | 32 | // REGLColorMask 33 | + (NSValue*)valueWithREGLColorMask:(REGLColorMask)v; 34 | - (REGLColorMask)REGLColorMaskValue; 35 | 36 | // REGLBlendFunc 37 | + (NSValue*)valueWithREGLBlendFunc:(REGLBlendFunc)v; 38 | - (REGLBlendFunc)REGLBlencFuncValue; 39 | 40 | // REGLStencilFunc 41 | + (NSValue*)valueWithREGLStencilFunc:(REGLStencilFunc)v; 42 | - (REGLStencilFunc)REGLStencilFuncValue; 43 | 44 | // REGLStencilOp 45 | + (NSValue*)valueWithREGLStencilOp:(REGLStencilOp)v; 46 | - (REGLStencilOp)REGLStencilOpValue; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /Classes/RENSValueAdditions.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENSValueAdditions.h" 28 | 29 | @implementation NSValue (REAdditions) 30 | 31 | + (NSValue*)valueWithREGLColorMask:(REGLColorMask)v { 32 | return [NSValue valueWithBytes:&v objCType:@encode(REGLColorMask)]; 33 | } 34 | 35 | - (REGLColorMask)REGLColorMaskValue { 36 | REGLColorMask v; 37 | [self getValue:&v]; 38 | return v; 39 | } 40 | 41 | + (NSValue*)valueWithREGLBlendFunc:(REGLBlendFunc)v { 42 | return [NSValue valueWithBytes:&v objCType:@encode(REGLBlendFunc)]; 43 | } 44 | 45 | - (REGLBlendFunc)REGLBlencFuncValue { 46 | REGLBlendFunc v; 47 | [self getValue:&v]; 48 | return v; 49 | } 50 | 51 | + (NSValue*)valueWithREGLStencilFunc:(REGLStencilFunc)v { 52 | return [NSValue valueWithBytes:&v objCType:@encode(REGLStencilFunc)]; 53 | } 54 | 55 | - (REGLStencilFunc)REGLStencilFuncValue { 56 | REGLStencilFunc v; 57 | [self getValue:&v]; 58 | return v; 59 | } 60 | 61 | + (NSValue*)valueWithREGLStencilOp:(REGLStencilOp)v { 62 | return [NSValue valueWithBytes:&v objCType:@encode(REGLStencilOp)]; 63 | } 64 | 65 | - (REGLStencilOp)REGLStencilOpValue { 66 | REGLStencilOp v; 67 | [self getValue:&v]; 68 | return v; 69 | } 70 | 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /Classes/REProgram.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REShader.h" 28 | 29 | 30 | // http://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttrib.xml 31 | // http://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindAttribLocation.xml 32 | 33 | /* Local state of program: 34 | uniform/attribute locations. 35 | uniform values. 36 | 37 | Not local: 38 | attribute values. 39 | 40 | The binding between a generic vertex attribute index and a user-defined attribute variable in a vertex shader is part of the state of a program object, but the current value of the generic vertex attribute is not. The value of each generic vertex attribute is part of current state and it is maintained even if a different program object is used. 41 | */ 42 | 43 | @interface REProgram : NSObject { 44 | 45 | REShader *vertexShader, *fragmentShader; 46 | 47 | GLuint program; 48 | 49 | NSMutableDictionary *uniformLocations, *attribLocations; 50 | } 51 | 52 | @property (nonatomic, readonly) GLuint program; 53 | 54 | /** This is the preferred way to return a program in the node program static method. The returned program is cached. */ 55 | + (REProgram*)programWithVertexFilename:(NSString*)vertexFilename fragmentFilename:(NSString*)fragmentFilename; 56 | 57 | - (id)initWithVertexShader:(REShader*)vertexShader fragmentShader:(REShader*)fragmentShader; 58 | 59 | - (GLint)uniformLocation:(NSString*)name; 60 | - (GLint)attribLocation:(NSString*)name; 61 | 62 | - (void)use; // Sets to current 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /Classes/REProgram.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REProgram.h" 28 | #import "REGLStateManager.h" 29 | 30 | @interface REProgram () 31 | 32 | - (void)link; 33 | 34 | @end 35 | 36 | @implementation REProgram 37 | 38 | @synthesize program; 39 | 40 | + (REProgram*)programWithVertexFilename:(NSString*)vertexFilename fragmentFilename:(NSString*)fragmentFilename { 41 | return [[[[self class] alloc] initWithVertexShader:[[[REShader alloc] initWithType:GL_VERTEX_SHADER filename:vertexFilename] autorelease] 42 | fragmentShader:[[[REShader alloc] initWithType:GL_FRAGMENT_SHADER filename:fragmentFilename] autorelease]] autorelease]; 43 | } 44 | 45 | - (id)initWithVertexShader:(REShader*)v fragmentShader:(REShader*)f { 46 | if ((self = [super init])) { 47 | 48 | vertexShader = [v retain]; 49 | fragmentShader = [f retain]; 50 | 51 | uniformLocations = [[NSMutableDictionary alloc] init]; 52 | attribLocations = [[NSMutableDictionary alloc] init]; 53 | 54 | program = glCreateProgram(); 55 | 56 | [self link]; 57 | 58 | } return self; 59 | } 60 | 61 | - (void)dealloc { 62 | [vertexShader release]; 63 | [fragmentShader release]; 64 | [uniformLocations release]; 65 | [attribLocations release]; 66 | glDeleteProgram(program); 67 | [super dealloc]; 68 | } 69 | 70 | - (void)link { 71 | glAttachShader(program, vertexShader.shader); 72 | glAttachShader(program, fragmentShader.shader); 73 | glLinkProgram(program); 74 | 75 | //glValidateProgram(program); 76 | /* IT MAY BE TO EARLY TO VALIDATE HERE! DO IT BEFORE DRAWING IF YOU LIKE 77 | glValidateProgram() is validating current context state (the sampler uniforms, the draw framebuffer, the current vertex array state, etc--- anything that can cause a draw-time error when used with the program.) You shouldn't validate immediately after linking. 78 | 79 | Really you should validate right before drawing. And only in your debug build; it's a tool for you to debug with. 80 | */ 81 | 82 | GLint status; 83 | 84 | glGetProgramiv(program, GL_LINK_STATUS, &status); 85 | 86 | 87 | // LOG // TODO: Parse and log line from error? 88 | { 89 | GLint logLength; 90 | 91 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength); 92 | if (logLength > 0) { 93 | GLchar *log = (GLchar *)malloc(logLength); 94 | glGetProgramInfoLog(program, logLength, &logLength, log); 95 | NSLog(@"REProgram: Compile log:\n%s", log); 96 | free(log); 97 | } 98 | } 99 | } 100 | 101 | - (void)use { 102 | [[REGLStateManager sharedManager] setCurrentProgram:program]; 103 | } 104 | 105 | #pragma mark - Uniform/Attribute Locations 106 | 107 | - (GLint)uniformLocation:(NSString*)name { 108 | NSNumber *number = [uniformLocations objectForKey:name]; 109 | if (number == nil) { 110 | GLint location = glGetUniformLocation(program, [name UTF8String]); 111 | number = [NSNumber numberWithInt:location]; 112 | [uniformLocations setObject:number forKey:name]; 113 | } return [number intValue]; 114 | } 115 | 116 | - (GLint)attribLocation:(NSString*)name { 117 | NSNumber *number = [attribLocations objectForKey:name]; 118 | if (number == nil) { 119 | GLint location = glGetAttribLocation(program, [name UTF8String]); 120 | number = [NSNumber numberWithInt:location]; 121 | [attribLocations setObject:number forKey:name]; 122 | } return [number intValue]; 123 | } 124 | 125 | 126 | @end 127 | -------------------------------------------------------------------------------- /Classes/RERotator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | #import "CC3Foundation.h" 29 | 30 | @class CC3GLMatrix; 31 | 32 | #define kCC3MatrixIsNotDirty 0 33 | #define kCC3MatrixIsDirtyByRotation 1 34 | #define kCC3MatrixIsDirtyByQuaternion 2 35 | #define kCC3MatrixIsDirtyByAxisAngle 3 36 | 37 | 38 | @interface RERotator : NSObject { 39 | 40 | CC3GLMatrix* rotationMatrix; 41 | 42 | CC3Vector rotation; 43 | CC3Vector4 quaternion; 44 | CC3Vector rotationAxis; 45 | GLfloat rotationAngle; 46 | 47 | int matrixIsDirtyBy; 48 | BOOL isRotationDirty; 49 | BOOL isQuaternionDirty; 50 | BOOL isAxisAngleDirty; 51 | BOOL isQuaternionDirtyByAxisAngle; 52 | } 53 | 54 | - (id)init; 55 | - (id)initOnRotationMatrix:(CC3GLMatrix*)m; 56 | 57 | 58 | @property(nonatomic, retain) CC3GLMatrix* rotationMatrix; 59 | @property(nonatomic, assign) CC3Vector rotation; 60 | @property(nonatomic, assign) CC3Vector4 quaternion; 61 | @property(nonatomic, assign) CC3Vector rotationAxis; 62 | @property(nonatomic, assign) GLfloat rotationAngle; 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /Classes/RERotator.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RERotator.h" 28 | #import "CC3GLMatrix.h" 29 | 30 | @interface RERotator () 31 | 32 | - (void)applyRotation; 33 | 34 | - (void)ensureRotationFromMatrix; 35 | - (void)ensureQuaternionFromMatrix; 36 | - (void)ensureQuaternionFromAxisAngle; 37 | - (void)ensureAxisAngleFromQuaternion; 38 | 39 | @end 40 | 41 | @implementation RERotator 42 | 43 | - (id)init { 44 | return [self initOnRotationMatrix: [CC3GLMatrix identity]]; 45 | } 46 | 47 | 48 | - (id)initOnRotationMatrix:(CC3GLMatrix*) aGLMatrix { 49 | if ((self = [super init])) { 50 | self.rotationMatrix = aGLMatrix; 51 | rotation = kCC3VectorZero; 52 | quaternion = kCC3Vector4QuaternionIdentity; 53 | rotationAxis = kCC3VectorZero; 54 | rotationAngle = 0.0; 55 | isRotationDirty = NO; 56 | isQuaternionDirty = NO; 57 | isAxisAngleDirty = NO; 58 | isQuaternionDirtyByAxisAngle = NO; 59 | matrixIsDirtyBy = kCC3MatrixIsNotDirty; 60 | } 61 | return self; 62 | } 63 | 64 | - (void)dealloc { 65 | [rotationMatrix release]; 66 | [super dealloc]; 67 | } 68 | 69 | - (CC3Vector)rotation { 70 | [self ensureRotationFromMatrix]; 71 | return rotation; 72 | } 73 | 74 | - (void)setRotation:(CC3Vector)r { 75 | rotation = CC3VectorRotationModulo(r); 76 | 77 | isRotationDirty = NO; 78 | isAxisAngleDirty = YES; 79 | isQuaternionDirty = YES; 80 | isQuaternionDirtyByAxisAngle = NO; 81 | 82 | matrixIsDirtyBy = kCC3MatrixIsDirtyByRotation; 83 | } 84 | 85 | - (CC3Vector4)quaternion { 86 | [self ensureQuaternionFromAxisAngle]; 87 | [self ensureQuaternionFromMatrix]; 88 | return quaternion; 89 | } 90 | 91 | - (void)setQuaternion:(CC3Vector4)q { 92 | quaternion = q; 93 | 94 | isRotationDirty = YES; 95 | isAxisAngleDirty = YES; 96 | isQuaternionDirty = NO; 97 | isQuaternionDirtyByAxisAngle = NO; 98 | 99 | matrixIsDirtyBy = kCC3MatrixIsDirtyByQuaternion; 100 | } 101 | 102 | - (CC3Vector)rotationAxis { 103 | [self ensureAxisAngleFromQuaternion]; 104 | return rotationAxis; 105 | } 106 | 107 | - (void)setRotationAxis:(CC3Vector)axis { 108 | rotationAxis = axis; 109 | 110 | isRotationDirty = YES; 111 | isAxisAngleDirty = NO; 112 | isQuaternionDirty = NO; 113 | isQuaternionDirtyByAxisAngle = YES; 114 | 115 | matrixIsDirtyBy = kCC3MatrixIsDirtyByAxisAngle; 116 | } 117 | 118 | - (GLfloat)rotationAngle { 119 | [self ensureAxisAngleFromQuaternion]; 120 | return rotationAngle; 121 | } 122 | 123 | - (void)setRotationAngle:(GLfloat)angle { 124 | rotationAngle = Cyclic(angle, kCircleDegreesPeriod); 125 | 126 | isRotationDirty = YES; 127 | isAxisAngleDirty = NO; 128 | isQuaternionDirty = NO; 129 | isQuaternionDirtyByAxisAngle = YES; 130 | 131 | matrixIsDirtyBy = kCC3MatrixIsDirtyByAxisAngle; 132 | } 133 | 134 | 135 | #pragma mark - Rotation Matrix 136 | 137 | - (CC3GLMatrix*)rotationMatrix { 138 | [self applyRotation]; 139 | return rotationMatrix; 140 | } 141 | 142 | - (void)setRotationMatrix:(CC3GLMatrix*)aGLMatrix { 143 | id oldMtx = rotationMatrix; 144 | rotationMatrix = [aGLMatrix retain]; 145 | [oldMtx release]; 146 | 147 | isRotationDirty = YES; 148 | isQuaternionDirty = YES; 149 | isAxisAngleDirty = YES; 150 | isQuaternionDirtyByAxisAngle = NO; 151 | 152 | matrixIsDirtyBy = kCC3MatrixIsNotDirty; 153 | } 154 | 155 | #pragma mark - Apple 156 | 157 | - (void)applyRotation { 158 | switch (matrixIsDirtyBy) { 159 | case kCC3MatrixIsDirtyByRotation: 160 | [rotationMatrix populateFromRotation:self.rotation]; 161 | matrixIsDirtyBy = kCC3MatrixIsNotDirty; 162 | break; 163 | case kCC3MatrixIsDirtyByQuaternion: 164 | case kCC3MatrixIsDirtyByAxisAngle: 165 | [rotationMatrix populateFromQuaternion:self.quaternion]; 166 | matrixIsDirtyBy = kCC3MatrixIsNotDirty; 167 | break; 168 | default: 169 | break; 170 | } 171 | } 172 | 173 | #pragma mark - Ensure 174 | 175 | - (void)ensureRotationFromMatrix { 176 | if (isRotationDirty) { 177 | rotation = [self.rotationMatrix extractRotation]; 178 | isRotationDirty = NO; 179 | } 180 | } 181 | 182 | /** If needed, extracts and sets the quaternion from the encapsulated rotation matrix. */ 183 | - (void)ensureQuaternionFromMatrix { 184 | if (isQuaternionDirty) { 185 | quaternion = [self.rotationMatrix extractQuaternion]; 186 | isQuaternionDirty = NO; 187 | } 188 | } 189 | 190 | /** If needed, extracts and sets the quaternion from the encapsulated rotation axis and angle. */ 191 | - (void)ensureQuaternionFromAxisAngle { 192 | // If q is a quaternion, (rx, ry, rz) is the rotation axis, and ra is 193 | // the rotation angle (negated for right-handed coordinate system), then: 194 | // q = ( sin(ra/2)*rx, sin(ra/2)*ry, sin(ra/2)*rz, cos(ra/2) ) 195 | 196 | if (isQuaternionDirtyByAxisAngle) { 197 | GLfloat halfAngle = -DegreesToRadians(rotationAngle) / 2.0; // negate for RH system 198 | CC3Vector axis = CC3VectorScaleUniform(CC3VectorNormalize(rotationAxis), sinf(halfAngle)); 199 | quaternion = CC3Vector4FromCC3Vector(axis, cosf(halfAngle)); 200 | isQuaternionDirtyByAxisAngle = NO; 201 | } 202 | } 203 | 204 | /** 205 | * If needed, extracts and returns a rotation axis and angle from the encapsulated quaternion. 206 | * If the rotation angle is zero, the axis is undefined, and will be set to the zero vector. 207 | */ 208 | - (void)ensureAxisAngleFromQuaternion { 209 | // If q is a quaternion, (rx, ry, rz) is the rotation axis, and ra is 210 | // the rotation angle (negated for right-handed coordinate system), then: 211 | // q = ( sin(ra/2)*rx, sin(ra/2)*ry, sin(ra/2)*rz, cos(ra/2) ) 212 | // ra = acos(q.w) * 2 213 | // (rx, ry, rz) = (q.x, q.y, q.z) / sin(ra/2) 214 | 215 | if (isAxisAngleDirty) { 216 | CC3Vector4 q = CC3Vector4Normalize(self.quaternion); 217 | GLfloat halfAngle = acosf(q.w); 218 | rotationAngle = -RadiansToDegrees(halfAngle) * 2.0; // negate for RH system 219 | 220 | // If angle is zero, rotation axis is undefined. Use zero vector. 221 | if (halfAngle != 0.0f) { 222 | rotationAxis = CC3VectorScaleUniform(CC3VectorFromTruncatedCC3Vector4(q), 223 | (1.0 / sinf(halfAngle))); 224 | } else { 225 | rotationAxis = kCC3VectorZero; 226 | } 227 | isAxisAngleDirty = NO; 228 | } 229 | } 230 | 231 | 232 | 233 | 234 | @end 235 | -------------------------------------------------------------------------------- /Classes/REScene.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | 29 | /** REScene 30 | 31 | Scenes are used as base nodes by REDirector. 32 | */ 33 | 34 | 35 | @interface REScene : RENode { 36 | 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /Classes/REScene.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REScene.h" 28 | 29 | @implementation REScene 30 | 31 | - (id)init { 32 | if ((self = [super init])) { 33 | // Initialization code here. 34 | } return self; 35 | } 36 | 37 | - (void)dealloc { 38 | [super dealloc]; 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /Classes/REScheduler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | 29 | @interface REScheduler : NSObject { 30 | NSMutableArray *updateTargets; 31 | } 32 | 33 | + (REScheduler*)sharedScheduler; 34 | 35 | - (void)tick:(double)dt; // Will cause to call update targets. (dt isn't used) 36 | 37 | - (void)scheduleUpdateForTarget:(id)target; // Will call update: before every frame 38 | - (void)unscheduleUpdateForTarget:(id)target; 39 | 40 | 41 | @end 42 | 43 | @protocol RESchedulerTarget 44 | 45 | - (void)update:(double)dt; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Classes/REScheduler.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REScheduler.h" 28 | #import "RENSArrayAdditions.h" 29 | 30 | @implementation REScheduler 31 | 32 | + (REScheduler*)sharedScheduler { 33 | static REScheduler *singleton = nil; 34 | if (singleton == nil) { 35 | singleton = [[[self class] alloc] init]; 36 | } return singleton; 37 | } 38 | 39 | - (id)init { 40 | if ((self = [super init])) { 41 | //updateTargets = [[NSMutableArray alloc] init]; 42 | updateTargets = [[NSMutableArray RE_arrayUsingWeakReferences] retain]; 43 | } return self; 44 | } 45 | 46 | - (void)scheduleUpdateForTarget:(id)target { 47 | if ([updateTargets indexOfObjectIdenticalTo:target] == NSNotFound) { 48 | [updateTargets addObject:target]; 49 | //[target release]; 50 | //NSLog(@"scheduleUpdateForTarget: %p", target); 51 | } else { 52 | //NSLog(@"REScheduler: Warning: Trying to schedule update for target twice"); 53 | } 54 | } 55 | 56 | - (void)unscheduleUpdateForTarget:(id)target { 57 | if ([updateTargets indexOfObjectIdenticalTo:target] != NSNotFound) { 58 | //[target retain]; 59 | [updateTargets removeObjectIdenticalTo:target]; 60 | //NSLog(@"unscheduleUpdateForTarget: %p", target); 61 | } else { 62 | //NSLog(@"REScheduler: Warning: Trying to unschedule update for that isn't scheduled"); 63 | } 64 | } 65 | 66 | - (void)tick:(double)dt { 67 | for (id target in [[updateTargets copy] autorelease]) { // WARNING! May be inefficient to copy! 68 | [target update:dt]; 69 | } 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /Classes/REShader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | 29 | 30 | @interface REShader : NSObject { 31 | GLenum type; 32 | GLuint shader; 33 | 34 | NSString *string; 35 | } 36 | 37 | @property (nonatomic, readonly) GLuint shader; 38 | 39 | 40 | - (id)initWithType:(GLenum)type string:(NSString*)string; // Designated 41 | - (id)initWithType:(GLenum)type filename:(NSString*)filename; 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /Classes/REShader.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | 28 | #import "REShader.h" 29 | 30 | @interface REShader () 31 | 32 | - (void)compile; 33 | 34 | @end 35 | 36 | @implementation REShader 37 | 38 | @synthesize shader; 39 | 40 | - (id)initWithType:(GLenum)t filename:(NSString*)n { 41 | NSString *s = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:n ofType:nil] encoding:NSUTF8StringEncoding error:nil]; 42 | 43 | NSString *assertMessage = [NSString stringWithFormat:@"REShader: Can't find file named: %@", n]; 44 | NSAssert(s, assertMessage); 45 | 46 | return [self initWithType:t string:s]; 47 | } 48 | 49 | - (id)initWithType:(GLenum)t string:(NSString*)s { 50 | if ((self = [super init])) { 51 | type = t; 52 | string = [s retain]; 53 | 54 | NSAssert(string, @"REShader: String is nil"); 55 | 56 | [self compile]; 57 | } return self; 58 | } 59 | 60 | - (void)dealloc { 61 | glDeleteShader(shader); 62 | [string release]; 63 | [super dealloc]; 64 | } 65 | 66 | - (void)compile { 67 | GLint status; 68 | 69 | const GLchar *source = [string UTF8String]; 70 | shader = glCreateShader(type); 71 | 72 | glShaderSource(shader, 1, &source, NULL); 73 | glCompileShader(shader); 74 | 75 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 76 | 77 | 78 | 79 | // LOG // TODO: Parse and log line from error? 80 | { 81 | GLint logLength; 82 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength); 83 | if (logLength > 0) { 84 | GLchar *log = (GLchar *)malloc(logLength); 85 | glGetShaderInfoLog(shader, logLength, &logLength, log); 86 | NSLog(@"GLProgram: (%@) Compile log:\n%s", string, log); 87 | free(log); 88 | } 89 | } 90 | 91 | } 92 | 93 | @end 94 | -------------------------------------------------------------------------------- /Classes/RESprite.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | 29 | @class RETexture2D; 30 | @class RESpriteBatchNode; 31 | @class REBuffer; 32 | 33 | 34 | typedef struct RESpriteAttribs { 35 | CC3Vector position; 36 | CC3Vector texCoord; 37 | } RESpriteAttribs; 38 | 39 | /* RESprite is a node with a texture. 40 | 41 | Local content is defined along x,y-plane 42 | 43 | 1 First clips to textureContentFrame 44 | 2. Then flips/rotates 45 | */ 46 | 47 | 48 | @interface RESprite : RENode { 49 | 50 | RESpriteBatchNode *batchNode_; 51 | 52 | RETexture2D *texture; 53 | RESpriteAttribs *attribs; 54 | 55 | BOOL areTexCoordsDirty; 56 | 57 | CC3Vector *originalTexCoords; 58 | CC3GLMatrix *texCoordTransform; 59 | 60 | BOOL textureFlipX, textureFlipY; 61 | 62 | CGRect textureContentFrame; 63 | 64 | CC3Vector4 multiplyColor; 65 | 66 | REBuffer *attribBuffer_; 67 | 68 | BOOL alphaPremultipled_; 69 | BOOL isProgramDirty_; 70 | 71 | } 72 | 73 | /** The texture used for drawing */ 74 | @property (nonatomic, retain) RETexture2D *texture; 75 | 76 | /** The content frame that is used for drawing. Defaults to CGRectZero, which means use entire texture. */ 77 | @property (nonatomic, assign) CGRect textureContentFrame; 78 | 79 | /** Defines whether texture should be flipped. Defaults to NO. */ 80 | @property (nonatomic, assign) BOOL textureFlipX, textureFlipY; 81 | 82 | /** Defines whether texture should be rotated. Defaults to 0. */ 83 | @property (nonatomic, assign) float textureRotationAngle; 84 | 85 | // Is alpha premultiplied in texture. It's better to premultiply when saving the texture, than doing it here. 86 | // Saving with alpha premultipled can be done with pvr-textures with PVRTexTool. 87 | @property (nonatomic, assign) BOOL alphaPremultipled; 88 | 89 | 90 | @property (nonatomic, assign) CGRect frame; // Undefined if rotation is set. 91 | 92 | @property (nonatomic, assign) CC3Vector4 multiplyColor; 93 | 94 | @property (nonatomic, readonly) RESpriteAttribs *attribs; // To be used for batch node and maybe others. Will be of length 4. 95 | 96 | // Do not set this directly 97 | @property (nonatomic, assign) RESpriteBatchNode *batchNode; 98 | 99 | 100 | 101 | @end 102 | -------------------------------------------------------------------------------- /Classes/RESpriteBatchNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | #import "RESprite.h" 29 | 30 | @class RESprite, REBuffer; 31 | 32 | /* 33 | Just used for drawing sprites. 34 | 35 | Needs to be added to world so it will get visited and hence draw. 36 | Sprites needs to be added to world so they will get correct transform from parents. 37 | 38 | Should be added to node that uses the same camera as the sprites. 39 | 40 | Supports multiplyColor on nodes 41 | */ 42 | 43 | @interface RESpriteBatchNode : RENode { 44 | 45 | NSMutableArray *sprites; 46 | 47 | GLushort *elementIndices; 48 | GLfloat *batchUnitAttrib; 49 | 50 | REBuffer *elementIndexBuffer; 51 | REBuffer *batchUnitAttribBuffer; 52 | REBuffer *positionAttribBuffer; 53 | 54 | GLfloat *modelViewMatrices; 55 | GLfloat *multiplyColors; 56 | 57 | // We don't need both of these 58 | RESpriteAttribs *batchedAttribs; // Not used 59 | NSMutableArray *batchedAttribsBuffers_; // One REBuffer for each batch. Index: batch index only used for tex coords. This may be updated for each frame, for instance on animation. 60 | 61 | // NSMutableArray *batchedTexCoordAttribBuffers; // One for each batch, connected 62 | NSMutableArray *vertexArrayObjects; // Index: batch index 63 | } 64 | 65 | // Do not call these directly. Set batch node property on sprite instead. 66 | // Sprite batch node doesn't retain it's sprites 67 | - (void)addSprite:(RESprite*)sprite; 68 | - (void)removeSprite:(RESprite*)sprite; 69 | 70 | @property (nonatomic, assign) int tag; 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /Classes/RETexture.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | /* 28 | HOW TO CREATE PVR 29 | // Example usage of texturetool: 30 | 31 | // -f PVR is to include header-info 32 | // -m is to generate mipmaps (not required) 33 | 34 | // ./texturetool -m -f PVR -e PVRTC --channel-weighting-perceptual --bits-per-pixel-4 -o ~/Desktop/pvrtest/mega1.pvrtc -p ~/Desktop/pvrtest/mega1_preview.png ~/Desktop/pvrtest/mega1.png 35 | 36 | // IMPROVE HERE 37 | // https://github.com/nicklockwood/GLView/ 38 | */ 39 | 40 | @interface RETexture : NSObject { 41 | GLuint texture_; 42 | GLenum target_; 43 | 44 | CGSize contentSize_; // Created here, but only exposed in texture2D 45 | } 46 | 47 | @property (nonatomic, readonly) GLuint texture; 48 | @property (nonatomic, readonly) CGSize contentSize; // Size of content in pixels. Only available of texture is RETexture2D! 49 | 50 | // Target must be either GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP 51 | - (id)initWithTarget:(GLenum)target; 52 | 53 | // Binds texture to GL_TEXTURE0 54 | - (void)bind; 55 | 56 | // Binds texture to specified texture unit. (GL_TEXTURE0, GL_TEXTURE1, etc..) 57 | - (void)bind:(GLenum)textureUnit; 58 | 59 | // Unbinds textures for all texture units. 60 | + (void)unbind; 61 | 62 | @end 63 | 64 | 65 | 66 | 67 | @interface RETexture2D : RETexture { 68 | 69 | } 70 | 71 | 72 | // Will use cache. Both png, jpg and pvr-files are ok. 73 | + (id)textureNamed:(NSString*)filename; 74 | 75 | // Will not use cache 76 | + (id)textureWithImage:(UIImage*)image; 77 | + (id)textureWithImageBuffer:(CVImageBufferRef)imageBuffer; 78 | + (id)textureWithRawData:(void*)pixels width:(GLsizei)width height:(GLsizei)height internalFormat:(GLint)internalFormat format:(GLint)format type:(GLint)type; 79 | 80 | 81 | 82 | // This method uses cache. 83 | //- (id)initWithImageNamed:(NSString*)filename; // NOT YET IMPLEMENTED 84 | 85 | - (id)initWithImage:(UIImage*)image; 86 | - (id)initWithImageBuffer:(CVImageBufferRef)imageBuffer; 87 | - (id)initWithPVRData:(NSData *)pvrData; 88 | - (id)initWithRawData:(void*)pixels width:(GLsizei)width height:(GLsizei)height internalFormat:(GLint)internalFormat format:(GLint)format type:(GLint)type; 89 | 90 | - (void)setImageFromImageBuffer:(CVImageBufferRef)imageBuffer; // Use this if updating texture from camera. Roughly 50-60% faster. 91 | 92 | @end 93 | 94 | 95 | @interface RETextureCubeMap : RETexture { 96 | 97 | } 98 | 99 | // Target should be one of following: 100 | // GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, or GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 101 | 102 | // NOTE: ALL TEXTURES NEEDS TO BE OF THE SAME SIZE! 103 | - (void)setImage:(UIImage*)image forTarget:(GLenum)target; 104 | - (void)setImageBuffer:(CVImageBufferRef)imageBuffer forTarget:(GLenum)target; 105 | - (void)setPVRData:(NSData*)data forTarget:(GLenum)target; 106 | - (void)setRawData:(void*)pixels width:(GLsizei)width height:(GLsizei)height internalFormat:(GLint)internalFormat format:(GLint)format type:(GLint)type forTarget:(GLenum)target; 107 | 108 | @end 109 | 110 | 111 | 112 | @interface RETexture2D (Text) 113 | 114 | - (id)initWithString:(NSString*)string font:(UIFont*)font; // Dependent on screen size 115 | 116 | @end 117 | 118 | 119 | -------------------------------------------------------------------------------- /Classes/REVertexArrayObject.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | 29 | @interface REVertexArrayObject : NSObject { 30 | GLuint vertexArrayObject; 31 | } 32 | 33 | - (void)bind; 34 | + (void)unbind; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /Classes/REVertexArrayObject.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REVertexArrayObject.h" 28 | #import "REGLStateManager.h" 29 | 30 | @implementation REVertexArrayObject 31 | 32 | - (id)init { 33 | if ((self = [super init])) { 34 | glGenVertexArraysOES(1, &vertexArrayObject); 35 | } return self; 36 | } 37 | 38 | - (void)dealloc { 39 | // Unbind if bound, for safety 40 | if ([[REGLStateManager sharedManager] vertexArrayObjectBinding] == vertexArrayObject) { 41 | [[self class] unbind]; 42 | } 43 | glDeleteVertexArraysOES(1, &vertexArrayObject); 44 | [super dealloc]; 45 | } 46 | 47 | - (void)bind { 48 | [[REGLStateManager sharedManager] setVertexArrayObjectBinding:vertexArrayObject]; 49 | } 50 | 51 | + (void)unbind { 52 | [[REGLStateManager sharedManager] setVertexArrayObjectBinding:0]; 53 | } 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /Classes/REWavefrontMesh.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import 28 | #import "CC3Foundation.h" 29 | 30 | @class REBuffer; 31 | 32 | /* REWavefrontMesh 33 | 34 | Contains support only for polygons with edge count 3 (triangels). 35 | 36 | This is because opengl es 2.0 draw elments doesn't support polygons with higher edge count. 37 | Only points, lines and triangels can be drawn. 38 | 39 | http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawElements.xml 40 | GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, and GL_TRIANGLES 41 | 42 | Note: Should probably sse GL_TRIANGELS to draw, (at least if distinct draw mode is not specified) 43 | 44 | Design choice: We want to keep the groups and other future parameters seperate. This makes the class more flexible and easier to add stuff later. 45 | 46 | Note: All data needed to recreate class is stored in REWavefrontElementComponents. See loadFromElementComponents: 47 | 48 | */ 49 | 50 | // The vertex data: (This is passed directly to opengl together with indices) 51 | typedef struct REWavefrontVertexAttributes { 52 | CC3Vector vertex; 53 | CC3Vector texCoord; 54 | CC3Vector normal; 55 | CC3Vector tangent; 56 | } REWavefrontVertexAttributes; 57 | 58 | 59 | 60 | @interface REWavefrontElementSet : NSObject { 61 | NSMutableArray *indexRanges; 62 | CC3BoundingBox boundingBox; 63 | } 64 | 65 | @property (nonatomic, readonly) CC3BoundingBox boundingBox; 66 | 67 | // Array of NSValues with NSRanges. Contains indexes ranges that indicade index of the original 68 | // element indices 69 | @property (nonatomic, readonly) NSArray *indexRanges; 70 | 71 | - (void)addIndex:(int)i; // Must be incremental 72 | - (void)extendBoundingBox:(CC3Vector)position; 73 | 74 | 75 | @end 76 | 77 | @interface REWavefrontMesh : NSObject { 78 | 79 | // This is what we want to get out: 80 | 81 | // 1. Vertex attribues 82 | REWavefrontVertexAttributes *vertexAttributes; 83 | uint vertexAttributeCount; 84 | 85 | // 2. Elements 86 | GLushort *elementIndices; 87 | uint elementIndexCount; 88 | 89 | // 3. Different element collections (could be all, or by group) 90 | REWavefrontElementSet *allElements; 91 | NSMutableDictionary *elementsByGroup; 92 | 93 | // 4. Has normals/texcoords 94 | BOOL hasNormals; 95 | BOOL hasTexCoords; 96 | 97 | NSMutableArray *groups; // Group names 98 | 99 | // Buffers 100 | REBuffer *vertexAttributeBuffer, *elementIndexBuffer; 101 | 102 | } 103 | 104 | 105 | 106 | @property (nonatomic, readonly) REWavefrontVertexAttributes *vertexAttributes; 107 | @property (nonatomic, readonly) uint vertexAttributeCount; 108 | 109 | @property (nonatomic, readonly) GLushort *elementIndices; 110 | @property (nonatomic, readonly) uint elementIndexCount; 111 | 112 | @property (nonatomic, readonly) BOOL hasNormals, hasTexCoords; 113 | 114 | @property (nonatomic, readonly) NSArray *groups; 115 | 116 | // Will use cache. Both obj and reobj are ok 117 | + (id)meshNamed:(NSString*)filename; 118 | 119 | - (id)initWithString:(NSString*)string; // Designated 120 | 121 | // Mesh filename in application bundle. 122 | - (id)initWithMeshNamed:(NSString*)filename; 123 | 124 | - (REWavefrontElementSet*)allElements; 125 | - (REWavefrontElementSet*)elementsForGroup:(NSString*)group; // Collection elements by group 126 | 127 | - (void)createBuffers; // Creates vertex attribute buffer and element index buffer 128 | - (void)deleteBuffers; 129 | - (BOOL)hasBuffers; 130 | - (void)bindBuffers; 131 | 132 | @end 133 | -------------------------------------------------------------------------------- /Classes/REWavefrontMeshElementIndexBatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | 28 | 29 | /* REWavefrontMeshElementIndexBatch 30 | * 31 | * The purpose of this object is combine element indices from multiple sets, so we can pass it in single draw call. 32 | * (And create buffers) 33 | */ 34 | 35 | @class REBuffer; 36 | 37 | @interface REWavefrontMeshElementIndexBatch : NSObject { 38 | GLushort *indices; 39 | int length; 40 | int numberOfAttributes; 41 | 42 | GLfloat *batchIndexAttributes; // Contains the batch index for each element indec 43 | 44 | REBuffer *elementIndexBuffer; 45 | REBuffer *batchIndexAttributeBuffer; 46 | } 47 | 48 | @property (nonatomic, readonly) GLushort *indices; // The batched indices 49 | @property (nonatomic, readonly) int length; 50 | @property (nonatomic, readonly) int numberOfAttributes; 51 | @property (nonatomic, readonly) GLfloat *batchIndexAttributes; 52 | 53 | - (id)initWithElementSets:(NSArray*)elementSets indices:(GLushort*)rawIndices; 54 | 55 | - (BOOL)hasElementIndexBuffer; 56 | - (void)createElementIndexBuffer; 57 | - (void)bindElementIndexBuffer; 58 | 59 | - (BOOL)hasBatchIndexAttributeBuffer; 60 | - (void)createBatchIndexAttributeBuffer; 61 | - (void)bindBatchIndexAttributeBuffer; 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /Classes/REWavefrontMeshElementIndexBatch.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REWavefrontMeshElementIndexBatch.h" 28 | #import "REWavefrontMesh.h" 29 | 30 | @implementation REWavefrontMeshElementIndexBatch 31 | 32 | @synthesize indices, length, batchIndexAttributes, numberOfAttributes; 33 | 34 | - (id)initWithElementSets:(NSArray*)elementSets indices:(GLushort*)rawIndices { 35 | if ((self = [super init])) { 36 | // Initialization code here. 37 | 38 | length = 0; 39 | 40 | numberOfAttributes = 0; // We can find this here, since indices higher than what we can find, won't be drawn any. We may also be able to send numberOfVertexAttributes from the mesh with (probably) the same result 41 | 42 | // Find total number of indices for this batch and store in 'length'. 43 | for (REWavefrontElementSet *elementSet in elementSets) { 44 | for (NSValue *rangeValue in elementSet.indexRanges) { 45 | NSRange range = [rangeValue rangeValue]; 46 | length += range.length; 47 | numberOfAttributes = MAX(numberOfAttributes, range.location + range.length); 48 | } 49 | } 50 | 51 | // Allocate memory 52 | indices = calloc(length, sizeof(GLushort)); 53 | batchIndexAttributes = calloc(numberOfAttributes, sizeof(GLfloat)); 54 | 55 | // Loop through again to populate from raw indices 56 | int currentIndex = 0; 57 | int elementSetIndex = 0; 58 | for (REWavefrontElementSet *elementSet in elementSets) { 59 | for (NSValue *rangeValue in elementSet.indexRanges) { 60 | NSRange range = [rangeValue rangeValue]; 61 | memcpy(indices + currentIndex, rawIndices + range.location, range.length * sizeof(GLushort)); 62 | 63 | // Set float attribute batch indices (can't be done with memset since it doesn't support floats) 64 | for (int i = 0; i < range.length; i++) { 65 | batchIndexAttributes[*(indices + currentIndex + i)] = (float)elementSetIndex; 66 | } 67 | 68 | currentIndex += range.length; 69 | } 70 | elementSetIndex ++; 71 | } 72 | 73 | } return self; 74 | } 75 | 76 | - (void)dealloc { 77 | free(indices); 78 | free(batchIndexAttributes); 79 | [elementIndexBuffer release]; 80 | [batchIndexAttributeBuffer release]; 81 | [super dealloc]; 82 | } 83 | 84 | - (BOOL)hasElementIndexBuffer { 85 | return elementIndexBuffer != nil; 86 | } 87 | 88 | - (void)createElementIndexBuffer { 89 | if (!elementIndexBuffer) { 90 | elementIndexBuffer = [[REBuffer alloc] initWithTarget:GL_ELEMENT_ARRAY_BUFFER data:indices length:length * sizeof(GLushort)]; 91 | } 92 | } 93 | 94 | - (void)bindElementIndexBuffer { 95 | NSAssert([self hasElementIndexBuffer], @"REWavefrontElementIndexBatch: Can't bind buffer if we have no buffer"); 96 | [elementIndexBuffer bind]; 97 | } 98 | 99 | - (BOOL)hasBatchIndexAttributeBuffer { 100 | return batchIndexAttributeBuffer != nil; 101 | } 102 | 103 | - (void)createBatchIndexAttributeBuffer { 104 | if (!batchIndexAttributeBuffer) { 105 | batchIndexAttributeBuffer = [[REBuffer alloc] initWithTarget:GL_ARRAY_BUFFER data:batchIndexAttributes length:numberOfAttributes * sizeof(GLfloat)]; 106 | } 107 | } 108 | 109 | - (void)bindBatchIndexAttributeBuffer { 110 | NSAssert([self hasBatchIndexAttributeBuffer], @"REWavefrontElementIndexBatch: Can't bind buffer if we have no buffer"); 111 | [batchIndexAttributeBuffer bind]; 112 | } 113 | 114 | @end 115 | -------------------------------------------------------------------------------- /Classes/REWavefrontMeshGroupNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | 29 | @class REWavefrontMesh, REWavefrontElementSet; 30 | @class RETexture2D; 31 | 32 | /* REWavefrontMeshGroupNode 33 | 34 | Since everything in a mesh is contained within subgroups. 35 | 36 | It's the groups that are actually drawn. 37 | 38 | */ 39 | 40 | @interface REWavefrontMeshGroupNode : RENode { 41 | 42 | REWavefrontMesh *wavefrontMesh; 43 | NSString *group; 44 | REWavefrontElementSet *elementSet; // The indices for this node 45 | 46 | RETexture2D *texture; 47 | } 48 | 49 | @property (nonatomic, readonly) NSString *group; 50 | @property (nonatomic, readonly) REWavefrontElementSet *elementSet; 51 | 52 | @property (nonatomic, assign) float nextFrameTween; 53 | @property (nonatomic, assign) CC3Vector nextFramePosition; 54 | @property (nonatomic, assign) CC3Vector4 nextFrameRotation; 55 | 56 | @property (nonatomic, retain) RETexture2D *texture; 57 | 58 | - (id)initWithWavefrontMesh:(REWavefrontMesh*)m group:(NSString*)g; 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /Classes/REWavefrontMeshGroupNode.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REWavefrontMeshGroupNode.h" 28 | #import "REWavefrontMesh.h" 29 | 30 | @implementation REWavefrontMeshGroupNode 31 | 32 | @synthesize texture, group, elementSet; 33 | @synthesize nextFrameTween, nextFramePosition, nextFrameRotation; 34 | 35 | - (id)initWithWavefrontMesh:(REWavefrontMesh*)m group:(NSString*)g { 36 | if ((self = [super init])) { 37 | wavefrontMesh = [m retain]; 38 | group = [g retain]; 39 | 40 | elementSet = [wavefrontMesh elementsForGroup:group]; 41 | 42 | self.boundingBox = elementSet.boundingBox; 43 | 44 | // Set anchor coordinate to center 45 | self.anchorCoordinate = CC3BoundingBoxCenter(self.boundingBox); 46 | self.position = self.anchorCoordinate; 47 | 48 | } return self; 49 | } 50 | 51 | - (void)dealloc { 52 | elementSet = nil; 53 | [wavefrontMesh release]; 54 | [group release]; 55 | [texture release]; 56 | [super dealloc]; 57 | } 58 | 59 | + (REProgram*)program { 60 | return [REProgram programWithVertexFilename:@"sREWavefrontMeshGroup.vsh" fragmentFilename:@"sREWavefrontMeshGroup.fsh"]; 61 | } 62 | 63 | 64 | - (void)draw { 65 | 66 | } 67 | 68 | @end 69 | -------------------------------------------------------------------------------- /Classes/REWavefrontMeshNode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "RENode.h" 28 | 29 | 30 | @class REWavefrontMesh; 31 | @class RETexture2D; 32 | @class REVertexArrayObject; 33 | 34 | /* Contains texture 35 | 36 | Doesn't draw anything itself. Needs group node to draw 37 | 38 | NOTE: Each vertex attribute must belong to specific group in single batch for this to work. Else, we need to take duplicates into account, which may be costly and should maybe be precalculated. 39 | 40 | */ 41 | 42 | @interface REWavefrontMeshNode : RENode { 43 | REWavefrontMesh *wavefrontMesh; 44 | 45 | NSMutableDictionary *groupNodes; // Key: groupname, value: REWavefrontMeshSubgroupNode 46 | 47 | RETexture2D *texture; 48 | 49 | NSMutableArray *indexBatches; // Index: batch index 50 | 51 | //REVertexArrayObject *vertexArrayObject; 52 | NSMutableArray *vertexArrayObjects; // Index: batch index 53 | } 54 | 55 | @property (nonatomic, readonly) REWavefrontMesh *wavefrontMesh; 56 | @property (nonatomic, readonly) NSDictionary *groupNodes; 57 | @property (nonatomic, retain) RETexture2D *texture; 58 | 59 | - (id)initWithWavefrontMesh:(REWavefrontMesh*)m; 60 | 61 | 62 | @end 63 | -------------------------------------------------------------------------------- /Classes/REWorld.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | 28 | #import "RENode.h" 29 | 30 | @class RECamera; 31 | @class RETextureCubeMap; 32 | @class RELight; 33 | 34 | /* REWorld 35 | 36 | Manages lights, etc. 37 | 38 | Note: Camera is moved to node, since we may have billboards and stuff wh 39 | */ 40 | 41 | @interface REWorld : RENode { 42 | //RECamera *camera; // Should camera be a node? The camera itself should not have a camera, contentSize, etc... 43 | 44 | NSMutableDictionary *lightsByClass_; // Key 45 | 46 | RETextureCubeMap *environmentMap; 47 | } 48 | 49 | @property (nonatomic, retain) RETextureCubeMap *environmentMap; 50 | 51 | @property (nonatomic, readonly) NSArray *lights; 52 | 53 | - (void)addLight:(RELight*)light; 54 | - (void)removeLight:(RELight*)light; 55 | 56 | - (NSArray*)lightsOfClass:(Class)lightClass; 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /Classes/REWorld.m: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REWorld.h" 28 | 29 | @interface REWorld () 30 | 31 | 32 | @end 33 | 34 | @implementation REWorld 35 | 36 | @synthesize environmentMap; 37 | 38 | - (id)init { 39 | if ((self = [super init])) { 40 | lightsByClass_ = [[NSMutableDictionary alloc] init]; 41 | } return self; 42 | } 43 | 44 | - (void)dealloc { 45 | [lightsByClass_ release]; 46 | [super dealloc]; 47 | } 48 | 49 | #pragma mark - Lights 50 | 51 | - (NSArray*)lightsOfClass:(Class)lightClass { 52 | NSString *key = NSStringFromClass(lightClass); 53 | return [lightsByClass_ objectForKey:key]; 54 | } 55 | 56 | - (NSArray*)lights { 57 | return nil; // Deprectated. 58 | } 59 | 60 | - (void)addLight:(RELight*)light { 61 | 62 | NSString *key = NSStringFromClass([light class]); 63 | NSMutableArray *lights = [lightsByClass_ objectForKey:key]; 64 | 65 | // If this is the first light of it's kind. Create array. 66 | if (!lights) { 67 | lights = [NSMutableArray array]; 68 | [lightsByClass_ setObject:lights forKey:key]; 69 | } 70 | 71 | if ([lights indexOfObjectIdenticalTo:light] == NSNotFound) { 72 | [lights addObject:light]; 73 | } 74 | 75 | } 76 | 77 | - (void)removeLight:(RELight*)light { 78 | 79 | NSString *key = NSStringFromClass([light class]); 80 | NSMutableArray *lights = [lightsByClass_ objectForKey:key]; 81 | 82 | if ([lights indexOfObjectIdenticalTo:light] != NSNotFound) { 83 | [lights removeObject:light]; 84 | 85 | // If this is the last light of it's kind. Remove array. 86 | if ([lights count] == 0) { 87 | [lightsByClass_ removeObjectForKey:key]; 88 | } 89 | } 90 | } 91 | 92 | 93 | @end 94 | -------------------------------------------------------------------------------- /Classes/Rend.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #import "REBuffer.h" 28 | #import "RECache.h" 29 | #import "RECamera.h" 30 | #import "REDirector.h" 31 | #import "REGLStateManager.h" 32 | #import "REGLTypes.h" 33 | #import "REGLView.h" 34 | #import "RELight.h" 35 | #import "REMaterial.h" 36 | #import "RENode.h" 37 | #import "RENSArrayAdditions.h" 38 | #import "RENSValueAdditions.h" 39 | #import "REProgram.h" 40 | #import "RERotator.h" 41 | #import "REScene.h" 42 | #import "REScheduler.h" 43 | #import "REShader.h" 44 | #import "RESprite.h" 45 | #import "RESpriteBatchNode.h" 46 | #import "RETexture.h" 47 | #import "REWorld.h" 48 | 49 | #import "REKeyframedMeshNode.h" 50 | #import "REWavefrontMesh.h" 51 | #import "REWavefrontMeshNode.h" -------------------------------------------------------------------------------- /Examples/Rend Example Collection/BumpSphere Example.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.rend.examplecollection.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/AngleUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // AngleUtil.h 3 | // Vodafone 4 | // 5 | // Created by Anton Holmberg on 2011-06-17. 6 | // Copyright 2011 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | @interface AngleUtil : NSObject 10 | 11 | + (double)spinAngle:(double)angle within360DegreesFrom:(double)intervalLowerLimitAngle; 12 | 13 | @end -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/AngleUtil.m: -------------------------------------------------------------------------------- 1 | // 2 | // AngleUtil.h 3 | // Vodafone 4 | // 5 | // Created by Anton Holmberg on 2011-06-17. 6 | // Copyright 2011 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | #import "AngleUtil.h" 10 | 11 | @implementation AngleUtil 12 | 13 | + (double)spinAngle:(double)angle within360DegreesFrom:(double)intervalLowerLimitAngle { 14 | double intervalUpperLimitAngle = intervalLowerLimitAngle + 360.0f; 15 | if(angle < intervalLowerLimitAngle) { 16 | double diff = ABS(angle - intervalLowerLimitAngle); 17 | int nSpins = ceil(diff/360.0f); 18 | angle += nSpins * 360.0f; 19 | } else if(angle > intervalUpperLimitAngle) { 20 | double diff = ABS(angle - intervalUpperLimitAngle); 21 | int nSpins = ceil(diff/360.0f); 22 | angle -= nSpins * 360.0f; 23 | } 24 | return angle; 25 | } 26 | 27 | @end -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/BumpSphereAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // BumpMappingExample 4 | // 5 | // Created by Anton Holmberg on 2012-07-01. 6 | // Copyright (c) 2012 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/BumpSphereAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // BumpMappingExample 4 | // 5 | // Created by Anton Holmberg on 2012-07-01. 6 | // Copyright (c) 2012 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | #import "BumpSphereAppDelegate.h" 10 | #import "SphereViewController.h" 11 | 12 | @implementation AppDelegate 13 | 14 | @synthesize window = _window; 15 | 16 | - (void)dealloc 17 | { 18 | [_window release]; 19 | [super dealloc]; 20 | } 21 | 22 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 23 | { 24 | // Create OpenGL Context 25 | EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 26 | [EAGLContext setCurrentContext:context]; 27 | 28 | // Setup window 29 | self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 30 | self.window.backgroundColor = [UIColor blackColor]; 31 | self.window.rootViewController = [[[SphereViewController alloc] init] autorelease]; 32 | [self.window makeKeyAndVisible]; 33 | 34 | return YES; 35 | } 36 | 37 | - (void)applicationWillResignActive:(UIApplication *)application 38 | { 39 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 40 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 41 | } 42 | 43 | - (void)applicationDidEnterBackground:(UIApplication *)application 44 | { 45 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 46 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 47 | } 48 | 49 | - (void)applicationWillEnterForeground:(UIApplication *)application 50 | { 51 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 52 | } 53 | 54 | - (void)applicationDidBecomeActive:(UIApplication *)application 55 | { 56 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 57 | } 58 | 59 | - (void)applicationWillTerminate:(UIApplication *)application 60 | { 61 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 62 | } 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/GLViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // GLViewController.h 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface GLViewController : UIViewController { 12 | REGLView *glView_; 13 | REScene *scene_; 14 | REWorld *world_; 15 | REDirector *director_; 16 | RECamera *camera_; 17 | } 18 | 19 | @property (nonatomic, readonly) REGLView *glView; 20 | @property (nonatomic, readonly) REScene *scene; 21 | @property (nonatomic, readonly) REWorld *world; 22 | 23 | - (void)update:(float)dt; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/GLViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // GLViewController.m 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "GLViewController.h" 10 | 11 | @interface GLViewController () 12 | 13 | @end 14 | 15 | @implementation GLViewController 16 | 17 | @synthesize glView = glView_; 18 | @synthesize scene = scene_; 19 | @synthesize world = world_; 20 | 21 | 22 | - (void)dealloc { 23 | [glView_ release]; 24 | [director_ release]; 25 | [scene_ release]; 26 | [world_ release]; 27 | [camera_ release]; 28 | [super dealloc]; 29 | } 30 | 31 | - (void)viewDidLoad { 32 | [super viewDidLoad]; 33 | 34 | glView_ = [[REGLView alloc] initWithFrame:CGRectMake(0, 0, 320, 320) colorFormat:kEAGLColorFormatRGBA8 multisampling:YES]; 35 | [self.view addSubview:glView_]; 36 | 37 | camera_ = [[RECamera alloc] initWithProjection:kRECameraProjectionOrthographic]; 38 | camera_.position = CC3VectorMake(0, 0, 320); 39 | camera_.upDirection = CC3VectorMake(0, 1, 0); 40 | camera_.lookDirection = CC3VectorMake(0, 0, -1); 41 | camera_.frustumNear = 10; 42 | camera_.frustumFar = 640; 43 | camera_.frustumLeft = -glView_.frame.size.width / 2.0; 44 | camera_.frustumRight = glView_.frame.size.width / 2.0; 45 | camera_.frustumBottom = -glView_.frame.size.height / 2.0; 46 | camera_.frustumTop = glView_.frame.size.height / 2.0; 47 | 48 | scene_ = [[REScene alloc] init]; 49 | scene_.camera = camera_; 50 | 51 | world_ = [[REWorld alloc] init]; 52 | [scene_ addChild:world_]; 53 | 54 | director_ = [[REDirector alloc] init]; 55 | director_.view = glView_; 56 | director_.scene = scene_; 57 | } 58 | 59 | - (void)viewDidUnload { 60 | [super viewDidUnload]; 61 | [glView_ release], glView_ = nil; 62 | [director_ release], director_ = nil; 63 | [scene_ release], scene_ = nil; 64 | [world_ release], world_ = nil; 65 | [camera_ release], camera_ = nil; 66 | } 67 | 68 | - (void)viewDidAppear:(BOOL)animated { 69 | [super viewDidAppear:animated]; 70 | director_.running = YES; 71 | [[REScheduler sharedScheduler] scheduleUpdateForTarget:self]; 72 | } 73 | 74 | - (void)viewWillDisappear:(BOOL)animated { 75 | [super viewWillDisappear:animated]; 76 | director_.running = NO; 77 | [[REScheduler sharedScheduler] unscheduleUpdateForTarget:self]; 78 | } 79 | 80 | - (void)update:(float)dt { 81 | 82 | } 83 | 84 | @end 85 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/Rend Example Collection-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.rend.examplecollection.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/Rend Example Collection-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'Rend Example Collection' target in the 'Rend Example Collection' project 3 | // 4 | 5 | #import 6 | 7 | #ifndef __IPHONE_3_0 8 | #warning "This project uses features only available in iOS SDK 3.0 and later." 9 | #endif 10 | 11 | #ifdef __OBJC__ 12 | #import 13 | #import 14 | 15 | #import "Rend.h" 16 | #endif 17 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/Resources/teapot.mtl: -------------------------------------------------------------------------------- 1 | # Max2Mtl Version 4.0 Mar 10th, 2001 2 | newmtl m1 3 | Ka 0.1 0.1 0.1 4 | Kd 1 1 1 5 | Ks 0.8 0.8 0.8 6 | d 1.0 7 | Ns 32 8 | illum 2 9 | # EOF 10 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/SphereNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // SphereNode.h 3 | // dgi12Projekt 4 | // 5 | // Created by Anton Holmberg on 2012-05-25. 6 | // Copyright (c) 2012 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | #import "RENode.h" 10 | 11 | typedef struct SphereNodeAttribs { 12 | CC3Vector position; 13 | CC3Vector texCoord; 14 | CC3Vector bumpAxisX; 15 | CC3Vector bumpAxisY; 16 | } SphereNodeAttribs; 17 | 18 | @interface SphereNode : RENode { 19 | 20 | int resolutionX_; 21 | int resolutionY_; 22 | int nAttribs_; 23 | SphereNodeAttribs *attribs_; 24 | } 25 | 26 | @property (nonatomic, retain) RETexture2D *texture; 27 | @property (nonatomic, retain) RETexture2D *bumpMap; 28 | @property (nonatomic, readwrite) float shinyness; 29 | @property (nonatomic, readwrite) float specularLightBrightness; 30 | @property (nonatomic, readwrite) float bumpMapOffset; 31 | 32 | - (id)initWithResolutionX:(int)resolutionX resolutionY:(int)resolutionY radius:(float)radius; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/SphereNode.m: -------------------------------------------------------------------------------- 1 | // 2 | // SphereNode.m 3 | // dgi12Projekt 4 | // 5 | // Created by Anton Holmberg on 2012-05-25. 6 | // Copyright (c) 2012 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | #import "SphereNode.h" 10 | 11 | @interface SphereNode () 12 | 13 | - (CC3Vector)positionForHorizontalAngle:(float)ha topAngle:(float)ta radius:(float)r; 14 | - (CC3Vector)bumpAxisXForHorizontalAngle:(float)ha topAngle:(float)ta; 15 | - (CC3Vector)bumpAxisYForHorizontalAngle:(float)ha topAngle:(float)ta; 16 | 17 | @end 18 | 19 | @implementation SphereNode 20 | 21 | @synthesize texture = texture_; 22 | @synthesize bumpMap = bumpMap_; 23 | @synthesize shinyness = shinyness_; 24 | @synthesize specularLightBrightness = specularLightBrightness_; 25 | @synthesize bumpMapOffset = bumpMapOffset_; 26 | 27 | - (id)initWithResolutionX:(int)resolutionX resolutionY:(int)resolutionY radius:(float)radius { 28 | if (self = [super init]) { 29 | 30 | resolutionX_ = resolutionX; 31 | resolutionY_ = resolutionY; 32 | 33 | float r = radius; 34 | 35 | nAttribs_ = 2 * resolutionX_ * (resolutionY_ - 1); 36 | 37 | NSLog(@"nAttribs_: %d", nAttribs_); 38 | attribs_ = calloc(nAttribs_, sizeof(SphereNodeAttribs)); 39 | memset(attribs_, 0, nAttribs_ * sizeof(SphereNodeAttribs)); 40 | for(int iy = 0; iy < resolutionY_ - 1; iy++) { 41 | for(int ix = 0; ix < resolutionX_; ix++) { 42 | 43 | int index = iy * 2 * resolutionX_ + 2 * ix; 44 | 45 | float fx = ix/(float)(resolutionX_ - 1); 46 | 47 | float fy = iy/(float)(resolutionY_ - 1); 48 | float nextFY = (iy + 1)/(float)(resolutionY_ - 1); 49 | 50 | float ha = fx * 2 * M_PI; 51 | float ta0 = fy * M_PI; 52 | float ta1 = nextFY * M_PI; 53 | 54 | attribs_[index].position = [self positionForHorizontalAngle:ha topAngle:ta0 radius:r]; 55 | attribs_[index].texCoord = CC3VectorMake(fx, fy, 0); 56 | attribs_[index].bumpAxisX = [self bumpAxisXForHorizontalAngle:ha topAngle:ta0]; 57 | attribs_[index].bumpAxisY = [self bumpAxisYForHorizontalAngle:ha topAngle:ta0]; 58 | 59 | attribs_[index+1].position = [self positionForHorizontalAngle:ha topAngle:ta1 radius:r]; 60 | attribs_[index+1].texCoord = CC3VectorMake(fx, nextFY, 0); 61 | attribs_[index+1].bumpAxisX = [self bumpAxisXForHorizontalAngle:ha topAngle:ta1]; 62 | attribs_[index+1].bumpAxisY = [self bumpAxisYForHorizontalAngle:ha topAngle:ta1]; 63 | } 64 | } 65 | } 66 | return self; 67 | } 68 | 69 | - (CC3Vector)bumpAxisXForHorizontalAngle:(float)ha topAngle:(float)ta { 70 | if(ta == 0) ta = 0.001; 71 | if(ABS(ta - M_PI) < 0.001) ta = M_PI - 0.001; 72 | CC3Vector axis = CC3VectorNormalize(CC3VectorDifference([self positionForHorizontalAngle:ha + 0.001 topAngle:ta radius:10], 73 | [self positionForHorizontalAngle:ha - 0.001 topAngle:ta radius:10])); 74 | return axis; 75 | } 76 | 77 | 78 | - (CC3Vector)bumpAxisYForHorizontalAngle:(float)ha topAngle:(float)ta { 79 | if(ta == 0) ta = 0.001; 80 | if(ABS(ta - M_PI) < 0.001) ta = M_PI - 0.001; 81 | CC3Vector axis = CC3VectorNormalize(CC3VectorDifference([self positionForHorizontalAngle:ha topAngle:ta + 0.001 radius:10], 82 | [self positionForHorizontalAngle:ha topAngle:ta - 0.001 radius:10])); 83 | return axis; 84 | } 85 | 86 | 87 | - (CC3Vector)positionForHorizontalAngle:(float)ha topAngle:(float)ta radius:(float)r { 88 | return CC3VectorMake(r * sin(ta) * cos(ha), r * cos(ta), r * sin(ta) * sin(ha)); 89 | } 90 | 91 | - (void)dealloc { 92 | free(attribs_); 93 | 94 | self.texture = nil; 95 | self.bumpMap = nil; 96 | 97 | [super dealloc]; 98 | } 99 | 100 | + (REProgram*)program { 101 | return [REProgram programWithVertexFilename:@"sBumpSphere.vsh" fragmentFilename:@"sBumpSphere.fsh"]; 102 | } 103 | 104 | - (void)draw { 105 | 106 | [super draw]; 107 | 108 | GLint a_position = [self.program attribLocation:@"a_position"]; 109 | GLint a_texCoord = [self.program attribLocation:@"a_texCoord"]; 110 | GLint a_bumpAxisX = [self.program attribLocation:@"a_bumpAxisX"]; 111 | GLint a_bumpAxisY = [self.program attribLocation:@"a_bumpAxisY"]; 112 | 113 | 114 | glUniform1i([self.program uniformLocation:@"s_texture"], 0); 115 | [texture_ bind:GL_TEXTURE0]; 116 | 117 | glUniform1i([self.program uniformLocation:@"s_bumpMap"], 1); 118 | [bumpMap_ bind:GL_TEXTURE1]; 119 | 120 | glUniform1f([self.program uniformLocation:@"u_shinyness"], shinyness_); 121 | glUniform1f([self.program uniformLocation:@"u_specularLightBrightness"], specularLightBrightness_); 122 | glUniform1f([self.program uniformLocation:@"u_bumpMapOffset"], bumpMapOffset_); 123 | 124 | glEnableVertexAttribArray(a_position); 125 | glEnableVertexAttribArray(a_texCoord); 126 | glEnableVertexAttribArray(a_bumpAxisX); 127 | glEnableVertexAttribArray(a_bumpAxisY); 128 | 129 | glVertexAttribPointer(a_position, 3, GL_FLOAT, GL_FALSE, sizeof(SphereNodeAttribs), (void*)(attribs_) + offsetof(SphereNodeAttribs, position)); 130 | glVertexAttribPointer(a_texCoord, 3, GL_FLOAT, GL_FALSE, sizeof(SphereNodeAttribs), (void*)(attribs_) + offsetof(SphereNodeAttribs, texCoord)); 131 | glVertexAttribPointer(a_bumpAxisX, 3, GL_FLOAT, GL_FALSE, sizeof(SphereNodeAttribs), (void*)(attribs_) + offsetof(SphereNodeAttribs, bumpAxisX)); 132 | glVertexAttribPointer(a_bumpAxisY, 3, GL_FLOAT, GL_FALSE, sizeof(SphereNodeAttribs), (void*)(attribs_) + offsetof(SphereNodeAttribs, bumpAxisY)); 133 | 134 | glDrawArrays(GL_TRIANGLE_STRIP, 0, nAttribs_); 135 | } 136 | 137 | @end 138 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/SphereViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SphereViewController.h 3 | // dgi12Projekt 4 | // 5 | // Created by Anton Holmberg on 2012-05-25. 6 | // Copyright (c) 2012 Anton Holmberg. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "SphereNode.h" 11 | 12 | @interface SphereViewController : UIViewController { 13 | 14 | // Rend classes 15 | REGLView *glView_; 16 | REDirector *director_; 17 | REScene *scene_; 18 | RECamera *camera_; 19 | REWorld *world_; 20 | 21 | // The sphere 22 | SphereNode *sphereNode_; 23 | int sphereTextureIndex_; 24 | 25 | // Touch / rotation handling 26 | CGPoint lastDragPoints_[2]; 27 | CC3Vector sphereRotationSpeed_; 28 | RENode *sphereRotationXNode_; 29 | RENode *sphereRotationYNode_; 30 | BOOL isTouchingSphere_; 31 | CGPoint touchStartPoint_; 32 | BOOL tapIsValid_; 33 | 34 | // Sliders 35 | IBOutlet UISlider *shinynessSlider_; 36 | IBOutlet UISlider *lightStrengthSlider_; 37 | IBOutlet UISlider *bumpOffsetSlider_; 38 | IBOutlet UIView *loadingView_; 39 | IBOutlet UILabel *progressLabel_; 40 | IBOutlet UIActivityIndicatorView *activityView_; 41 | 42 | // Images 43 | NSMutableArray *textureImages_; 44 | NSMutableArray *bumpMapImages_; 45 | } 46 | 47 | - (IBAction)shinynessChanged:(UISlider *)slider; 48 | - (IBAction)lightStrengthChanged:(UISlider *)slider; 49 | - (IBAction)bumpOffsetChanged:(UISlider *)slider; 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/TeapotAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/TeapotAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "TeapotAppDelegate.h" 10 | #import "GLViewController.h" 11 | #import "TeapotController.h" 12 | 13 | @implementation AppDelegate 14 | 15 | @synthesize window = _window; 16 | 17 | - (void)dealloc 18 | { 19 | [_window release]; 20 | [super dealloc]; 21 | } 22 | 23 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 24 | 25 | EAGLContext *context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease]; 26 | [EAGLContext setCurrentContext:context]; 27 | 28 | self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 29 | // Override point for customization after application launch. 30 | 31 | TeapotController *vc = [[[TeapotController alloc] init] autorelease]; 32 | self.window.rootViewController = vc; 33 | 34 | self.window.backgroundColor = [UIColor whiteColor]; 35 | [self.window makeKeyAndVisible]; 36 | return YES; 37 | } 38 | 39 | - (void)applicationWillResignActive:(UIApplication *)application 40 | { 41 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 42 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 43 | } 44 | 45 | - (void)applicationDidEnterBackground:(UIApplication *)application 46 | { 47 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 48 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 49 | } 50 | 51 | - (void)applicationWillEnterForeground:(UIApplication *)application 52 | { 53 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 54 | } 55 | 56 | - (void)applicationDidBecomeActive:(UIApplication *)application 57 | { 58 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 59 | } 60 | 61 | - (void)applicationWillTerminate:(UIApplication *)application 62 | { 63 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 64 | } 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/TeapotController.h: -------------------------------------------------------------------------------- 1 | // 2 | // TeapotController.h 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "GLViewController.h" 10 | #import "TeapotNode.h" 11 | 12 | @interface TeapotController : GLViewController { 13 | TeapotNode *teapotNode_; 14 | } 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/TeapotController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TeapotController.m 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "TeapotController.h" 10 | 11 | @interface TeapotController () 12 | 13 | @end 14 | 15 | @implementation TeapotController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | 20 | 21 | RELight *light = [REDirectionalLight light]; 22 | [self.world addLight:light]; 23 | 24 | 25 | REWavefrontMesh *teapotMesh = [REMeshCache meshNamed:@"teapot.obj"]; 26 | 27 | teapotNode_ = [[[TeapotNode alloc] initWithDefaultMesh:teapotMesh] autorelease]; 28 | teapotNode_.rotationAxis = CC3VectorMake(0.1, 1, 0.3); 29 | teapotNode_.material.ambient = CC3Vector4Make(0.3, 0.3, 0.3, 1.0); 30 | teapotNode_.material.diffuse = CC3Vector4Make(0.4, 0.2, 0.2, 1.0); 31 | teapotNode_.material.specular = CC3Vector4Make(0.5, 0.6, 0.5, 1.0); 32 | teapotNode_.material.shininess = 24; 33 | [teapotNode_ setSizeX:200]; 34 | [self.world addChild:teapotNode_]; 35 | } 36 | 37 | - (void)viewDidUnload { 38 | [super viewDidUnload]; 39 | teapotNode_ = nil; 40 | } 41 | 42 | - (void)update:(float)dt { 43 | static float angle = 0; 44 | 45 | angle += 0.4; 46 | teapotNode_.rotationAngle = angle; 47 | 48 | 49 | 50 | 51 | } 52 | 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/TeapotNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // TeapotNode.h 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "REKeyframedMeshNode.h" 10 | 11 | @interface TeapotNode : REKeyframedMeshNode 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/TeapotNode.m: -------------------------------------------------------------------------------- 1 | // 2 | // TeapotNode.m 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "TeapotNode.h" 10 | 11 | @implementation TeapotNode 12 | 13 | 14 | + (REProgram*)program { 15 | //return [REProgram programWithVertexFilename:@"sVertexLighting.vsh" fragmentFilename:@"sVertexLighting.fsh"]; 16 | return [REProgram programWithVertexFilename:@"sFragmentLighting.vsh" fragmentFilename:@"sFragmentLighting.fsh"]; 17 | } 18 | 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/UIImageAdditions.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define kEncodingKey @"UIImage" 5 | 6 | static void __attribute__((constructor)) initialize() { 7 | @autoreleasepool { 8 | 9 | if (![[UIImage class] conformsToProtocol:@protocol(NSCoding)]) { 10 | Class class = [UIImage class]; 11 | 12 | if (!class_addMethod( 13 | class, 14 | @selector(initWithCoder:), 15 | class_getMethodImplementation(class, @selector(initWithCoderForArchiver:)), 16 | protocol_getMethodDescription(@protocol(NSCoding), @selector(initWithCoder:), YES, YES).types 17 | )) { 18 | NSLog(@"Critical Error - [UIImage initWithCoder:] not defined."); 19 | } 20 | 21 | if (!class_addMethod( 22 | class, 23 | @selector(encodeWithCoder:), 24 | class_getMethodImplementation(class, @selector(encodeWithCoderForArchiver:)), 25 | protocol_getMethodDescription(@protocol(NSCoding), @selector(encodeWithCoder:), YES, YES).types 26 | )) { 27 | NSLog(@"Critical Error - [UIImage encodeWithCoder:] not defined."); 28 | } 29 | 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/UIImageAdditions.m: -------------------------------------------------------------------------------- 1 | 2 | #import "UIImageAdditions.h" 3 | 4 | @implementation UIImage(NSCoding) 5 | 6 | - (id) initWithCoderForArchiver:(NSCoder *)decoder { 7 | 8 | if ((self = [super init])) 9 | { 10 | NSData *data = [decoder decodeObjectForKey:kEncodingKey]; 11 | self = [self initWithData:data]; 12 | } 13 | 14 | return self; 15 | 16 | } 17 | 18 | - (void) encodeWithCoderForArchiver:(NSCoder *)encoder { 19 | 20 | NSData *data = UIImagePNGRepresentation(self); 21 | [encoder encodeObject:data forKey:kEncodingKey]; 22 | 23 | } 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/UIViewAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewAdditions.h 3 | // Template 4 | // 5 | // Created by Monterosa iOS Team 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface UIView (Additions) 13 | 14 | #pragma mark - Origin / Size 15 | 16 | @property (nonatomic, assign) CGPoint origin; 17 | @property (nonatomic, assign) CGSize size; 18 | @property (nonatomic, assign) CGFloat left; 19 | @property (nonatomic, assign) CGFloat right; 20 | @property (nonatomic, assign) CGFloat top; 21 | @property (nonatomic, assign) CGFloat bottom; 22 | @property (nonatomic, assign) CGFloat width; 23 | @property (nonatomic, assign) CGFloat height; 24 | @property (nonatomic, assign) CGFloat centerX; 25 | @property (nonatomic, assign) CGFloat centerY; 26 | 27 | #pragma mark - Centering 28 | 29 | - (void)centerInSuperview; 30 | - (void)centerHorizontallyInSuperview; 31 | - (void)centerVerticallyInSuperview; 32 | 33 | // Creates snapshop of current content and returns image. 34 | - (UIImage *)snapshot; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/UIViewAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewAdditions.h 3 | // Template 4 | // 5 | // Created by Monterosa iOS Team 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import "UIViewAdditions.h" 10 | 11 | @implementation UIView (Additions) 12 | 13 | #pragma mark - Origin / Size 14 | 15 | - (CGPoint)origin { 16 | return CGPointMake(self.frame.origin.x, self.frame.origin.y); 17 | } 18 | 19 | - (void)setOrigin:(CGPoint)origin { 20 | self.frame = CGRectMake(origin.x, origin.y, self.frame.size.width, self.frame.size.height); 21 | } 22 | 23 | - (CGSize)size { 24 | return CGSizeMake(self.frame.size.width, self.frame.size.height); 25 | } 26 | 27 | - (void)setSize:(CGSize)size { 28 | self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, size.width, size.height); 29 | } 30 | 31 | #pragma mark - Centering 32 | 33 | - (void)centerInSuperview { 34 | self.frame = CGRectMake(round(self.superview.frame.size.width / 2 - self.frame.size.width / 2), round(self.superview.frame.size.height / 2 - self.frame.size.height / 2), self.frame.size.width, self.frame.size.height); 35 | } 36 | 37 | - (void)centerHorizontallyInSuperview { 38 | self.frame = CGRectMake(round(self.superview.frame.size.width / 2 - self.frame.size.width / 2), self.frame.origin.y, self.frame.size.width, self.frame.size.height); 39 | } 40 | 41 | - (void)centerVerticallyInSuperview { 42 | self.frame = CGRectMake(self.frame.origin.x, round(self.superview.frame.size.height / 2 - self.frame.size.height / 2), self.frame.size.width, self.frame.size.height); 43 | } 44 | 45 | #pragma mark - Positioning 46 | 47 | - (CGFloat)left { 48 | return self.frame.origin.x; 49 | } 50 | 51 | - (void)setLeft:(CGFloat)x { 52 | CGRect frame = self.frame; 53 | frame.origin.x = x; 54 | self.frame = frame; 55 | } 56 | 57 | - (CGFloat)top { 58 | return self.frame.origin.y; 59 | } 60 | 61 | - (void)setTop:(CGFloat)y { 62 | CGRect frame = self.frame; 63 | frame.origin.y = y; 64 | self.frame = frame; 65 | } 66 | 67 | 68 | - (CGFloat)right { 69 | return self.frame.origin.x + self.frame.size.width; 70 | } 71 | 72 | - (void)setRight:(CGFloat)right { 73 | CGRect frame = self.frame; 74 | frame.origin.x = right - frame.size.width; 75 | self.frame = frame; 76 | } 77 | 78 | - (CGFloat)bottom { 79 | return self.frame.origin.y + self.frame.size.height; 80 | } 81 | 82 | - (void)setBottom:(CGFloat)bottom { 83 | CGRect frame = self.frame; 84 | frame.origin.y = bottom - frame.size.height; 85 | self.frame = frame; 86 | } 87 | 88 | - (CGFloat)centerX { 89 | return self.center.x; 90 | } 91 | 92 | - (void)setCenterX:(CGFloat)centerX { 93 | self.center = CGPointMake(centerX, self.center.y); 94 | } 95 | 96 | - (CGFloat)centerY { 97 | return self.center.y; 98 | } 99 | 100 | - (void)setCenterY:(CGFloat)centerY { 101 | self.center = CGPointMake(self.center.x, centerY); 102 | } 103 | 104 | - (CGFloat)width { 105 | return self.frame.size.width; 106 | } 107 | 108 | - (void)setWidth:(CGFloat)width { 109 | CGRect frame = self.frame; 110 | frame.size.width = width; 111 | self.frame = frame; 112 | } 113 | 114 | - (CGFloat)height { 115 | return self.frame.size.height; 116 | } 117 | 118 | - (void)setHeight:(CGFloat)height { 119 | CGRect frame = self.frame; 120 | frame.size.height = height; 121 | self.frame = frame; 122 | } 123 | 124 | #pragma mark - Render 125 | 126 | - (UIImage *)snapshot { 127 | UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0); 128 | [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 129 | UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 130 | UIGraphicsEndImageContext(); 131 | return img; 132 | } 133 | 134 | 135 | @end 136 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Rend Example Collection 4 | // 5 | // Created by Anton Holmquist on 6/26/12. 6 | // Copyright (c) 2012 Monterosa. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | NSAutoreleasePool *pool = [NSAutoreleasePool new]; 14 | UIApplicationMain(argc, argv, nil, @"AppDelegate"); 15 | [pool release]; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/sBumpSphere.fsh: -------------------------------------------------------------------------------- 1 | 2 | precision highp float; 3 | 4 | uniform sampler2D s_texture; 5 | uniform sampler2D s_bumpMap; 6 | 7 | uniform float u_shinyness; 8 | uniform float u_specularLightBrightness; 9 | uniform float u_bumpMapOffset; 10 | 11 | varying vec2 v_texCoord; 12 | varying vec3 v_normal; 13 | 14 | varying vec3 v_bumpAxisX; 15 | varying vec3 v_bumpAxisY; 16 | 17 | void main() { 18 | vec4 pixelColor = texture2D(s_texture, v_texCoord); 19 | 20 | vec4 bumpOffset = -0.5 + texture2D(s_bumpMap, v_texCoord); 21 | vec3 normal = normalize(v_normal + u_bumpMapOffset * bumpOffset.x * normalize(v_bumpAxisX) + u_bumpMapOffset * bumpOffset.y * normalize(v_bumpAxisY)); 22 | vec3 directionToLight = normalize(vec3(-1, 1, 1)); 23 | vec3 directionToViewer = vec3(0, 0, 1); 24 | 25 | // Diffuse light 26 | float diffuseLight = max(dot(normal, directionToLight), 0.0); 27 | pixelColor.rgb = pixelColor.rgb * (0.3 + 0.7 * diffuseLight); 28 | 29 | // Specular light 30 | vec3 reflectanceDirection = normalize(2.0 * dot(normal, directionToLight) * normal - directionToLight); 31 | float sl = max(dot(reflectanceDirection, directionToViewer), 0.0); 32 | float specularLight = pow(sl, u_shinyness); 33 | pixelColor.rgb = pixelColor.rgb + u_specularLightBrightness * specularLight; 34 | 35 | gl_FragColor = pixelColor; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/sBumpSphere.vsh: -------------------------------------------------------------------------------- 1 | 2 | uniform mat4 u_mvpMatrix; 3 | uniform mat4 u_mvMatrix; 4 | 5 | attribute vec3 a_position; 6 | attribute vec2 a_texCoord; 7 | attribute vec3 a_bumpAxisX; 8 | attribute vec3 a_bumpAxisY; 9 | 10 | varying vec2 v_texCoord; 11 | varying vec3 v_normal; 12 | varying vec3 v_bumpAxisX; 13 | varying vec3 v_bumpAxisY; 14 | 15 | 16 | void main() { 17 | v_texCoord = a_texCoord; 18 | v_bumpAxisX = (u_mvMatrix * vec4(a_bumpAxisX, 0)).xyz; 19 | v_bumpAxisY = (u_mvMatrix * vec4(a_bumpAxisY, 0)).xyz; 20 | v_normal = (u_mvMatrix * vec4(a_position, 0)).xyz; 21 | gl_Position = u_mvpMatrix * vec4(a_position, 1); 22 | } -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/sFragmentLighting.fsh: -------------------------------------------------------------------------------- 1 | // 2 | // sFragmentLighting.fsh 3 | // 4 | // Created by Anton Holmquist 5 | // Copyright 2012 Anton Holmquist. All rights reserved. 6 | // 7 | // http://antonholmquist.com 8 | // http://twitter.com/antonholmquist 9 | // 10 | // http://github.com/antonholmquist/opengl-es-2-0-shaders 11 | // 12 | 13 | precision highp float; 14 | 15 | struct DirectionalLight { 16 | vec3 direction; 17 | vec3 halfplane; 18 | vec4 ambientColor; 19 | vec4 diffuseColor; 20 | vec4 specularColor; 21 | }; 22 | 23 | struct Material { 24 | vec4 ambientFactor; 25 | vec4 diffuseFactor; 26 | vec4 specularFactor; 27 | float shininess; 28 | }; 29 | 30 | // Light 31 | uniform DirectionalLight u_directionalLight; 32 | 33 | // Material 34 | uniform Material u_material; 35 | 36 | varying vec3 v_ecNormal; 37 | 38 | void main() { 39 | 40 | 41 | // Normalize v_ecNormal 42 | vec3 ecNormal = v_ecNormal / length(v_ecNormal); 43 | 44 | float ecNormalDotLightDirection = max(0.0, dot(ecNormal, u_directionalLight.direction)); 45 | float ecNormalDotLightHalfplane = max(0.0, dot(ecNormal, u_directionalLight.halfplane)); 46 | 47 | // Calculate ambient light 48 | vec4 ambientLight = u_directionalLight.ambientColor * u_material.ambientFactor; 49 | 50 | // Calculate diffuse light 51 | vec4 diffuseLight = ecNormalDotLightDirection * u_directionalLight.diffuseColor * u_material.diffuseFactor; 52 | 53 | // Calculate specular light 54 | vec4 specularLight = vec4(0.0); 55 | if (ecNormalDotLightHalfplane > 0.0) { 56 | specularLight = pow(ecNormalDotLightHalfplane, u_material.shininess) * u_directionalLight.specularColor * u_material.specularFactor; 57 | } 58 | 59 | vec4 light = ambientLight + diffuseLight + specularLight; 60 | 61 | gl_FragColor = light; 62 | } 63 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/sFragmentLighting.vsh: -------------------------------------------------------------------------------- 1 | // 2 | // sFragmentLighting.vsh 3 | // 4 | // Created by Anton Holmquist 5 | // Copyright 2012 Anton Holmquist. All rights reserved. 6 | // 7 | // http://antonholmquist.com 8 | // http://twitter.com/antonholmquist 9 | // 10 | // http://github.com/antonholmquist/opengl-es-2-0-shaders 11 | // 12 | 13 | precision highp float; 14 | 15 | // Matrices 16 | uniform mat4 u_mvMatrix; 17 | uniform mat4 u_mvpMatrix; 18 | 19 | // Attributes 20 | attribute vec4 a_position; 21 | attribute vec3 a_normal; 22 | 23 | // Varyings 24 | varying vec3 v_ecNormal; 25 | 26 | void main() { 27 | 28 | // Define position and normal in model coordinates 29 | vec4 mcPosition = a_position; 30 | vec3 mcNormal = a_normal; 31 | 32 | // Calculate and normalize eye space normal 33 | vec3 ecNormal = vec3(u_mvMatrix * vec4(mcNormal, 0.0)); 34 | ecNormal = ecNormal / length(ecNormal); 35 | v_ecNormal = ecNormal; 36 | 37 | gl_Position = u_mvpMatrix * mcPosition; 38 | 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/sVertexLighting.fsh: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | precision highp float; 28 | 29 | varying vec4 v_light; 30 | 31 | void main() { 32 | gl_FragColor = v_light; 33 | } 34 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/Rend Example Collection/sVertexLighting.vsh: -------------------------------------------------------------------------------- 1 | /* 2 | * Rend 3 | * 4 | * Author: Anton Holmquist 5 | * Copyright (c) 2012 Anton Holmquist All rights reserved. 6 | * http://antonholmquist.com 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | struct DirectionalLight { 28 | vec3 direction; 29 | vec3 halfplane; 30 | vec4 ambientColor; 31 | vec4 diffuseColor; 32 | vec4 specularColor; 33 | }; 34 | 35 | struct Material { 36 | vec4 ambientFactor; 37 | vec4 diffuseFactor; 38 | vec4 specularFactor; 39 | float shininess; 40 | }; 41 | 42 | // Light 43 | uniform DirectionalLight u_directionalLight; 44 | 45 | // Material 46 | uniform Material u_material; 47 | 48 | // Matrices 49 | uniform mat4 u_mvMatrix; 50 | uniform mat4 u_mvpMatrix; 51 | 52 | // Attributes 53 | attribute vec4 a_position; 54 | attribute vec3 a_normal; 55 | 56 | // Varyings 57 | varying vec4 v_light; 58 | 59 | void main() { 60 | 61 | // Define position and normal in model coordinates 62 | vec4 mcPosition = a_position; 63 | vec3 mcNormal = a_normal; 64 | 65 | // Calculate and normalize eye space normal 66 | vec3 ecNormal = vec3(u_mvMatrix * vec4(mcNormal, 0.0)); 67 | ecNormal = ecNormal / length(ecNormal); 68 | 69 | // Do light calculations 70 | float ecNormalDotLightDirection = max(0.0, dot(ecNormal, u_directionalLight.direction)); 71 | float ecNormalDotLightHalfplane = max(0.0, dot(ecNormal, u_directionalLight.halfplane)); 72 | 73 | // Ambient light 74 | vec4 ambientLight = u_directionalLight.ambientColor * u_material.ambientFactor; 75 | 76 | // Diffuse light 77 | vec4 diffuseLight = ecNormalDotLightDirection * u_directionalLight.diffuseColor * u_material.diffuseFactor; 78 | 79 | // Specular light 80 | vec4 specularLight = vec4(0.0); 81 | if (ecNormalDotLightHalfplane > 0.0) { 82 | specularLight = pow(ecNormalDotLightHalfplane, u_material.shininess) * u_directionalLight.specularColor * u_material.specularFactor; 83 | } 84 | 85 | v_light = ambientLight + diffuseLight + specularLight; 86 | 87 | gl_Position = u_mvpMatrix * mcPosition; 88 | 89 | } 90 | 91 | 92 | -------------------------------------------------------------------------------- /Examples/Rend Example Collection/TeapotExample copy-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.rend.examplecollection.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /LICENSE.cocos2d: -------------------------------------------------------------------------------- 1 | cocos2d for iPhone: http://www.cocos2d-iphone.org 2 | 3 | Copyright (c) 2008-2010 - Ricardo Quesada and contributors 4 | (see each file to see the different copyright owners) 5 | 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /LICENSE.cocos3d: -------------------------------------------------------------------------------- 1 | COCOS3D LICENSE 2 | 3 | cocos3d 0.6.5 4 | 5 | Copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved. 6 | http://www.brenwill.com 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | 26 | http://en.wikipedia.org/wiki/MIT_License 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE.rend: -------------------------------------------------------------------------------- 1 | Rend License 2 | 3 | Author: Anton Holmquist 4 | Copyright (c) 2012 Anton Holmquist All rights reserved. 5 | http://antonholmquist.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rend - A lightweight OpenGL ES 2.0 framework for iOS 2 | ==================== 3 | Author: Anton Holmquist | http://antonholmquist.com 4 | 5 | Rend is a very lightweight OpenGL ES 2.0 framework designed for pure rendering and to be easily integrated with UIKit. 6 | 7 | Why another framework? 8 | -------------------- 9 | Rend is similar in some senses to Cocos2d/3d, but it's lighter and very flexible which may suit some kind of projects better. If you're writing a game you should probably look at Cocos or Unity, but if you want to create a rendering or interface component to be integrated into your UIKit-based project, this framework may be well suited! 10 | 11 | Background 12 | -------------------- 13 | >When I was looking for a framework I looked at three existing options, Cocos2d, Cocos3d and Unity. None of those seemed perfect for my needs. Cocos2d obviously doesn’t have very good 3D support, Cocos3d didn't have shader support, and Unity seemed too bloated and hard to integrate with UIKit. 14 | 15 | The full story can be found in [this blog post](http://antonholmquist.com/blog/introducing-rend-a-lightweight-objective-c-opengl-es-2-0-framework-ios/). 16 | 17 | 18 | Example usage 19 | -------------------- 20 | The first thing to do is to create a view where we can draw our content. 21 | 22 | REGLView *view = [[REGLView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; 23 | 24 | 25 | Next, we create a scene that acts as a root node in our scene graph hierarchy. All content should be added as children to this node. 26 | 27 | REScene *scene = [[REScene alloc] init]; 28 | 29 | Next, we create a director that is responsible for managing drawing and connecting the view and scene: 30 | 31 | REDirector *director = [[REDirector alloc] init]; 32 | director.view = view; 33 | director.scene = scene; 34 | 35 | We also need to attach a camera to our scene. The camera requires some configuration so we place it at (0,0,1) facing at origo. This framework uses math functions from Cocos3d which is why you see the CC3 prefix. 36 | 37 | RECamera *camera = [[RECamera alloc] initWithProjection:kRECameraProjectionPerspective]; 38 | camera.position = CC3VectorMake(0, 0, 1); 39 | camera.upDirection = CC3VectorMake(0, 1, 0); 40 | camera.lookDirection = CC3VectorMake(0, 0, -1); 41 | camera.frustumLeft = -1; 42 | camera.frustumRight = 1; 43 | camera.frustumBottom = -1; 44 | camera.frustumTop = 1; 45 | camera.frustumNear = 0.5; 46 | camera.frustumFar = 2; 47 | 48 | 49 | Finally, we add a sprite to our scene, positioning it at origo and setting width and height to 1.0. We also need to select a texture from the app bundle and add the node to our scene. 50 | 51 | RESprite *sprite = [[RESprite alloc] init]; 52 | sprite.positon = CC3VectorMake(0.0, 0.0, 0.0); 53 | sprite.size = CC3VectorMake(1.0, 1.0, 0.0); 54 | sprite.texture = [RETexture2D textureNamed:@"test.png"]; 55 | [scene addChild:sprite]; 56 | 57 | Now everything is set up. The only thing left is to make the director run. 58 | 59 | director.running = YES; 60 | 61 | So that's a very basic example on how to draw a sprite. There are some more advanced examples provided with the repository. -------------------------------------------------------------------------------- /Shaders/sRESprite.fsh: -------------------------------------------------------------------------------- 1 | 2 | 3 | precision mediump float; 4 | 5 | uniform sampler2D s_texture; 6 | uniform vec4 u_multiplyColor; 7 | 8 | varying vec2 v_texCoord; 9 | 10 | void main() { 11 | vec4 pixelColor = texture2D(s_texture, v_texCoord); 12 | 13 | pixelColor = pixelColor * u_multiplyColor; 14 | 15 | gl_FragColor = pixelColor; 16 | } 17 | -------------------------------------------------------------------------------- /Shaders/sRESprite.vsh: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | uniform mat4 u_mvMatrix; 5 | uniform mat4 u_pMatrix; 6 | 7 | attribute vec4 a_position; 8 | attribute vec2 a_texCoord; 9 | 10 | varying vec2 v_texCoord; 11 | 12 | void main() { 13 | v_texCoord = a_texCoord; 14 | gl_Position = u_pMatrix * u_mvMatrix * a_position; 15 | } -------------------------------------------------------------------------------- /Shaders/sRESpriteBatch.fsh: -------------------------------------------------------------------------------- 1 | 2 | precision mediump float; 3 | 4 | uniform sampler2D s_texture; // All sprites in batch use the same texture 5 | 6 | varying mediump vec2 v_texCoord; 7 | varying lowp vec4 v_multiplyColor; 8 | 9 | void main() { 10 | //gl_FragColor = texture2D(s_texture, v_texCoord) * v_multiplyColor; 11 | 12 | // Multiply with multiply color, and then with alpha, to simulate that the alpha was premultiplied. 13 | gl_FragColor = texture2D(s_texture, v_texCoord) * v_multiplyColor * vec4(v_multiplyColor.a); 14 | 15 | } 16 | 17 | // More on premultiplied alpha: 18 | // http://blog.rarepebble.com/111/premultiplied-alpha-in-opengl/ 19 | // http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D -------------------------------------------------------------------------------- /Shaders/sRESpriteBatch.vsh: -------------------------------------------------------------------------------- 1 | 2 | 3 | attribute mediump float a_batchUnit; // batch index may be bad name? it's actually kind of sub-batchindex or "group". maybe should be called batchunit/element 4 | 5 | uniform mat4 u_mvMatrix[24]; // Variable mv matrix 6 | uniform mat4 u_pMatrix; 7 | uniform vec4 u_multiplyColor[24]; // Variable multiply color matrix 8 | 9 | 10 | attribute highp vec4 a_position; 11 | attribute mediump vec2 a_texCoord; 12 | 13 | varying mediump vec2 v_texCoord; 14 | varying lowp vec4 v_multiplyColor; 15 | 16 | void main() { 17 | 18 | int batchUnit = int(a_batchUnit); 19 | 20 | v_multiplyColor = u_multiplyColor[batchUnit]; 21 | 22 | v_texCoord = a_texCoord; 23 | gl_Position = u_pMatrix * u_mvMatrix[batchUnit] * a_position; 24 | } --------------------------------------------------------------------------------