gluLookAt()
iPhoneDev/OpenGL ES 2009/07/03 14:51 |OpenGL ES는 모바일 환경을 고려해서 만들어졌기 때문에 많은 함수들이 빠져 있습니다.
그중 카메라(시점)와 관계가 있는 gluLookAt()함수가 빠져 있기 때문에 시점 변환 등에 에로사항이 꽃피게 됩니다.
이런 초대박 불편함을 해결 하기 위해서 아래 처럼 스스로 gluLookAt()함수를 만들어서 사용하는 방법이 있습니다.
라이선스는 후리(free)이지만, 코드를 작성해주신 MIT 능자님들에 대한 감탄, 존경, 경외, 배려, 존중의 의미로 주석을 넣어줍시다!
gluLookAt.h
/*
* gluLookAt.h
*
* This is a modified version of the function of the same name from
* the Mesa3D project ( http://mesa3d.org/ ), which is licensed
* under the MIT license, which allows use, modification, and
* redistribution
*
* In order to work under OpenGL ES, all instances of GLdouble
* had to be changed to GLfloat, and all "d" function calls had
* to be changed to the "f" versions.
*
* Original developer's comments have been left in place.
*
* Out of respect for the original authors, this is licensed under
* the Mesa (MIT) license. Original license follows:
*
* -----------------------------------------------------------------------
*
* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
GLfloat centerx, GLfloat centery, GLfloat centerz,
GLfloat upx, GLfloat upy, GLfloat upz);
gluLookAt.m/*
* gluLookAt.c
*/
void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
GLfloat centerx, GLfloat centery, GLfloat centerz,
GLfloat upx, GLfloat upy, GLfloat upz)
{
GLfloat m;
GLfloat x, y, z;
GLfloat mag;
/* Make rotation matrix */
/* Z vector */
z = eyex - centerx;
z = eyey - centery;
z = eyez - centerz;
mag = sqrt(z * z + z * z + z * z);
if { /* mpichler, 19950515 */
z /= mag;
z /= mag;
z /= mag;
}
/* Y vector */
y = upx;
y = upy;
y = upz;
/* X vector = Y cross Z */
x = y * z - y * z;
x = -y * z + y * z;
x = y * z - y * z;
/* Recompute Y = Z cross X */
y = z * x - z * x;
y = -z * x + z * x;
y = z * x - z * x;
/* mpichler, 19950515 */
/* cross product gives area of parallelogram, which is < 1.0 for
* non-perpendicular unit-length vectors; so normalize x, y here
*/
mag = sqrt(x * x + x * x + x * x);
if {
x /= mag;
x /= mag;
x /= mag;
}
mag = sqrt(y * y + y * y + y * y);
if {
y /= mag;
y /= mag;
y /= mag;
}
M(0, 0) = x;
M(0, 1) = x;
M(0, 2) = x;
M(0, 3) = 0.0;
M(1, 0) = y;
M(1, 1) = y;
M(1, 2) = y;
M(1, 3) = 0.0;
M(2, 0) = z;
M(2, 1) = z;
M(2, 2) = z;
M(2, 3) = 0.0;
M(3, 0) = 0.0;
M(3, 1) = 0.0;
M(3, 2) = 0.0;
M(3, 3) = 1.0;
glMultMatrixf(m);
/* Translate Eye to Origin */
glTranslatef(-eyex, -eyey, -eyez);
}원본 출처는 아래와 같습니다:
http://iphonedevelopment.blogspot.com/2008/12/glulookat.html



댓글을 달아 주세요