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, NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24, NSOpenGLPFADepthSize, 24,
// Must specify the 3.2 Core Profile to use OpenGL 3.2 // Must specify the 3.2 Core Profile to use OpenGL 3.2
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
NSOpenGLPFAOpenGLProfile, NSOpenGLPFAOpenGLProfile,
NSOpenGLProfileVersion3_2Core, NSOpenGLProfileVersion3_2Core,
#endif
0 0
}; };
@@ -116,7 +114,7 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt
NSOpenGLContext* context = [[[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] autorelease]; 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 // 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 // This will make it much more obvious where and when such a function call is made so
// that we can remove such calls. // that we can remove such calls.

View File

@@ -6,7 +6,7 @@
// Copyright (c) 2014 deadc0de.org. All rights reserved. // Copyright (c) 2014 deadc0de.org. All rights reserved.
// //
#if ESSENTIAL_GL_PRACTICES_IOS #if TARGET_OS_IPHONE
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#else #else
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
@@ -21,7 +21,7 @@ int main(int argc_, const char *argv_[])
argc = argc_; argc = argc_;
argv = argv_; argv = argv_;
#if ESSENTIAL_GL_PRACTICES_IOS #if TARGET_OS_IPHONE
@autoreleasepool { @autoreleasepool {
retVal = UIApplicationMain(argc, argv, nil, nil); 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 // with the proper version preprocessor string prepended
float glLanguageVersion; float glLanguageVersion;
#if ESSENTIAL_GL_PRACTICES_IOS #if TARGET_OS_IPHONE
sscanf((char *)glGetString(GL_SHADING_LANGUAGE_VERSION), "OpenGL ES GLSL ES %f", &glLanguageVersion); sscanf((char *)glGetString(GL_SHADING_LANGUAGE_VERSION), "OpenGL ES GLSL ES %f", &glLanguageVersion);
#else #else
sscanf((char *)glGetString(GL_SHADING_LANGUAGE_VERSION), "%f", &glLanguageVersion); sscanf((char *)glGetString(GL_SHADING_LANGUAGE_VERSION), "%f", &glLanguageVersion);

View File

@@ -1,145 +1,77 @@
/* /*
File: glUtil.h * Apple // emulator for *nix
Abstract: *
Includes the appropriate OpenGL headers (depending on whether this * This software package is subject to the GNU General Public License
is built for iOS or OSX) and provides some API utility functions * version 2 or later (your choice) as published by the Free Software
* Foundation.
Version: 1.7 *
* THERE ARE NO WARRANTIES WHATSOEVER.
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __GL_UTIL_H__ #ifndef __GL_UTIL_H__
#define __GL_UTIL_H__ #define __GL_UTIL_H__
#if ESSENTIAL_GL_PRACTICES_IOS #if defined(__APPLE__)
# include <TargetConditionals.h>
#import <OpenGLES/ES2/gl.h> # if TARGET_OS_IPHONE
#import <OpenGLES/ES2/glext.h> # import <OpenGLES/ES2/gl.h>
# import <OpenGLES/ES2/glext.h>
#else # else
# import <OpenGL/OpenGL.h>
#import <OpenGL/OpenGL.h> # import <OpenGL/gl3.h>
# endif
// OpenGL 3.2 is only supported on MacOS X Lion and later #elif defined(USE_GL3W)
// CGL_VERSION_1_3 is defined as 1 on MacOS X Lion and later # include <GL3/gl3.h>
#if CGL_VERSION_1_3 # include <GL3/gl3w.h>
// Set to 0 to run on the Legacy OpenGL Profile
#define ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 1
#else #else
#define ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 0 # define GLEW_STATIC
#endif //!CGL_VERSION_1_3 # include <GL/glew.h>
# define FREEGLUT_STATIC
# include <GL/freeglut.h>
#endif
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 static inline const char * GetGLErrorString(GLenum error) {
#import <OpenGL/gl3.h> const char *str;
#else switch (error) {
#import <OpenGL/gl.h> case GL_NO_ERROR:
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 str = "GL_NO_ERROR";
break;
#endif // !ESSENTIAL_GL_PRACTICES_IOS case GL_INVALID_ENUM:
str = "GL_INVALID_ENUM";
break;
//The name of the VertexArrayObject are slightly different in case GL_INVALID_VALUE:
// OpenGLES, OpenGL Core Profile, and OpenGL Legacy str = "GL_INVALID_VALUE";
// The arguments are exactly the same across these APIs however break;
#if ESSENTIAL_GL_PRACTICES_IOS case GL_INVALID_OPERATION:
#define glBindVertexArray glBindVertexArrayOES str = "GL_INVALID_OPERATION";
#define glGenVertexArrays glGenVertexArraysOES break;
#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;
#if defined __gl_h_ || defined __gl3_h_ #if defined __gl_h_ || defined __gl3_h_
case GL_OUT_OF_MEMORY: case GL_OUT_OF_MEMORY:
str = "GL_OUT_OF_MEMORY"; str = "GL_OUT_OF_MEMORY";
break; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: case GL_INVALID_FRAMEBUFFER_OPERATION:
str = "GL_INVALID_FRAMEBUFFER_OPERATION"; str = "GL_INVALID_FRAMEBUFFER_OPERATION";
break; break;
#endif #endif
#if defined __gl_h_ #if defined __gl_h_
case GL_STACK_OVERFLOW: case GL_STACK_OVERFLOW:
str = "GL_STACK_OVERFLOW"; str = "GL_STACK_OVERFLOW";
break; break;
case GL_STACK_UNDERFLOW: case GL_STACK_UNDERFLOW:
str = "GL_STACK_UNDERFLOW"; str = "GL_STACK_UNDERFLOW";
break; break;
case GL_TABLE_TOO_LARGE: case GL_TABLE_TOO_LARGE:
str = "GL_TABLE_TOO_LARGE"; str = "GL_TABLE_TOO_LARGE";
break; break;
#endif #endif
default: default:
str = "(ERROR: Unknown Error Enum)"; str = "(ERROR: Unknown Error Enum)";
break; break;
} }
return str; return str;
} }
#endif // __GL_UTIL_H__ #endif // __GL_UTIL_H__

View File

@@ -1,75 +1,34 @@
/* /*
File: imageUtil.h * Apple // emulator for *nix
Abstract: *
Functions for loading an image files for textures. * This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
Version: 1.7 * Foundation.
*
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple * THERE ARE NO WARRANTIES WHATSOEVER.
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __IMAGE_UTIL_H__ #ifndef __IMAGE_UTIL_H__
#define __IMAGE_UTIL_H__ #define __IMAGE_UTIL_H__
#include "glUtil.h" #include "glUtil.h"
typedef struct demoImageRec typedef struct demoImageRec {
{ GLubyte *data;
GLubyte* data; GLsizei size;
GLuint width;
GLsizei size; GLuint height;
GLenum format;
GLuint width; GLenum type;
GLuint height; GLuint rowByteSize;
GLenum format;
GLenum type;
GLuint rowByteSize;
} demoImage; } 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__ #endif //__IMAGE_UTIL_H__

View File

@@ -1,116 +1,76 @@
/* /*
File: imageUtil.m * Apple // emulator for *nix
Abstract: *
Functions for loading an image files for textures. * This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
Version: 1.7 * Foundation.
*
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple * THERE ARE NO WARRANTIES WHATSOEVER.
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "imageUtil.h" #include "imageUtil.h"
#if ESSENTIAL_GL_PRACTICES_IOS #if TARGET_OS_IPHONE
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#else #else
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#endif #endif
demoImage* imgLoadImage(const char* filepathname, int flipVertical) demoImage *imgLoadImage(const char *filepathname, int flipVertical) {
{ NSString *filepathString = [[NSString alloc] initWithUTF8String:filepathname];
NSString *filepathString = [[NSString alloc] initWithUTF8String:filepathname];
#if TARGET_OS_IPHONE
#if ESSENTIAL_GL_PRACTICES_IOS UIImage* imageClass = [[UIImage alloc] initWithContentsOfFile:filepathString];
UIImage* imageClass = [[UIImage alloc] initWithContentsOfFile:filepathString]; #else
#else
NSImage *nsimage = [[NSImage alloc] initWithContentsOfFile: filepathString]; NSImage *nsimage = [[NSImage alloc] initWithContentsOfFile: filepathString];
NSBitmapImageRep *imageClass = [[NSBitmapImageRep alloc] initWithData:[nsimage TIFFRepresentation]];
NSBitmapImageRep *imageClass = [[NSBitmapImageRep alloc] initWithData:[nsimage TIFFRepresentation]]; [nsimage release];
[nsimage release];
#endif #endif
CGImageRef cgImage = imageClass.CGImage; CGImageRef cgImage = imageClass.CGImage;
if (!cgImage) if (!cgImage)
{ {
[filepathString release]; [filepathString release];
[imageClass release]; [imageClass release];
return NULL; return NULL;
} }
demoImage* image = malloc(sizeof(demoImage)); demoImage *image = malloc(sizeof(demoImage));
image->width = CGImageGetWidth(cgImage); image->width = CGImageGetWidth(cgImage);
image->height = CGImageGetHeight(cgImage); image->height = CGImageGetHeight(cgImage);
image->rowByteSize = image->width * 4; image->rowByteSize = image->width * 4;
image->data = malloc(image->height * image->rowByteSize); image->data = malloc(image->height * image->rowByteSize);
image->format = GL_RGBA; image->format = GL_RGBA;
image->type = GL_UNSIGNED_BYTE; image->type = GL_UNSIGNED_BYTE;
CGContextRef context = CGBitmapContextCreate(image->data, image->width, image->height, 8, image->rowByteSize, CGImageGetColorSpace(cgImage), kCGImageAlphaNoneSkipLast); CGContextRef context = CGBitmapContextCreate(image->data, image->width, image->height, 8, image->rowByteSize, CGImageGetColorSpace(cgImage), kCGImageAlphaNoneSkipLast);
CGContextSetBlendMode(context, kCGBlendModeCopy); CGContextSetBlendMode(context, kCGBlendModeCopy);
if(flipVertical) if (flipVertical) {
{ CGContextTranslateCTM(context, 0.0, image->height);
CGContextTranslateCTM(context, 0.0, image->height); CGContextScaleCTM(context, 1.0, -1.0);
CGContextScaleCTM(context, 1.0, -1.0); }
} CGContextDrawImage(context, CGRectMake(0.0, 0.0, image->width, image->height), cgImage);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, image->width, image->height), cgImage); CGContextRelease(context);
CGContextRelease(context);
if (!image->data) {
if(NULL == image->data) [filepathString release];
{ [imageClass release];
[filepathString release];
[imageClass release]; imgDestroyImage(image);
return NULL;
imgDestroyImage(image); }
return NULL;
} [filepathString release];
[imageClass release];
[filepathString release];
[imageClass release]; return image;
}
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 * Apple // emulator for *nix
Abstract: *
Functions for performing matrix math. * This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
Version: 1.7 * Foundation.
*
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple * THERE ARE NO WARRANTIES WHATSOEVER.
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __MATRIX_UTIL_H__ #ifndef __MATRIX_UTIL_H__
#define __MATRIX_UTIL_H__ #define __MATRIX_UTIL_H__
@@ -60,99 +24,94 @@
// [ 3 7 11 15 ] // [ 3 7 11 15 ]
// MTX = LeftHandSideMatrix * RightHandSideMatrix // 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 // MTX = IdentityMatrix
void mtxLoadIdentity(float* mtx); void mtxLoadIdentity(float *mtx);
// MTX = Transpos(SRC) // MTX = Transpos(SRC)
void mtxTranspose(float* mtx, const float* src); void mtxTranspose(float *mtx, const float *src);
// MTX = src^-1 // MTX = src^-1
void mtxInvert(float* mtx, const float* src); void mtxInvert(float *mtx, const float *src);
// MTX = PerspectiveProjectionMatrix // 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 // MTX = OrthographicProjectionMatrix
void mtxLoadOrthographic(float* mtx, void mtxLoadOrthographic(float *mtx, float left, float right, float bottom, float top, float nearZ, float farZ);
float left, float right,
float bottom, float top,
float nearZ, float farZ);
// MTX = ObliqueProjectionMatrix(src, clipPlane) // 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 // MTX = TranlationMatrix
void mtxLoadTranslate(float* mtx, float xTrans, float yTrans, float zTrans); void mtxLoadTranslate(float *mtx, float xTrans, float yTrans, float zTrans);
// MTX = ScaleMatrix // MTX = ScaleMatrix
void mtxLoadScale(float* mtx, float xScale, float yScale, float zScale); void mtxLoadScale(float *mtx, float xScale, float yScale, float zScale);
// MTX = RotateXYZMatrix // 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 // MTX = RotateXMatrix
void mtxLoadRotateX(float* mtx, float deg); void mtxLoadRotateX(float *mtx, float deg);
// MTX = RotateYMatrix // MTX = RotateYMatrix
void mtxLoadRotateY(float* mtx, float deg); void mtxLoadRotateY(float *mtx, float deg);
// MTX = RotateZMatrix // MTX = RotateZMatrix
void mtxLoadRotateZ(float* mtx, float deg); void mtxLoadRotateZ(float *mtx, float deg);
// MTX = MTX * TranslationMatrix - Similar to glTranslate // 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 // 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 // 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 // MTX = MTX * RotateXMatrix
void mtxRotateXApply(float* mtx, float rad); void mtxRotateXApply(float *mtx, float rad);
// MTX = MTX * RotateYMatrix // MTX = MTX * RotateYMatrix
void mtxRotateYApply(float* mtx, float rad); void mtxRotateYApply(float *mtx, float rad);
// MTX = MTX * RotateZMatrix // MTX = MTX * RotateZMatrix
void mtxRotateZApply(float* mtx, float rad); void mtxRotateZApply(float *mtx, float rad);
// MTX = TranslationMatrix * MTX // 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 // 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 // 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 // MTX = RotateXMatrix * MTX
void mtxRotateXMatrix(float* mtx, float rad); void mtxRotateXMatrix(float *mtx, float rad);
// MTX = RotateYMatrix * MTX // MTX = RotateYMatrix * MTX
void mtxRotateYMatrix(float* mtx, float rad); void mtxRotateYMatrix(float *mtx, float rad);
// MTX = RotateZMatrix * MTX // MTX = RotateZMatrix * MTX
void mtxRotateZMatrix(float* mtx, float rad); void mtxRotateZMatrix(float *mtx, float rad);
// 3x3 MTX = 3x3 IdendityMatrix
void mtx3x3LoadIdentity(float* mtx);
// 3x3 MTX = 3x3 IdentityMatrix
void mtx3x3LoadIdentity(float *mtx);
// 3x3 MTX = 3x3 LHS x 3x3 RHS // 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
// 3x3 MTX = TopLeft of MTX void mtx3x3FromTopLeftOf4x4(float *mtx, const float *src);
void mtx3x3FromTopLeftOf4x4(float* mtx, const float* src);
// 3x3 MTX = Transpose(3x3 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 // 3x3 MTX = 3x3 SRC^-1
void mtx3x3Invert(float* mtx, const float* src); void mtx3x3Invert(float *mtx, const float *src);
#endif //__MATRIX_UTIL_H__ #endif //__MATRIX_UTIL_H__

View File

@@ -1,422 +1,326 @@
/* /*
File: modelUtil.c * Apple // emulator for *nix
Abstract: *
Functions for loading a model file for vertex arrays. The model file * This software package is subject to the GNU General Public License
format used is a simple "binary blob" invented for the purpose of * version 2 or later (your choice) as published by the Free Software
this sample code. * Foundation.
*
Version: 1.7 * THERE ARE NO WARRANTIES WHATSOEVER.
*
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "modelUtil.h" #include "modelUtil.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
typedef struct modelHeaderRec typedef struct modelHeaderRec {
{ char fileIdentifier[30];
char fileIdentifier[30]; unsigned int majorVersion;
unsigned int majorVersion; unsigned int minorVersion;
unsigned int minorVersion;
} modelHeader; } modelHeader;
typedef struct modelTOCRec typedef struct modelTOCRec {
{ unsigned int attribHeaderSize;
unsigned int attribHeaderSize; unsigned int byteElementOffset;
unsigned int byteElementOffset; unsigned int bytePositionOffset;
unsigned int bytePositionOffset; unsigned int byteTexcoordOffset;
unsigned int byteTexcoordOffset; unsigned int byteNormalOffset;
unsigned int byteNormalOffset;
} modelTOC; } modelTOC;
typedef struct modelAttribRec typedef struct modelAttribRec {
{ unsigned int byteSize;
unsigned int byteSize; GLenum datatype;
GLenum datatype; GLenum primType; //If index data
GLenum primType; //If index data unsigned int sizePerElement;
unsigned int sizePerElement; unsigned int numElements;
unsigned int numElements;
} modelAttrib; } modelAttrib;
demoModel* mdlLoadModel(const char* filepathname) demoModel *mdlLoadModel(const char *filepathname) {
{ if (!filepathname) {
if(NULL == filepathname) return NULL;
{ }
return NULL; demoModel *model = (demoModel *)calloc(sizeof(demoModel), 1);
} if (!model) {
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;
}
}
fseek(curFile, toc.bytePositionOffset, SEEK_SET); size_t sizeRead = 0;
int error = 0;
sizeRead = fread(&attrib, 1, toc.attribHeaderSize, curFile); FILE *curFile = fopen(filepathname, "r");
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 (!curFile) {
if(model->numVertices != attrib.numElements) mdlDestroyModel(model);
{ return NULL;
fclose(curFile); }
mdlDestroyModel(model);
return NULL; modelHeader header = { 0 };
} sizeRead = fread(&header, 1, sizeof(modelHeader), curFile);
if (sizeRead != sizeof(modelHeader)) {
model->normals = (GLubyte*) malloc(model->normalArraySize ); fclose(curFile);
mdlDestroyModel(model);
sizeRead = fread(model->normals, 1, model->normalArraySize , curFile); return NULL;
}
if(sizeRead != model->normalArraySize)
{ if (strncmp(header.fileIdentifier, "AppleOpenGLDemoModelWWDC2010", sizeof(header.fileIdentifier))) {
fclose(curFile); fclose(curFile);
mdlDestroyModel(model); mdlDestroyModel(model);
return NULL; return NULL;
} }
fclose(curFile); if (header.majorVersion != 0 && header.minorVersion != 1) {
fclose(curFile);
return model; 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() demoModel *mdlLoadQuadModel() {
{ GLfloat posArray[] = {
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, 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 );
model->normalType = GL_FLOAT; GLfloat texcoordArray[] = {
model->normalSize = 3; 0.0f, 1.0f,
model->normalArraySize = sizeof(normalArray); 1.0f, 1.0f,
model->normals = (GLubyte*)malloc(model->normalArraySize); 1.0f, 0.0f,
memcpy(model->normals, normalArray, model->normalArraySize); 0.0f, 0.0f
};
model->elementArraySize = sizeof(elementArray);
model->elements = (GLubyte*)malloc(model->elementArraySize); GLfloat normalArray[] = {
memcpy(model->elements, elementArray, model->elementArraySize); 0.0f, 0.0f, 1.0,
0.0f, 0.0f, 1.0f,
model->primType = GL_TRIANGLES; 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
};
model->numElements = sizeof(elementArray) / sizeof(GLushort);
model->elementType = GL_UNSIGNED_SHORT; GLushort elementArray[] = {
model->numVertices = model->positionArraySize / (model->positionSize * sizeof(GLfloat)); 0, 2, 1,
0, 3, 2
return model; };
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) void mdlDestroyModel(demoModel *model) {
{ if (!model) {
if(NULL == model) return;
{ }
return;
} free(model->elements);
free(model->positions);
free(model->elements); free(model->normals);
free(model->positions); free(model->texcoords);
free(model->normals);
free(model->texcoords); free(model);
free(model);
} }

View File

@@ -1,91 +1,52 @@
/* /*
File: modelUtil.h * Apple // emulator for *nix
Abstract: *
Functions for loading a model file for vertex arrays. The model file * This software package is subject to the GNU General Public License
format used is a simple "binary blob" invented for the purpose of * version 2 or later (your choice) as published by the Free Software
this sample code. * Foundation.
*
Version: 1.7 * THERE ARE NO WARRANTIES WHATSOEVER.
*
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __MODEL_UTIL_H__ #ifndef __MODEL_UTIL_H__
#define __MODEL_UTIL_H__ #define __MODEL_UTIL_H__
#include "glUtil.h" #include "glUtil.h"
typedef struct demoModelRec typedef struct demoModelRec {
{ GLuint numVertices;
GLuint numVertices;
GLubyte *positions;
GLubyte *positions; GLenum positionType;
GLenum positionType; GLuint positionSize;
GLuint positionSize; GLsizei positionArraySize;
GLsizei positionArraySize;
GLubyte *texcoords;
GLubyte *texcoords; GLenum texcoordType;
GLenum texcoordType; GLuint texcoordSize;
GLuint texcoordSize; GLsizei texcoordArraySize;
GLsizei texcoordArraySize;
GLubyte *normals;
GLubyte *normals; GLenum normalType;
GLenum normalType; GLuint normalSize;
GLuint normalSize; GLsizei normalArraySize;
GLsizei normalArraySize;
GLubyte *elements;
GLubyte *elements; GLenum elementType;
GLenum elementType; GLuint numElements;
GLuint numElements; GLsizei elementArraySize;
GLsizei elementArraySize;
GLenum primType;
GLenum primType;
} demoModel; } 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__ #endif //__MODEL_UTIL_H__

View File

@@ -1,105 +1,63 @@
/* /*
File: sourceUtil.c * Apple // emulator for *nix
Abstract: *
Functions for loading source files for shaders. * This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
Version: 1.7 * Foundation.
*
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple * THERE ARE NO WARRANTIES WHATSOEVER.
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.
*/ */
// Based on sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "sourceUtil.h" #include "sourceUtil.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
demoSource* srcLoadSource(const char* filepathname) demoSource *srcLoadSource(const char *filepathname) {
{ demoSource *source = (demoSource *)calloc(sizeof(demoSource), 1);
demoSource* source = (demoSource*) calloc(sizeof(demoSource), 1);
// Check the file name suffix to determine what type of shader this is
// Check the file name suffix to determine what type of shader this is const char *suffixBegin = filepathname + strlen(filepathname) - 4;
const char* suffixBegin = filepathname + strlen(filepathname) - 4;
if (!strncmp(suffixBegin, ".fsh", 4)) {
if(0 == strncmp(suffixBegin, ".fsh", 4)) source->shaderType = GL_FRAGMENT_SHADER;
{ } else if (!strncmp(suffixBegin, ".vsh", 4)) {
source->shaderType = GL_FRAGMENT_SHADER; source->shaderType = GL_VERTEX_SHADER;
} } else {
else if(0 == strncmp(suffixBegin, ".vsh", 4)) // Unknown suffix
{ source->shaderType = 0;
source->shaderType = GL_VERTEX_SHADER; }
}
else FILE *curFile = fopen(filepathname, "r");
{
// Unknown suffix // Get the size of the source
source->shaderType = 0; fseek(curFile, 0, SEEK_END);
} GLsizei fileSize = ftell (curFile);
FILE* curFile = fopen(filepathname, "r"); // Add 1 to the file size to include the null terminator for the string
source->byteSize = fileSize + 1;
// Get the size of the source
fseek(curFile, 0, SEEK_END); // Alloc memory for the string
GLsizei fileSize = ftell (curFile); source->string = malloc(source->byteSize);
// Add 1 to the file size to include the null terminator for the string // Read entire file into the string from beginning of the file
source->byteSize = fileSize + 1; fseek(curFile, 0, SEEK_SET);
fread(source->string, 1, fileSize, curFile);
// Alloc memory for the string
source->string = malloc(source->byteSize); fclose(curFile);
// Read entire file into the string from beginning of the file // Insert null terminator
fseek(curFile, 0, SEEK_SET); source->string[fileSize] = 0;
fread(source->string, 1, fileSize, curFile);
return source;
fclose(curFile); }
// Insert null terminator void srcDestroySource(demoSource *source) {
source->string[fileSize] = 0; free(source->string);
free(source);
return source;
} }
void srcDestroySource(demoSource* source)
{
free(source->string);
free(source);
}

View File

@@ -1,69 +1,29 @@
/* /*
File: sourceUtil.h * Apple // emulator for *nix
Abstract: *
Functions for loading source files for shaders. * This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
Version: 1.7 * Foundation.
*
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple * THERE ARE NO WARRANTIES WHATSOEVER.
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.
*/ */
// Modified sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#ifndef __SOURCE_UTIL_H__ #ifndef __SOURCE_UTIL_H__
#define __SOURCE_UTIL_H__ #define __SOURCE_UTIL_H__
#include "glUtil.h" #include "glUtil.h"
typedef struct demoSourceRec typedef struct demoSourceRec {
{ GLchar *string;
GLchar* string; GLsizei byteSize;
GLenum shaderType; // Vertex or Fragment
GLsizei byteSize;
GLenum shaderType; // Vertex or Fragment
} demoSource; } demoSource;
demoSource* srcLoadSource(const char* filepathname); demoSource *srcLoadSource(const char *filepathname);
void srcDestroySource(demoSource* source); void srcDestroySource(demoSource *source);
#endif // __SOURCE_UTIL_H__ #endif // __SOURCE_UTIL_H__

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

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