#include "matrix.h"
#include <stdio.h>
#include "esUtil.h"
#ifdef __cplusplus
extern "C" {
#endif
float *getRotateM(float *rm, int rmOffset, float a, float x, float y, float z) {
if (rm == NULL) {
rm = (float *) malloc(sizeof(float) * 16);
memset(rm, 0, sizeof(float) * 16);
}
rm[rmOffset + 3] = 0;
rm[rmOffset + 7] = 0;
rm[rmOffset + 11] = 0;
rm[rmOffset + 12] = 0;
rm[rmOffset + 13] = 0;
rm[rmOffset + 14] = 0;
rm[rmOffset + 15] = 1;
a *= (float) (PI / 180.0f);
float s = (float) sin(a);
float c = (float) cos(a);
if (1.0f == x && 0.0f == y && 0.0f == z) {
rm[rmOffset + 5] = c;
rm[rmOffset + 10] = c;
rm[rmOffset + 6] = s;
rm[rmOffset + 9] = -s;
rm[rmOffset + 1] = 0;
rm[rmOffset + 2] = 0;
rm[rmOffset + 4] = 0;
rm[rmOffset + 8] = 0;
rm[rmOffset + 0] = 1;
} else if (0.0f == x && 1.0f == y && 0.0f == z) {
rm[rmOffset + 0] = c;
rm[rmOffset + 10] = c;
rm[rmOffset + 8] = s;
rm[rmOffset + 2] = -s;
rm[rmOffset + 1] = 0;
rm[rmOffset + 4] = 0;
rm[rmOffset + 6] = 0;
rm[rmOffset + 9] = 0;
rm[rmOffset + 5] = 1;
} else if (0.0f == x && 0.0f == y && 1.0f == z) {
rm[rmOffset + 0] = c;
rm[rmOffset + 5] = c;
rm[rmOffset + 1] = s;
rm[rmOffset + 4] = -s;
rm[rmOffset + 2] = 0;
rm[rmOffset + 6] = 0;
rm[rmOffset + 8] = 0;
rm[rmOffset + 9] = 0;
rm[rmOffset + 10] = 1;
} else {
float len = (float) sqrt(x * x + y * y + z * z);
if (1.0f != len) {
float recipLen = 1.0f / len;
x *= recipLen;
y *= recipLen;
z *= recipLen;
}
float nc = 1.0f - c;
float xy = x * y;
float yz = y * z;
float zx = z * x;
float xs = x * s;
float ys = y * s;
float zs = z * s;
rm[rmOffset + 0] = x * x * nc + c;
rm[rmOffset + 4] = xy * nc - zs;
rm[rmOffset + 8] = zx * nc + ys;
rm[rmOffset + 1] = xy * nc + zs;
rm[rmOffset + 5] = y * y * nc + c;
rm[rmOffset + 9] = yz * nc - xs;
rm[rmOffset + 2] = zx * nc - ys;
rm[rmOffset + 6] = yz * nc + xs;
rm[rmOffset + 10] = z * z * nc + c;
}
return rm;
}
void translateM(float *m, int mOffset, float x, float y, float z) {
if (m == NULL) {
LOGW_MATRIX("translateM() matrix is NULL than do nothing!");
return;
}
int i;
for (i = 0; i < 4; ++i) {
int mi = mOffset + i;
m[12 + mi] += m[mi] * x + m[4 + mi] * y + m[8 + mi] * z;
}
}
float *setLookAtM(float *rm, int rmOffset, float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ, float upX, float upY,
float upZ) {
// See the OpenGL GLUT documentation for gluLookAt for a description
// of the algorithm. We implement it in a straightforward way:
if (NULL == rm) {
rm = (float *) malloc(sizeof(float) * 16);
memset(rm, 0, sizeof(float) * 16);
}
float fx = centerX - eyeX;
float fy = centerY - eyeY;
float fz = centerZ - eyeZ;
// Normalize f
float rlf = 1.0f / length(fx, fy, fz);
fx *= rlf;
fy *= rlf;
fz *= rlf;
// compute s = f x up (x means "cross product")
float sx = fy * upZ - fz * upY;
float sy = fz * upX - fx * upZ;
float sz = fx * upY - fy * upX;
// and normalize s
float rls = 1.0f / length(sx, sy, sz);
sx *= rls;
sy *= rls;
sz *= rls;
// compute u = s x f
float ux = sy * fz - sz * fy;
float uy = sz * fx - sx * fz;
float uz = sx * fy - sy * fx;
rm[rmOffset + 0] = sx;
rm[rmOffset + 1] = ux;
rm[rmOffset + 2] = -fx;
rm[rmOffset + 3] = 0.0f;
rm[rmOffset + 4] = sy;
rm[rmOffset + 5] = uy;
rm[rmOffset + 6] = -fy;
rm[rmOffset + 7] = 0.0f;
rm[rmOffset + 8] = sz;
rm[rmOffset + 9] = uz;
rm[rmOffset + 10] = -fz;
rm[rmOffset + 11] = 0.0f;
rm[rmOffset + 12] = 0.0f;
rm[rmOffset + 13] = 0.0f;
rm[rmOffset + 14] = 0.0f;
rm[rmOffset + 15] = 1.0f;
translateM(rm, rmOffset, -eyeX, -eyeY, -eyeZ);
return rm;
}
float length(float x, float y, float z) {
return (float) sqrt(x * x + y * y + z * z);
}
void matrixMM4(float *left, float *right) {
float *tmp = (float *) malloc(sizeof(float) * 16);
memset(tmp, 0, sizeof(float) * 16);
typedef float (*DyadicArray)[4]; //��ά�������Ͷ���
DyadicArray dLeft = (DyadicArray) left; //����ǿת��,���ͷ��Ƿ���Ӱ��.
DyadicArray dRight = (DyadicArray) right;
DyadicArray dTmp = (DyadicArray) tmp;
int i, j, len;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
float t;
t = 0.0f;
for (len = 0; len < 4; ++len) {
t += (dLeft[i][len] * dRight[len][j]);
}
dTmp[i][j] = t;
}
}
memcpy(left, dTmp, 16 * sizeof(float));
free(tmp);
}
/**
* Defines a projection matrix in terms of six clip planes.
*
* @param m the float array that holds the output perspective matrix
* @param offset the offset into float array m where the perspective
* matrix data is written
* @param left
* @param right
* @param bottom
* @param top
* @param near
* @param far
*/
float *
frustumM(float *m, int offset, float left, float right, float bottom,
float top, float near, GLfloat far) {
if (NULL == m) {
m = (float *) malloc(sizeof(float) * 16);
memset(m, 0, sizeof(float) * 16);
}
if (left == right) {
LOGW_MATRIX("frustumM() left == right!!!");
}
if (top == bottom) {
LOGW_MATRIX("frustumM() top == bottom!!!");
}
if (near == far) {
LOGW_MATRIX("frustumM() near == far!!!");
}
if (near <= 0.0f) {
LOGW_MATRIX("frustumM() near <= 0.0f!!! near = %lf", near);
}
if (far <= 0.0f) {
LOGW_MATRIX("frustumM() far <= 0.0f!!! far = %lf", far);
}
float r_width = 1.0f / (right - left);
float r_height = 1.0f / (top - bottom);
float r_depth = 1.0f / (near - far);
float x = 2.0f * (near * r_width);
float y = 2.0f * (near * r_height);
float A = (right + left) * r_width;
float B = (top + bottom) * r_height;
float C = (far + near) * r_depth;
float D = 2.0f * (far * near * r_depth);
m[offset + 0] = x;
m[offset + 5] = y;
m[offset + 8] = A;
m[offset + 9] = B;
m[offset + 10] = C;
m[offset + 14] = D;
m[offset + 11] = -1.0f;
m[offset + 1] = 0.0f;
m[offset + 2] = 0.0f;
m[offset + 3] = 0.0f;
m[offset + 4] = 0.0f;
m[offset + 6] = 0.0f;
m[offset + 7] = 0.0f;
m[offset + 12] = 0.0f;
m[offset + 13] = 0.0f;
m[offset + 15] = 0.0f;
return m;
}
void printArray(char *name, float *rm) {
char str[1000] = {0};
int i;
for (i = 0; i < 16; ++i) {
sprintf(str + strlen(str), "%s[%d] = %g,", name, i, rm[i]);
}
LOGI_MATRIX("%s = %s", name, str);
}
#ifdef __cplusplus
}
#endif
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
renderer.zip (91个子文件)
renderer
sdl
SDL_power.h 2KB
SDL_opengles2_gl2platform.h 913B
SDL_config_pandora.h 3KB
SDL_config_wiz.h 3KB
SDL_pixels.h 17KB
SDL_revision.h 79B
SDL_test_assert.h 3KB
SDL_config_winrt.h 6KB
SDL_system.h 7KB
SDL_haptic.h 38KB
SDL_copying.h 939B
SDL_scancode.h 15KB
SDL_blendmode.h 2KB
SDL_test_crc32.h 3KB
SDL_config_windows.h 6KB
SDL_quit.h 2KB
SDL_log.h 6KB
SDL_filesystem.h 5KB
SDL_opengles2_gl2.h 31KB
SDL_test_harness.h 4KB
SDL_egl.h 72KB
SDL_mouse.h 11KB
SDL_loadso.h 3KB
SDL_version.h 5KB
SDL_messagebox.h 5KB
SDL_test_images.h 2KB
SDL_bits.h 2KB
SDL_audio.h 27KB
begin_code.h 4KB
SDL_rwops.h 7KB
SDL_config_android.h 4KB
SDL_opengles.h 1KB
SDL_test.h 2KB
SDL_opengles2_gl2ext.h 96KB
SDL_video.h 43KB
SDL_touch.h 2KB
close_code.h 1KB
SDL_error.h 2KB
SDL_test_random.h 3KB
SDL_render.h 34KB
SDL_hints.h 32KB
SDL_gamecontroller.h 11KB
SDL_config_macosx.h 5KB
SDL_cpuinfo.h 4KB
SDL_test_compare.h 2KB
SDL_assert.h 11KB
SDL_keycode.h 14KB
SDL_opengles2.h 2KB
SDL_events.h 27KB
SDL_endian.h 6KB
SDL_gesture.h 2KB
SDL_config.h 2KB
SDL_mutex.h 7KB
SDL_atomic.h 9KB
SDL_config.h.cmake 17KB
SDL_stdinc.h 18KB
SDL_test_md5.h 5KB
SDL_platform.h 5KB
SDL.h 4KB
SDL_test_common.h 5KB
SDL_config_iphoneos.h 4KB
SDL_main.h 4KB
SDL_opengl.h 80KB
SDL_test_fuzzer.h 13KB
SDL_keyboard.h 6KB
SDL_test_log.h 2KB
SDL_clipboard.h 2KB
SDL_shape.h 6KB
SDL_opengles2_khrplatform.h 10KB
SDL_thread.h 10KB
SDL_syswm.h 9KB
SDL_name.h 1KB
SDL_config.h.in 9KB
SDL_rect.h 4KB
SDL_test_font.h 2KB
SDL_joystick.h 9KB
SDL_config_minimal.h 3KB
SDL_config_psp.h 4KB
SDL_opengl_glext.h 714KB
SDL_types.h 1KB
SDL_timer.h 3KB
SDL_surface.h 19KB
SimpleAndroidGLRenderer.cpp 11KB
glutils
matrix.c 6KB
esUtil.h 2KB
shaders.h 1KB
esUtil.c 3KB
matrix.h 974B
BaseRenderer.cpp 322B
SimpleAndroidGLRenderer.h 2KB
BaseRenderer.h 458B
共 91 条
- 1
vonchenchen1
- 粉丝: 4151
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页