Refactor for whitespace and clarity

This commit is contained in:
Aaron Culliney 2014-10-07 21:59:21 -07:00
parent d083fa958f
commit e696f7882f
14 changed files with 1535 additions and 2146 deletions

View File

@ -100,10 +100,8 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
// Must specify the 3.2 Core Profile to use OpenGL 3.2
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
NSOpenGLPFAOpenGLProfile,
NSOpenGLProfileVersion3_2Core,
#endif
0
};
@ -116,7 +114,7 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt
NSOpenGLContext* context = [[[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] autorelease];
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 && defined(DEBUG)
#if defined(DEBUG)
// When we're using a CoreProfile context, crash if we call a legacy OpenGL function
// This will make it much more obvious where and when such a function call is made so
// that we can remove such calls.

View File

@ -6,7 +6,7 @@
// Copyright (c) 2014 deadc0de.org. All rights reserved.
//
#if ESSENTIAL_GL_PRACTICES_IOS
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
@ -21,7 +21,7 @@ int main(int argc_, const char *argv_[])
argc = argc_;
argv = argv_;
#if ESSENTIAL_GL_PRACTICES_IOS
#if TARGET_OS_IPHONE
@autoreleasepool {
retVal = UIApplicationMain(argc, argv, nil, nil);
}

View File

@ -305,7 +305,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc
// with the proper version preprocessor string prepended
float glLanguageVersion;
#if ESSENTIAL_GL_PRACTICES_IOS
#if TARGET_OS_IPHONE
sscanf((char *)glGetString(GL_SHADING_LANGUAGE_VERSION), "OpenGL ES GLSL ES %f", &glLanguageVersion);
#else
sscanf((char *)glGetString(GL_SHADING_LANGUAGE_VERSION), "%f", &glLanguageVersion);

View File

@ -1,145 +1,77 @@
/*
File: glUtil.h
Abstract:
Includes the appropriate OpenGL headers (depending on whether this
is built for iOS or OSX) and provides some API utility functions
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __GL_UTIL_H__
#define __GL_UTIL_H__
#if ESSENTIAL_GL_PRACTICES_IOS
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#else
#import <OpenGL/OpenGL.h>
// OpenGL 3.2 is only supported on MacOS X Lion and later
// CGL_VERSION_1_3 is defined as 1 on MacOS X Lion and later
#if CGL_VERSION_1_3
// Set to 0 to run on the Legacy OpenGL Profile
#define ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 1
#if defined(__APPLE__)
# include <TargetConditionals.h>
# if TARGET_OS_IPHONE
# import <OpenGLES/ES2/gl.h>
# import <OpenGLES/ES2/glext.h>
# else
# import <OpenGL/OpenGL.h>
# import <OpenGL/gl3.h>
# endif
#elif defined(USE_GL3W)
# include <GL3/gl3.h>
# include <GL3/gl3w.h>
#else
#define ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 0
#endif //!CGL_VERSION_1_3
# define GLEW_STATIC
# include <GL/glew.h>
# define FREEGLUT_STATIC
# include <GL/freeglut.h>
#endif
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#import <OpenGL/gl3.h>
#else
#import <OpenGL/gl.h>
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#endif // !ESSENTIAL_GL_PRACTICES_IOS
//The name of the VertexArrayObject are slightly different in
// OpenGLES, OpenGL Core Profile, and OpenGL Legacy
// The arguments are exactly the same across these APIs however
#if ESSENTIAL_GL_PRACTICES_IOS
#define glBindVertexArray glBindVertexArrayOES
#define glGenVertexArrays glGenVertexArraysOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#else
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#define glBindVertexArray glBindVertexArray
#define glGenVertexArrays glGenVertexArrays
#define glGenerateMipmap glGenerateMipmap
#define glDeleteVertexArrays glDeleteVertexArrays
#else
#define glBindVertexArray glBindVertexArrayAPPLE
#define glGenVertexArrays glGenVertexArraysAPPLE
#define glGenerateMipmap glGenerateMipmapEXT
#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#endif //!ESSENTIAL_GL_PRACTICES_IOS
static inline const char * GetGLErrorString(GLenum error)
{
const char *str;
switch( error )
{
case GL_NO_ERROR:
str = "GL_NO_ERROR";
break;
case GL_INVALID_ENUM:
str = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
str = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
str = "GL_INVALID_OPERATION";
break;
static inline const char * GetGLErrorString(GLenum error) {
const char *str;
switch (error) {
case GL_NO_ERROR:
str = "GL_NO_ERROR";
break;
case GL_INVALID_ENUM:
str = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
str = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
str = "GL_INVALID_OPERATION";
break;
#if defined __gl_h_ || defined __gl3_h_
case GL_OUT_OF_MEMORY:
str = "GL_OUT_OF_MEMORY";
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
str = "GL_INVALID_FRAMEBUFFER_OPERATION";
break;
case GL_OUT_OF_MEMORY:
str = "GL_OUT_OF_MEMORY";
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
str = "GL_INVALID_FRAMEBUFFER_OPERATION";
break;
#endif
#if defined __gl_h_
case GL_STACK_OVERFLOW:
str = "GL_STACK_OVERFLOW";
break;
case GL_STACK_UNDERFLOW:
str = "GL_STACK_UNDERFLOW";
break;
case GL_TABLE_TOO_LARGE:
str = "GL_TABLE_TOO_LARGE";
break;
case GL_STACK_OVERFLOW:
str = "GL_STACK_OVERFLOW";
break;
case GL_STACK_UNDERFLOW:
str = "GL_STACK_UNDERFLOW";
break;
case GL_TABLE_TOO_LARGE:
str = "GL_TABLE_TOO_LARGE";
break;
#endif
default:
str = "(ERROR: Unknown Error Enum)";
break;
}
return str;
default:
str = "(ERROR: Unknown Error Enum)";
break;
}
return str;
}
#endif // __GL_UTIL_H__

View File

@ -1,75 +1,34 @@
/*
File: imageUtil.h
Abstract:
Functions for loading an image files for textures.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __IMAGE_UTIL_H__
#define __IMAGE_UTIL_H__
#include "glUtil.h"
typedef struct demoImageRec
{
GLubyte* data;
GLsizei size;
GLuint width;
GLuint height;
GLenum format;
GLenum type;
GLuint rowByteSize;
typedef struct demoImageRec {
GLubyte *data;
GLsizei size;
GLuint width;
GLuint height;
GLenum format;
GLenum type;
GLuint rowByteSize;
} demoImage;
demoImage* imgLoadImage(const char* filepathname, int flipVertical);
demoImage *imgLoadImage(const char *filepathname, int flipVertical);
void imgDestroyImage(demoImage* image);
void imgDestroyImage(demoImage *image);
#endif //__IMAGE_UTIL_H__

View File

@ -1,116 +1,76 @@
/*
File: imageUtil.m
Abstract:
Functions for loading an image files for textures.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "imageUtil.h"
#if ESSENTIAL_GL_PRACTICES_IOS
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif
demoImage* imgLoadImage(const char* filepathname, int flipVertical)
{
NSString *filepathString = [[NSString alloc] initWithUTF8String:filepathname];
#if ESSENTIAL_GL_PRACTICES_IOS
UIImage* imageClass = [[UIImage alloc] initWithContentsOfFile:filepathString];
#else
demoImage *imgLoadImage(const char *filepathname, int flipVertical) {
NSString *filepathString = [[NSString alloc] initWithUTF8String:filepathname];
#if TARGET_OS_IPHONE
UIImage* imageClass = [[UIImage alloc] initWithContentsOfFile:filepathString];
#else
NSImage *nsimage = [[NSImage alloc] initWithContentsOfFile: filepathString];
NSBitmapImageRep *imageClass = [[NSBitmapImageRep alloc] initWithData:[nsimage TIFFRepresentation]];
[nsimage release];
NSBitmapImageRep *imageClass = [[NSBitmapImageRep alloc] initWithData:[nsimage TIFFRepresentation]];
[nsimage release];
#endif
CGImageRef cgImage = imageClass.CGImage;
if (!cgImage)
{
[filepathString release];
[imageClass release];
return NULL;
}
demoImage* image = malloc(sizeof(demoImage));
image->width = CGImageGetWidth(cgImage);
image->height = CGImageGetHeight(cgImage);
image->rowByteSize = image->width * 4;
image->data = malloc(image->height * image->rowByteSize);
image->format = GL_RGBA;
image->type = GL_UNSIGNED_BYTE;
CGContextRef context = CGBitmapContextCreate(image->data, image->width, image->height, 8, image->rowByteSize, CGImageGetColorSpace(cgImage), kCGImageAlphaNoneSkipLast);
CGContextSetBlendMode(context, kCGBlendModeCopy);
if(flipVertical)
{
CGContextTranslateCTM(context, 0.0, image->height);
CGContextScaleCTM(context, 1.0, -1.0);
}
CGContextDrawImage(context, CGRectMake(0.0, 0.0, image->width, image->height), cgImage);
CGContextRelease(context);
if(NULL == image->data)
{
[filepathString release];
[imageClass release];
imgDestroyImage(image);
return NULL;
}
[filepathString release];
[imageClass release];
return image;
CGImageRef cgImage = imageClass.CGImage;
if (!cgImage)
{
[filepathString release];
[imageClass release];
return NULL;
}
demoImage *image = malloc(sizeof(demoImage));
image->width = CGImageGetWidth(cgImage);
image->height = CGImageGetHeight(cgImage);
image->rowByteSize = image->width * 4;
image->data = malloc(image->height * image->rowByteSize);
image->format = GL_RGBA;
image->type = GL_UNSIGNED_BYTE;
CGContextRef context = CGBitmapContextCreate(image->data, image->width, image->height, 8, image->rowByteSize, CGImageGetColorSpace(cgImage), kCGImageAlphaNoneSkipLast);
CGContextSetBlendMode(context, kCGBlendModeCopy);
if (flipVertical) {
CGContextTranslateCTM(context, 0.0, image->height);
CGContextScaleCTM(context, 1.0, -1.0);
}
CGContextDrawImage(context, CGRectMake(0.0, 0.0, image->width, image->height), cgImage);
CGContextRelease(context);
if (!image->data) {
[filepathString release];
[imageClass release];
imgDestroyImage(image);
return NULL;
}
[filepathString release];
[imageClass release];
return image;
}
void imgDestroyImage(demoImage* image) {
free(image->data);
free(image);
}
void imgDestroyImage(demoImage* image)
{
free(image->data);
free(image);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,52 +1,16 @@
/*
File: matrixUtil.h
Abstract:
Functions for performing matrix math.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __MATRIX_UTIL_H__
#define __MATRIX_UTIL_H__
@ -60,99 +24,94 @@
// [ 3 7 11 15 ]
// MTX = LeftHandSideMatrix * RightHandSideMatrix
void mtxMultiply(float* ret, const float* lhs, const float* rhs);
void mtxMultiply(float *ret, const float *lhs, const float *rhs);
// MTX = IdentityMatrix
void mtxLoadIdentity(float* mtx);
void mtxLoadIdentity(float *mtx);
// MTX = Transpos(SRC)
void mtxTranspose(float* mtx, const float* src);
void mtxTranspose(float *mtx, const float *src);
// MTX = src^-1
void mtxInvert(float* mtx, const float* src);
void mtxInvert(float *mtx, const float *src);
// MTX = PerspectiveProjectionMatrix
void mtxLoadPerspective(float* mtx, float fov, float aspect, float nearZ, float farZ);
void mtxLoadPerspective(float *mtx, float fov, float aspect, float nearZ, float farZ);
// MTX = OrthographicProjectionMatrix
void mtxLoadOrthographic(float* mtx,
float left, float right,
float bottom, float top,
float nearZ, float farZ);
void mtxLoadOrthographic(float *mtx, float left, float right, float bottom, float top, float nearZ, float farZ);
// MTX = ObliqueProjectionMatrix(src, clipPlane)
void mtxModifyObliqueProjection(float* mtx, const float* src, const float* plane);
void mtxModifyObliqueProjection(float *mtx, const float *src, const float *plane);
// MTX = TranlationMatrix
void mtxLoadTranslate(float* mtx, float xTrans, float yTrans, float zTrans);
void mtxLoadTranslate(float *mtx, float xTrans, float yTrans, float zTrans);
// MTX = ScaleMatrix
void mtxLoadScale(float* mtx, float xScale, float yScale, float zScale);
void mtxLoadScale(float *mtx, float xScale, float yScale, float zScale);
// MTX = RotateXYZMatrix
void mtxLoadRotate(float*mtx, float deg, float xAxis, float , float zAxis);
void mtxLoadRotate(float *mtx, float deg, float xAxis, float , float zAxis);
// MTX = RotateXMatrix
void mtxLoadRotateX(float* mtx, float deg);
void mtxLoadRotateX(float *mtx, float deg);
// MTX = RotateYMatrix
void mtxLoadRotateY(float* mtx, float deg);
void mtxLoadRotateY(float *mtx, float deg);
// MTX = RotateZMatrix
void mtxLoadRotateZ(float* mtx, float deg);
void mtxLoadRotateZ(float *mtx, float deg);
// MTX = MTX * TranslationMatrix - Similar to glTranslate
void mtxTranslateApply(float* mtx, float xTrans, float yTrans, float zTrans);
void mtxTranslateApply(float *mtx, float xTrans, float yTrans, float zTrans);
// MTX = MTX * ScaleMatrix - Similar to glScale
void mtxScaleApply(float* mtx, float xScale, float yScale, float zScale);
void mtxScaleApply(float *mtx, float xScale, float yScale, float zScale);
// MTX = MTX * RotateXYZMatrix - Similar to glRotate
void mtxRotateApply(float* mtx, float deg, float xAxis, float yAxis, float zAxis);
void mtxRotateApply(float *mtx, float deg, float xAxis, float yAxis, float zAxis);
// MTX = MTX * RotateXMatrix
void mtxRotateXApply(float* mtx, float rad);
void mtxRotateXApply(float *mtx, float rad);
// MTX = MTX * RotateYMatrix
void mtxRotateYApply(float* mtx, float rad);
void mtxRotateYApply(float *mtx, float rad);
// MTX = MTX * RotateZMatrix
void mtxRotateZApply(float* mtx, float rad);
void mtxRotateZApply(float *mtx, float rad);
// MTX = TranslationMatrix * MTX
void mtxTranslateMatrix(float* mtx, float xTrans, float yTrans, float zTrans);
void mtxTranslateMatrix(float *mtx, float xTrans, float yTrans, float zTrans);
// MTX = ScaleMatrix * MTX
void mtxScaleMatrix(float* mtx, float xScale, float yScale, float zScale);
void mtxScaleMatrix(float *mtx, float xScale, float yScale, float zScale);
// MTX = RotateXYZMatrix * MTX
void mtxRotateMatrix(float* mtx, float rad, float xAxis, float yAxis, float zAxis);
void mtxRotateMatrix(float *mtx, float rad, float xAxis, float yAxis, float zAxis);
// MTX = RotateXMatrix * MTX
void mtxRotateXMatrix(float* mtx, float rad);
void mtxRotateXMatrix(float *mtx, float rad);
// MTX = RotateYMatrix * MTX
void mtxRotateYMatrix(float* mtx, float rad);
void mtxRotateYMatrix(float *mtx, float rad);
// MTX = RotateZMatrix * MTX
void mtxRotateZMatrix(float* mtx, float rad);
// 3x3 MTX = 3x3 IdendityMatrix
void mtx3x3LoadIdentity(float* mtx);
void mtxRotateZMatrix(float *mtx, float rad);
// 3x3 MTX = 3x3 IdentityMatrix
void mtx3x3LoadIdentity(float *mtx);
// 3x3 MTX = 3x3 LHS x 3x3 RHS
void mtx3x3Multiply(float* mtx, const float* lhs, const float* rhs);
void mtx3x3Multiply(float *mtx, const float *lhs, const float *rhs);
// 3x3 MTX = TopLeft of MTX
void mtx3x3FromTopLeftOf4x4(float* mtx, const float* src);
// 3x3 MTX = TopLeft of MTX
void mtx3x3FromTopLeftOf4x4(float *mtx, const float *src);
// 3x3 MTX = Transpose(3x3 SRC)
void mtx3x3Transpose(float* mtx, const float* src);
void mtx3x3Transpose(float *mtx, const float *src);
// 3x3 MTX = 3x3 SRC^-1
void mtx3x3Invert(float* mtx, const float* src);
void mtx3x3Invert(float *mtx, const float *src);
#endif //__MATRIX_UTIL_H__

View File

@ -1,422 +1,326 @@
/*
File: modelUtil.c
Abstract:
Functions for loading a model file for vertex arrays. The model file
format used is a simple "binary blob" invented for the purpose of
this sample code.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "modelUtil.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct modelHeaderRec
{
char fileIdentifier[30];
unsigned int majorVersion;
unsigned int minorVersion;
typedef struct modelHeaderRec {
char fileIdentifier[30];
unsigned int majorVersion;
unsigned int minorVersion;
} modelHeader;
typedef struct modelTOCRec
{
unsigned int attribHeaderSize;
unsigned int byteElementOffset;
unsigned int bytePositionOffset;
unsigned int byteTexcoordOffset;
unsigned int byteNormalOffset;
typedef struct modelTOCRec {
unsigned int attribHeaderSize;
unsigned int byteElementOffset;
unsigned int bytePositionOffset;
unsigned int byteTexcoordOffset;
unsigned int byteNormalOffset;
} modelTOC;
typedef struct modelAttribRec
{
unsigned int byteSize;
GLenum datatype;
GLenum primType; //If index data
unsigned int sizePerElement;
unsigned int numElements;
typedef struct modelAttribRec {
unsigned int byteSize;
GLenum datatype;
GLenum primType; //If index data
unsigned int sizePerElement;
unsigned int numElements;
} modelAttrib;
demoModel* mdlLoadModel(const char* filepathname)
{
if(NULL == filepathname)
{
return NULL;
}
demoModel* model = (demoModel*) calloc(sizeof(demoModel), 1);
if(NULL == model)
{
return NULL;
}
size_t sizeRead;
int error;
FILE* curFile = fopen(filepathname, "r");
if(!curFile)
{
mdlDestroyModel(model);
return NULL;
}
modelHeader header;
sizeRead = fread(&header, 1, sizeof(modelHeader), curFile);
if(sizeRead != sizeof(modelHeader))
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
if(strncmp(header.fileIdentifier, "AppleOpenGLDemoModelWWDC2010", sizeof(header.fileIdentifier)))
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
if(header.majorVersion != 0 && header.minorVersion != 1)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
modelTOC toc;
sizeRead = fread(&toc, 1, sizeof(modelTOC), curFile);
if(sizeRead != sizeof(modelTOC))
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
if(toc.attribHeaderSize > sizeof(modelAttrib))
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
modelAttrib attrib;
error = fseek(curFile, toc.byteElementOffset, SEEK_SET);
if(error < 0)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if(sizeRead != toc.attribHeaderSize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->elementArraySize = attrib.byteSize;
model->elementType = attrib.datatype;
model->numElements = attrib.numElements;
// OpenGL ES cannot use UNSIGNED_INT elements
// So if the model has UI element...
if(GL_UNSIGNED_INT == model->elementType)
{
//...Load the UI elements and convert to UNSIGNED_SHORT
GLubyte* uiElements = (GLubyte*) malloc(model->elementArraySize);
size_t ushortElementArraySize = model->numElements * sizeof(GLushort);
model->elements = (GLubyte*)malloc(ushortElementArraySize);
sizeRead = fread(uiElements, 1, model->elementArraySize, curFile);
if(sizeRead != model->elementArraySize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
GLuint elemNum = 0;
for(elemNum = 0; elemNum < model->numElements; elemNum++)
{
//We can't handle this model if an element is out of the UNSIGNED_INT range
if(((GLuint*)uiElements)[elemNum] >= 0xFFFF)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
((GLushort*)model->elements)[elemNum] = ((GLuint*)uiElements)[elemNum];
}
free(uiElements);
model->elementType = GL_UNSIGNED_SHORT;
model->elementArraySize = model->numElements * sizeof(GLushort);
}
else
{
model->elements = (GLubyte*)malloc(model->elementArraySize);
sizeRead = fread(model->elements, 1, model->elementArraySize, curFile);
if(sizeRead != model->elementArraySize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
}
demoModel *mdlLoadModel(const char *filepathname) {
if (!filepathname) {
return NULL;
}
demoModel *model = (demoModel *)calloc(sizeof(demoModel), 1);
if (!model) {
return NULL;
}
fseek(curFile, toc.bytePositionOffset, SEEK_SET);
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if(sizeRead != toc.attribHeaderSize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->positionArraySize = attrib.byteSize;
model->positionType = attrib.datatype;
model->positionSize = attrib.sizePerElement;
model->numVertices = attrib.numElements;
model->positions = (GLubyte*) malloc(model->positionArraySize);
sizeRead = fread(model->positions, 1, model->positionArraySize, curFile);
if(sizeRead != model->positionArraySize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
error = fseek(curFile, toc.byteTexcoordOffset, SEEK_SET);
if(error < 0)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if(sizeRead != toc.attribHeaderSize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->texcoordArraySize = attrib.byteSize;
model->texcoordType = attrib.datatype;
model->texcoordSize = attrib.sizePerElement;
//Must have the same number of texcoords as positions
if(model->numVertices != attrib.numElements)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->texcoords = (GLubyte*) malloc(model->texcoordArraySize);
sizeRead = fread(model->texcoords, 1, model->texcoordArraySize, curFile);
if(sizeRead != model->texcoordArraySize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
error = fseek(curFile, toc.byteNormalOffset, SEEK_SET);
if(error < 0)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if(sizeRead != toc.attribHeaderSize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->normalArraySize = attrib.byteSize;
model->normalType = attrib.datatype;
model->normalSize = attrib.sizePerElement;
size_t sizeRead = 0;
int error = 0;
FILE *curFile = fopen(filepathname, "r");
//Must have the same number of normals as positions
if(model->numVertices != attrib.numElements)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->normals = (GLubyte*) malloc(model->normalArraySize );
sizeRead = fread(model->normals, 1, model->normalArraySize , curFile);
if(sizeRead != model->normalArraySize)
{
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
fclose(curFile);
return model;
if (!curFile) {
mdlDestroyModel(model);
return NULL;
}
modelHeader header = { 0 };
sizeRead = fread(&header, 1, sizeof(modelHeader), curFile);
if (sizeRead != sizeof(modelHeader)) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
if (strncmp(header.fileIdentifier, "AppleOpenGLDemoModelWWDC2010", sizeof(header.fileIdentifier))) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
if (header.majorVersion != 0 && header.minorVersion != 1) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
modelTOC toc = { 0 };
sizeRead = fread(&toc, 1, sizeof(modelTOC), curFile);
if (sizeRead != sizeof(modelTOC)) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
if (toc.attribHeaderSize > sizeof(modelAttrib)) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
modelAttrib attrib = { 0 };
error = fseek(curFile, toc.byteElementOffset, SEEK_SET);
if (error < 0) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if (sizeRead != toc.attribHeaderSize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->elementArraySize = attrib.byteSize;
model->elementType = attrib.datatype;
model->numElements = attrib.numElements;
// OpenGL ES cannot use UNSIGNED_INT elements
// so if the model has UI element...
if (GL_UNSIGNED_INT == model->elementType) {
// ...load the UI elements and convert to UNSIGNED_SHORT
GLubyte *uiElements = (GLubyte *)malloc(model->elementArraySize);
size_t ushortElementArraySize = model->numElements * sizeof(GLushort);
model->elements = (GLubyte *)malloc(ushortElementArraySize);
sizeRead = fread(uiElements, 1, model->elementArraySize, curFile);
if (sizeRead != model->elementArraySize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
GLuint elemNum = 0;
for (elemNum = 0; elemNum < model->numElements; elemNum++) {
// can't handle this model if an element is out of the UNSIGNED_INT range
if (((GLuint *)uiElements)[elemNum] >= 0xFFFF) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
((GLushort *)model->elements)[elemNum] = ((GLuint *)uiElements)[elemNum];
}
free(uiElements);
model->elementType = GL_UNSIGNED_SHORT;
model->elementArraySize = model->numElements * sizeof(GLushort);
} else {
model->elements = (GLubyte*)malloc(model->elementArraySize);
sizeRead = fread(model->elements, 1, model->elementArraySize, curFile);
if (sizeRead != model->elementArraySize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
}
fseek(curFile, toc.bytePositionOffset, SEEK_SET);
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if (sizeRead != toc.attribHeaderSize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->positionArraySize = attrib.byteSize;
model->positionType = attrib.datatype;
model->positionSize = attrib.sizePerElement;
model->numVertices = attrib.numElements;
model->positions = (GLubyte*) malloc(model->positionArraySize);
sizeRead = fread(model->positions, 1, model->positionArraySize, curFile);
if (sizeRead != model->positionArraySize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
error = fseek(curFile, toc.byteTexcoordOffset, SEEK_SET);
if (error < 0) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if (sizeRead != toc.attribHeaderSize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->texcoordArraySize = attrib.byteSize;
model->texcoordType = attrib.datatype;
model->texcoordSize = attrib.sizePerElement;
// must have the same number of texcoords as positions
if (model->numVertices != attrib.numElements) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->texcoords = (GLubyte*) malloc(model->texcoordArraySize);
sizeRead = fread(model->texcoords, 1, model->texcoordArraySize, curFile);
if (sizeRead != model->texcoordArraySize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
error = fseek(curFile, toc.byteNormalOffset, SEEK_SET);
if (error < 0) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile);
if (sizeRead != toc.attribHeaderSize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->normalArraySize = attrib.byteSize;
model->normalType = attrib.datatype;
model->normalSize = attrib.sizePerElement;
// must have the same number of normals as positions
if (model->numVertices != attrib.numElements) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
model->normals = (GLubyte*) malloc(model->normalArraySize );
sizeRead = fread(model->normals, 1, model->normalArraySize , curFile);
if (sizeRead != model->normalArraySize) {
fclose(curFile);
mdlDestroyModel(model);
return NULL;
}
fclose(curFile);
return model;
}
demoModel* mdlLoadQuadModel()
{
GLfloat posArray[] = {
-200.0f, 0.0f, -200.0f,
200.0f, 0.0f, -200.0f,
200.0f, 0.0f, 200.0f,
-200.0f, 0.0f, 200.0f
};
GLfloat texcoordArray[] = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f
};
GLfloat normalArray[] = {
0.0f, 0.0f, 1.0,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
};
GLushort elementArray[] =
{
0, 2, 1,
0, 3, 2
};
demoModel* model = (demoModel*) calloc(sizeof(demoModel), 1);
if(NULL == model)
{
return NULL;
}
model->positionType = GL_FLOAT;
model->positionSize = 3;
model->positionArraySize = sizeof(posArray);
model->positions = (GLubyte*)malloc(model->positionArraySize);
memcpy(model->positions, posArray, model->positionArraySize);
model->texcoordType = GL_FLOAT;
model->texcoordSize = 2;
model->texcoordArraySize = sizeof(texcoordArray);
model->texcoords = (GLubyte*)malloc(model->texcoordArraySize);
memcpy(model->texcoords, texcoordArray, model->texcoordArraySize );
demoModel *mdlLoadQuadModel() {
GLfloat posArray[] = {
-200.0f, 0.0f, -200.0f,
200.0f, 0.0f, -200.0f,
200.0f, 0.0f, 200.0f,
-200.0f, 0.0f, 200.0f
};
model->normalType = GL_FLOAT;
model->normalSize = 3;
model->normalArraySize = sizeof(normalArray);
model->normals = (GLubyte*)malloc(model->normalArraySize);
memcpy(model->normals, normalArray, model->normalArraySize);
model->elementArraySize = sizeof(elementArray);
model->elements = (GLubyte*)malloc(model->elementArraySize);
memcpy(model->elements, elementArray, model->elementArraySize);
model->primType = GL_TRIANGLES;
model->numElements = sizeof(elementArray) / sizeof(GLushort);
model->elementType = GL_UNSIGNED_SHORT;
model->numVertices = model->positionArraySize / (model->positionSize * sizeof(GLfloat));
return model;
GLfloat texcoordArray[] = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f
};
GLfloat normalArray[] = {
0.0f, 0.0f, 1.0,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
};
GLushort elementArray[] = {
0, 2, 1,
0, 3, 2
};
demoModel *model = (demoModel *)calloc(sizeof(demoModel), 1);
if (!model) {
return NULL;
}
model->positionType = GL_FLOAT;
model->positionSize = 3;
model->positionArraySize = sizeof(posArray);
model->positions = (GLubyte*)malloc(model->positionArraySize);
memcpy(model->positions, posArray, model->positionArraySize);
model->texcoordType = GL_FLOAT;
model->texcoordSize = 2;
model->texcoordArraySize = sizeof(texcoordArray);
model->texcoords = (GLubyte*)malloc(model->texcoordArraySize);
memcpy(model->texcoords, texcoordArray, model->texcoordArraySize );
model->normalType = GL_FLOAT;
model->normalSize = 3;
model->normalArraySize = sizeof(normalArray);
model->normals = (GLubyte*)malloc(model->normalArraySize);
memcpy(model->normals, normalArray, model->normalArraySize);
model->elementArraySize = sizeof(elementArray);
model->elements = (GLubyte*)malloc(model->elementArraySize);
memcpy(model->elements, elementArray, model->elementArraySize);
model->primType = GL_TRIANGLES;
model->numElements = sizeof(elementArray) / sizeof(GLushort);
model->elementType = GL_UNSIGNED_SHORT;
model->numVertices = model->positionArraySize / (model->positionSize * sizeof(GLfloat));
return model;
}
void mdlDestroyModel(demoModel* model)
{
if(NULL == model)
{
return;
}
free(model->elements);
free(model->positions);
free(model->normals);
free(model->texcoords);
free(model);
void mdlDestroyModel(demoModel *model) {
if (!model) {
return;
}
free(model->elements);
free(model->positions);
free(model->normals);
free(model->texcoords);
free(model);
}

View File

@ -1,91 +1,52 @@
/*
File: modelUtil.h
Abstract:
Functions for loading a model file for vertex arrays. The model file
format used is a simple "binary blob" invented for the purpose of
this sample code.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __MODEL_UTIL_H__
#define __MODEL_UTIL_H__
#include "glUtil.h"
typedef struct demoModelRec
{
GLuint numVertices;
GLubyte *positions;
GLenum positionType;
GLuint positionSize;
GLsizei positionArraySize;
GLubyte *texcoords;
GLenum texcoordType;
GLuint texcoordSize;
GLsizei texcoordArraySize;
GLubyte *normals;
GLenum normalType;
GLuint normalSize;
GLsizei normalArraySize;
GLubyte *elements;
GLenum elementType;
GLuint numElements;
GLsizei elementArraySize;
GLenum primType;
typedef struct demoModelRec {
GLuint numVertices;
GLubyte *positions;
GLenum positionType;
GLuint positionSize;
GLsizei positionArraySize;
GLubyte *texcoords;
GLenum texcoordType;
GLuint texcoordSize;
GLsizei texcoordArraySize;
GLubyte *normals;
GLenum normalType;
GLuint normalSize;
GLsizei normalArraySize;
GLubyte *elements;
GLenum elementType;
GLuint numElements;
GLsizei elementArraySize;
GLenum primType;
} demoModel;
demoModel* mdlLoadModel(const char* filepathname);
demoModel *mdlLoadModel(const char *filepathname);
demoModel* mdlLoadQuadModel();
demoModel *mdlLoadQuadModel();
void mdlDestroyModel(demoModel* model);
void mdlDestroyModel(demoModel *model);
#endif //__MODEL_UTIL_H__

View File

@ -1,105 +1,63 @@
/*
File: sourceUtil.c
Abstract:
Functions for loading source files for shaders.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Based on sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "sourceUtil.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
demoSource* srcLoadSource(const char* filepathname)
{
demoSource* source = (demoSource*) calloc(sizeof(demoSource), 1);
// Check the file name suffix to determine what type of shader this is
const char* suffixBegin = filepathname + strlen(filepathname) - 4;
if(0 == strncmp(suffixBegin, ".fsh", 4))
{
source->shaderType = GL_FRAGMENT_SHADER;
}
else if(0 == strncmp(suffixBegin, ".vsh", 4))
{
source->shaderType = GL_VERTEX_SHADER;
}
else
{
// Unknown suffix
source->shaderType = 0;
}
FILE* curFile = fopen(filepathname, "r");
// Get the size of the source
fseek(curFile, 0, SEEK_END);
GLsizei fileSize = ftell (curFile);
// Add 1 to the file size to include the null terminator for the string
source->byteSize = fileSize + 1;
// Alloc memory for the string
source->string = malloc(source->byteSize);
// Read entire file into the string from beginning of the file
fseek(curFile, 0, SEEK_SET);
fread(source->string, 1, fileSize, curFile);
fclose(curFile);
// Insert null terminator
source->string[fileSize] = 0;
return source;
demoSource *srcLoadSource(const char *filepathname) {
demoSource *source = (demoSource *)calloc(sizeof(demoSource), 1);
// Check the file name suffix to determine what type of shader this is
const char *suffixBegin = filepathname + strlen(filepathname) - 4;
if (!strncmp(suffixBegin, ".fsh", 4)) {
source->shaderType = GL_FRAGMENT_SHADER;
} else if (!strncmp(suffixBegin, ".vsh", 4)) {
source->shaderType = GL_VERTEX_SHADER;
} else {
// Unknown suffix
source->shaderType = 0;
}
FILE *curFile = fopen(filepathname, "r");
// Get the size of the source
fseek(curFile, 0, SEEK_END);
GLsizei fileSize = ftell (curFile);
// Add 1 to the file size to include the null terminator for the string
source->byteSize = fileSize + 1;
// Alloc memory for the string
source->string = malloc(source->byteSize);
// Read entire file into the string from beginning of the file
fseek(curFile, 0, SEEK_SET);
fread(source->string, 1, fileSize, curFile);
fclose(curFile);
// Insert null terminator
source->string[fileSize] = 0;
return source;
}
void srcDestroySource(demoSource *source) {
free(source->string);
free(source);
}
void srcDestroySource(demoSource* source)
{
free(source->string);
free(source);
}

View File

@ -1,69 +1,29 @@
/*
File: sourceUtil.h
Abstract:
Functions for loading source files for shaders.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __SOURCE_UTIL_H__
#define __SOURCE_UTIL_H__
#include "glUtil.h"
typedef struct demoSourceRec
{
GLchar* string;
GLsizei byteSize;
GLenum shaderType; // Vertex or Fragment
typedef struct demoSourceRec {
GLchar *string;
GLsizei byteSize;
GLenum shaderType; // Vertex or Fragment
} demoSource;
demoSource* srcLoadSource(const char* filepathname);
demoSource *srcLoadSource(const char *filepathname);
void srcDestroySource(demoSource* source);
void srcDestroySource(demoSource *source);
#endif // __SOURCE_UTIL_H__

258
src/video_util/vectorUtil.c Executable file → Normal file
View File

@ -1,157 +1,101 @@
/*
File: vectorUtil.c
Abstract:
Functions for performing vector math.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
*/
#include "vectorUtil.h"
#include <math.h>
#include <memory.h>
void vec4Add(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] + rhs[0];
vec[1] = lhs[1] + rhs[1];
vec[2] = lhs[2] + rhs[2];
vec[3] = lhs[3] + rhs[3];
}
void vec4Subtract(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] - rhs[0];
vec[1] = lhs[1] - rhs[1];
vec[2] = lhs[2] - rhs[2];
vec[3] = lhs[3] - rhs[3];
}
void vec4Multiply(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] * rhs[0];
vec[1] = lhs[1] * rhs[1];
vec[2] = lhs[2] * rhs[2];
vec[3] = lhs[3] * rhs[3];
}
void vec4Divide(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] / rhs[0];
vec[1] = lhs[1] / rhs[1];
vec[2] = lhs[2] / rhs[2];
vec[3] = lhs[3] / rhs[3];
}
void vec3Add(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] + rhs[0];
vec[1] = lhs[1] + rhs[1];
vec[2] = lhs[2] + rhs[2];
}
void vec3Subtract(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] - rhs[0];
vec[1] = lhs[1] - rhs[1];
vec[2] = lhs[2] - rhs[2];
}
void vec3Multiply(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] * rhs[0];
vec[1] = lhs[1] * rhs[1];
vec[2] = lhs[2] * rhs[2];
}
void vec3Divide(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[0] / rhs[0];
vec[1] = lhs[1] / rhs[1];
vec[2] = lhs[2] / rhs[2];
}
float vec3DotProduct(const float* lhs, const float* rhs)
{
return lhs[0]*rhs[0] + lhs[1]*rhs[1] + lhs[2]*rhs[2];
}
float vec4DotProduct(const float* lhs, const float* rhs)
{
return lhs[0]*rhs[0] + lhs[1]*rhs[1] + lhs[2]*rhs[2] + lhs[3]*rhs[3];
}
void vec3CrossProduct(float* vec, const float* lhs, const float* rhs)
{
vec[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
vec[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
vec[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
}
float vec3Length(const float* vec)
{
return sqrtf(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
}
float vec3Distance(const float* pointA, const float* pointB)
{
float diffx = pointA[0]-pointB[0];
float diffy = pointA[1]-pointB[1];
float diffz = pointA[2]-pointB[2];
return sqrtf(diffx*diffx + diffy*diffy + diffz*diffz);
}
void vec3Normalize(float* vec, const float* src)
{
float length = vec3Length(src);
vec[0] = src[0]/length;
vec[1] = src[1]/length;
vec[2] = src[2]/length;
}
/*
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "vectorUtil.h"
#include <math.h>
#include <memory.h>
void vec4Add(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] + rhs[0];
vec[1] = lhs[1] + rhs[1];
vec[2] = lhs[2] + rhs[2];
vec[3] = lhs[3] + rhs[3];
}
void vec4Subtract(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] - rhs[0];
vec[1] = lhs[1] - rhs[1];
vec[2] = lhs[2] - rhs[2];
vec[3] = lhs[3] - rhs[3];
}
void vec4Multiply(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] * rhs[0];
vec[1] = lhs[1] * rhs[1];
vec[2] = lhs[2] * rhs[2];
vec[3] = lhs[3] * rhs[3];
}
void vec4Divide(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] / rhs[0];
vec[1] = lhs[1] / rhs[1];
vec[2] = lhs[2] / rhs[2];
vec[3] = lhs[3] / rhs[3];
}
void vec3Add(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] + rhs[0];
vec[1] = lhs[1] + rhs[1];
vec[2] = lhs[2] + rhs[2];
}
void vec3Subtract(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] - rhs[0];
vec[1] = lhs[1] - rhs[1];
vec[2] = lhs[2] - rhs[2];
}
void vec3Multiply(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] * rhs[0];
vec[1] = lhs[1] * rhs[1];
vec[2] = lhs[2] * rhs[2];
}
void vec3Divide(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[0] / rhs[0];
vec[1] = lhs[1] / rhs[1];
vec[2] = lhs[2] / rhs[2];
}
float vec3DotProduct(const float *lhs, const float *rhs) {
return lhs[0]*rhs[0] + lhs[1]*rhs[1] + lhs[2]*rhs[2];
}
float vec4DotProduct(const float *lhs, const float *rhs) {
return lhs[0]*rhs[0] + lhs[1]*rhs[1] + lhs[2]*rhs[2] + lhs[3]*rhs[3];
}
void vec3CrossProduct(float *vec, const float *lhs, const float *rhs) {
vec[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
vec[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
vec[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
}
float vec3Length(const float *vec) {
return sqrtf(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
}
float vec3Distance(const float *pointA, const float *pointB) {
float diffx = pointA[0]-pointB[0];
float diffy = pointA[1]-pointB[1];
float diffz = pointA[2]-pointB[2];
return sqrtf(diffx*diffx + diffy*diffy + diffz*diffz);
}
void vec3Normalize(float *vec, const float *src) {
float length = vec3Length(src);
vec[0] = src[0]/length;
vec[1] = src[1]/length;
vec[2] = src[2]/length;
}

164
src/video_util/vectorUtil.h Executable file → Normal file
View File

@ -1,100 +1,64 @@
/*
File: vectorUtil.h
Abstract:
Functions for performing vector math.
Version: 1.7
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
*/
#ifndef __VECTOR_UTIL_H__
#define __VECTOR_UTIL_H__
// A Vector is floating point array with either 3 or 4 components
// functions with the vec4 prefix require 4 elements in the array
// functions with vec3 prefix require only 3 elements in the array
// Subtracts one 4D vector to another
void vec4Add(float* vec, const float* lhs, const float* rhs);
// Subtracts one 4D vector from another
void vec4Subtract(float* vec, const float* lhs, const float* rhs);
// Multiplys one 4D vector by another
void vec4Multiply(float* vec, const float* lhs, const float* rhs);
// Divides one 4D vector by another
void vec4Divide(float* vec, const float* lhs, const float* rhs);
// Subtracts one 4D vector to another
void vec3Add(float* vec, const float* lhs, const float* rhs);
// Subtracts one 4D vector from another
void vec3Subtract(float* vec, const float* lhs, const float* rhs);
// Multiplys one 4D vector by another
void vec3Multiply(float* vec, const float* lhs, const float* rhs);
// Divides one 4D vector by another
void vec3Divide(float* vec, const float* lhs, const float* rhs);
// Calculates the Cross Product of a 3D vector
void vec3CrossProduct(float* vec, const float* lhs, const float* rhs);
// Normalizes a 3D vector
void vec3Normalize(float* vec, const float* src);
// Returns the Dot Product of 2 3D vectors
float vec3DotProduct(const float* lhs, const float* rhs);
// Returns the Dot Product of 2 4D vectors
float vec4DotProduct(const float* lhs, const float* rhs);
// Returns the length of a 3D vector
// (i.e the distance of a point from the origin)
float vec3Length(const float* vec);
// Returns the distance between two 3D points
float vec3Distance(const float* pointA, const float* pointB);
#endif //__VECTOR_UTIL_H__
/*
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
*/
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __VECTOR_UTIL_H__
#define __VECTOR_UTIL_H__
// A Vector is floating point array with either 3 or 4 components
// functions with the vec4 prefix require 4 elements in the array
// functions with vec3 prefix require only 3 elements in the array
// Subtracts one 4D vector to another
void vec4Add(float *vec, const float *lhs, const float *rhs);
// Subtracts one 4D vector from another
void vec4Subtract(float *vec, const float *lhs, const float *rhs);
// Multiplys one 4D vector by another
void vec4Multiply(float *vec, const float *lhs, const float *rhs);
// Divides one 4D vector by another
void vec4Divide(float *vec, const float *lhs, const float *rhs);
// Subtracts one 4D vector to another
void vec3Add(float *vec, const float *lhs, const float *rhs);
// Subtracts one 4D vector from another
void vec3Subtract(float *vec, const float *lhs, const float *rhs);
// Multiplys one 4D vector by another
void vec3Multiply(float *vec, const float *lhs, const float *rhs);
// Divides one 4D vector by another
void vec3Divide(float *vec, const float *lhs, const float *rhs);
// Calculates the Cross Product of a 3D vector
void vec3CrossProduct(float *vec, const float *lhs, const float *rhs);
// Normalizes a 3D vector
void vec3Normalize(float *vec, const float *src);
// Returns the Dot Product of 2 3D vectors
float vec3DotProduct(const float *lhs, const float *rhs);
// Returns the Dot Product of 2 4D vectors
float vec4DotProduct(const float *lhs, const float *rhs);
// Returns the length of a 3D vector
// (i.e the distance of a point from the origin)
float vec3Length(const float *vec);
// Returns the distance between two 3D points
float vec3Distance(const float *pointA, const float *pointB);
#endif //__VECTOR_UTIL_H__