Commit: 0e7c3dfe75cb16158eb5df0520f32b7b7f0921f0 Author: Mike Erwin Date: Thu Sep 22 12:11:40 2016 +0200 Branches: blender2.8 https://developer.blender.org/rB0e7c3dfe75cb16158eb5df0520f32b7b7f0921f0
OpenGL: matrix code from viewport-fx Bringing over whole files from rB194998766c65 =================================================================== A source/blender/gpu/GPU_matrix.h A source/blender/gpu/intern/gpu_matrix.c =================================================================== diff --git a/source/blender/gpu/GPU_matrix.h b/source/blender/gpu/GPU_matrix.h new file mode 100644 index 0000000..9d61084 --- /dev/null +++ b/source/blender/gpu/GPU_matrix.h @@ -0,0 +1,106 @@ +#ifndef _GPU_MATRIX_H_ +#define _GPU_MATRIX_H_ + +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2012 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Alexandr Kuznetsov, Jason Wilkins + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file source/blender/gpu/GPU_matrix.h + * \ingroup gpu + */ + +#include "GPU_glew.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum eGPUMatrixMode { + GPU_MODELVIEW_MATRIX = 0, + GPU_PROJECTION_MATRIX = 1, + GPU_TEXTURE_MATRIX = 2 +} eGPUMatrixMode; + +void gpuPushMatrix(eGPUMatrixMode stack); +void gpuPopMatrix(eGPUMatrixMode stack); + +void gpuLoadMatrix(eGPUMatrixMode stack, const float m[16]); +void gpuLoadMatrixd(eGPUMatrixMode stack, const double m[16]); +const float *gpuGetMatrix(eGPUMatrixMode stack, float m[16]); +void gpuGetMatrixd(eGPUMatrixMode stack, double m[16]); + +void gpuLoadIdentity(eGPUMatrixMode stack); + +void gpuMultMatrix(eGPUMatrixMode stack, const float m[16]); +void gpuMultMatrixd(eGPUMatrixMode stack, const double m[16]); + +void gpuTranslate(eGPUMatrixMode stack, float x, float y, float z); +void gpuScale(eGPUMatrixMode stack, GLfloat x, GLfloat y, GLfloat z); +void gpuRotateVector(eGPUMatrixMode stack, GLfloat deg, GLfloat vector[3]); +void gpuRotateAxis(eGPUMatrixMode stack, GLfloat deg, char axis); +void gpuRotateRight(eGPUMatrixMode stack, char type); + +void gpuOrtho(eGPUMatrixMode stack, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal); +void gpuFrustum(eGPUMatrixMode stack, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal); + +void gpuLoadOrtho(eGPUMatrixMode stack, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal); +void gpuLoadFrustum(eGPUMatrixMode stack, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearVal, GLfloat farVal); + +void gpuLookAt(eGPUMatrixMode stack, GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat upX, GLfloat upY, GLfloat upZ); + +void gpuProject(const GLfloat obj[3], const GLfloat model[16], const GLfloat proj[16], const GLint view[4], GLfloat win[3]); +GLboolean gpuUnProject(const GLfloat win[3], const GLfloat model[16], const GLfloat proj[16], const GLint view[4], GLfloat obj[3]); + +void gpu_commit_matrix(void); + +void GPU_feedback_vertex_3fv(GLenum type, GLfloat x, GLfloat y, GLfloat z, GLfloat out[3]); +void GPU_feedback_vertex_4fv(GLenum type, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat out[4]); +void GPU_feedback_vertex_4dv(GLenum type, GLdouble x, GLdouble y, GLdouble z, GLdouble w, GLdouble out[4]); + +#if defined(GLEW_ES_ONLY) + +/* ES 2.0 doesn't define these symbolic constants, but the matrix stack replacement library emulates them + * (GL core has deprecated matrix stacks, but it should still be in the header) */ + +#ifndef GL_MODELVIEW_MATRIX +#define GL_MODELVIEW_MATRIX 0x0BA6 +#endif + +#ifndef GL_PROJECTION_MATRIX +#define GL_PROJECTION_MATRIX 0x0BA7 +#endif + +#ifndef GL_TEXTURE_MATRIX +#define GL_TEXTURE_MATRIX 0x0BA8 +#endif + +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* GPU_MATRIX_H */ diff --git a/source/blender/gpu/intern/gpu_matrix.c b/source/blender/gpu/intern/gpu_matrix.c new file mode 100644 index 0000000..29b8c02 --- /dev/null +++ b/source/blender/gpu/intern/gpu_matrix.c @@ -0,0 +1,468 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the ipmlied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2012 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Alexandr Kuznetsov, Jason Wilkins + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file source/blender/gpu/intern/gpu_matrix.c + * \ingroup gpu + */ + +#if WITH_GL_PROFILE_COMPAT +#define GPU_MANGLE_DEPRECATED 0 /* Allow use of deprecated OpenGL functions in this file */ +#endif + +#include "BLI_sys_types.h" + +#include "GPU_common.h" +#include "GPU_debug.h" +#include "GPU_extensions.h" +#include "GPU_matrix.h" + +/* internal */ +#include "intern/gpu_private.h" + +/* external */ + +#include "BLI_math_matrix.h" +#include "BLI_math_rotation.h" +#include "BLI_math_vector.h" + +#include "MEM_guardedalloc.h" + + + +typedef GLfloat GPU_matrix[4][4]; + +typedef struct GPU_matrix_stack +{ + GLsizei size; + GLsizei pos; + GPU_matrix *dynstack; +} GPU_matrix_stack; + + +static GPU_matrix_stack mstacks[3]; + +/* Check if we have a good matrix */ +#ifdef WITH_GPU_SAFETY + +static void checkmat(GLfloat *m) +{ + GLint i; + + for (i = 0; i < 16; i++) { +#if _MSC_VER + BLI_assert(_finite(m[i])); +#else + BLI_assert(!isinf(m[i])); +#endif + } +} + +#define CHECKMAT(m) checkmat((GLfloat*)m) + +#else + +#define CHECKMAT(m) + +#endif + +static void ms_init(GPU_matrix_stack* ms, GLint initsize) +{ + BLI_assert(initsize > 0); + + ms->size = initsize; + ms->pos = 0; + ms->dynstack = (GPU_matrix*)MEM_mallocN(ms->size * sizeof(*(ms->dynstack)), "MatrixStack"); +} + +static void ms_free(GPU_matrix_stack *ms) +{ + ms->size = 0; + ms->pos = 0; + MEM_freeN(ms->dynstack); + ms->dynstack = NULL; +} + +void gpu_matrix_init(void) +{ + ms_init(&mstacks[GPU_TEXTURE_MATRIX], 16); + ms_init(&mstacks[GPU_PROJECTION_MATRIX], 16); + ms_init(&mstacks[GPU_MODELVIEW_MATRIX], 32); +} + +void gpu_matrix_exit(void) +{ + ms_free(&mstacks[GPU_TEXTURE_MATRIX]); + ms_free(&mstacks[GPU_PROJECTION_MATRIX]); + ms_free(&mstacks[GPU_MODELVIEW_MATRIX]); +} + +void gpu_commit_matrix(void) +{ + const struct GPUcommon *common = gpu_get_common(); + + GPU_ASSERT_NO_GL_ERRORS("gpu_commit_matrix start"); + + if (common) { + int i; + + float (*m)[4] = (float (*)[4])gpuGetMatrix(GPU_MODELVIEW_MATRIX, NULL); + float (*p)[4] = (float (*)[4])gpuGetMatrix(GPU_PROJECTION_MATRIX, NULL); + + if (common->modelview_matrix != -1) + glUniformMatrix4fv(common->modelview_matrix, 1, GL_FALSE, m[0]); + + if (common->normal_matrix != -1) { + float n[3][3]; + copy_m3_m4(n, m); + invert_m3(n); + transpose_m3(n); + glUniformMatrix3fv(common->normal_matrix, 1, GL_FALSE, n[0]); + } + + if (common->modelview_matrix_inverse != -1) { + float i[4][4]; + invert_m4_m4(i, m); + glUniformMatrix4fv(common->modelview_matrix_inverse, 1, GL_FALSE, i[0]); + } + + if (common->modelview_projection_matrix != -1) { + float pm[4][4]; + mul_m4_m4m4(pm, p, m); + glUniformMatrix4fv(common->modelview_projection_matrix, 1, GL_FALSE, pm[0]); + } + + if (common->projection_matrix != -1) + glUniformMatrix4fv(common->projection_matrix, 1, GL_FALSE, p[0]); + + for (i = 0; i < GPU_MAX_COMMON_TEXCOORDS; i++) { + if (common->texture_matrix[i] != -1) { + GPU_set_common_active_texture(i); + glUniformMatrix4fv(common->texture_matrix[i], 1, GL_FALSE, gpuGetMatrix(GPU_TEXTURE_MATRIX, NULL)); + } + } + + GPU_set_common_active_texture(0); + + GPU_ASSERT_NO_GL_ERRORS("gpu_commit_matrix end"); + + return; + } + +#if defined(WITH_GL_PROFILE_COMPAT) + glMatrixMode(GL_TEXTURE); + glLoadMatrixf(gpuGetMatrix(GPU_TEXTURE_MATRIX, NULL)); + + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(gpuGetMatrix(GPU_PROJECTION_MATRIX, NULL)); + + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(gpuGetMatrix(GPU_MODELVIEW_MATRIX, NULL)); +#endif + + GPU_ASSERT_NO_GL_ERRORS("gpu_commit_matrix end"); +} + +void gpuPushMatrix(eGPUMatrixMode stack) +{ + GPU_matrix_stack *ms_current = mstacks + stack; + GLsizei new_pos = ms_current->pos + 1; + + BLI_assert(new_pos < ms_current->size); + + if (new_pos < ms_current->size) { + ms_current->pos++; + + gpuLoadMatrix(stack, ms_current->dynstack[ms_current->pos - 1][0]); + } +} + +void gpuPopMatrix(eGPUMatrixMode stack) +{ + GPU_matrix_stack *ms_current = mstacks + stack; + BLI_assert(ms_current->pos != 0); + + if (ms_current->pos != 0) { + ms_current->pos--; + + CHECKMAT(ms_current); + } +} + +void gpuLoadMatrix(eGPUMatrixMode stack, const float *m) +{ + GPU_matrix_stack *ms_current = mstacks + stack; + copy_m4_m4(ms_current->dynstack[ms_current->pos], (GLfloat (*)[4])m); + + CHECKMAT(ms_current); +} + +void gpuLoadMatrixd(eGPUMatrixMode stack, const double *md) +{ + GPU_matrix_stack *ms_current = mstacks + stack; + float mf[16]; + int i; + + for (i = 0; i < 16; i++) { + mf[i] = md[i]; + } + + copy_m4_m4(ms_current->dynstack[ms_current->pos], (float (*)[4])mf); + + CHECKMAT(ms_current); +} + + +const GLfloat *gpuGetMatrix(eGPUMatrixMode stack, GLfloat *m) +{ + GPU_matrix_stack *ms_select = mstacks + stack; + + if @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs