From c94455c14d28221f6e05f33ba42e23a5d3245a28 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Sun, 8 Jan 2006 09:37:15 +0000 Subject: more linux game engine work. hopefully works now! --- SConstruct | 2 +- source/gameengine/Converter/BL_Material.cpp | 132 ---- source/gameengine/Converter/BL_Material.h | 155 ----- source/gameengine/Converter/BL_Shader.cpp | 914 ---------------------------- source/gameengine/Converter/BL_Shader.h | 117 ---- source/gameengine/Converter/BL_Texture.cpp | 389 ------------ source/gameengine/Converter/BL_Texture.h | 53 -- source/gameengine/Converter/SConscript | 5 +- source/gameengine/Ketsji/BL_Material.cpp | 132 ++++ source/gameengine/Ketsji/BL_Material.h | 155 +++++ source/gameengine/Ketsji/BL_Shader.cpp | 914 ++++++++++++++++++++++++++++ source/gameengine/Ketsji/BL_Shader.h | 117 ++++ source/gameengine/Ketsji/BL_Texture.cpp | 389 ++++++++++++ source/gameengine/Ketsji/BL_Texture.h | 53 ++ source/gameengine/Ketsji/KX_PythonInit.cpp | 2 +- source/gameengine/Ketsji/SConscript | 5 +- 16 files changed, 1767 insertions(+), 1767 deletions(-) delete mode 100644 source/gameengine/Converter/BL_Material.cpp delete mode 100644 source/gameengine/Converter/BL_Material.h delete mode 100644 source/gameengine/Converter/BL_Shader.cpp delete mode 100644 source/gameengine/Converter/BL_Shader.h delete mode 100644 source/gameengine/Converter/BL_Texture.cpp delete mode 100644 source/gameengine/Converter/BL_Texture.h create mode 100644 source/gameengine/Ketsji/BL_Material.cpp create mode 100644 source/gameengine/Ketsji/BL_Material.h create mode 100644 source/gameengine/Ketsji/BL_Shader.cpp create mode 100644 source/gameengine/Ketsji/BL_Shader.h create mode 100644 source/gameengine/Ketsji/BL_Texture.cpp create mode 100644 source/gameengine/Ketsji/BL_Texture.h diff --git a/SConstruct b/SConstruct index 963d76b68c5..e998ef82ebd 100644 --- a/SConstruct +++ b/SConstruct @@ -63,7 +63,7 @@ env = Environment (ENV = os.environ) if sys.platform == 'linux2' or sys.platform == 'linux-i386': use_international = 'true' - use_gameengine = 'false' + use_gameengine = 'true' use_openal = 'false' use_fmod = 'false' use_quicktime = 'false' diff --git a/source/gameengine/Converter/BL_Material.cpp b/source/gameengine/Converter/BL_Material.cpp deleted file mode 100644 index b77750c1644..00000000000 --- a/source/gameengine/Converter/BL_Material.cpp +++ /dev/null @@ -1,132 +0,0 @@ -// ------------------------------------ -#ifdef WIN32 -#include -#endif // WIN32 -#ifdef __APPLE__ -#include -#include -#else -#include -#include -#endif - - -#include - -#include "BL_Material.h" -#include "MT_assert.h" - -#include "DNA_material_types.h" -#include "DNA_texture_types.h" -#include "DNA_image_types.h" -#include "DNA_mesh_types.h" -#include "IMB_imbuf_types.h" -#include "IMB_imbuf.h" - -MTex* getImageFromMaterial(Material *mat, int index) -{ - if(!mat) return 0; - - if(!(index >=0 && index <=10) ) return 0; - - MTex *m = mat->mtex[index]; - return m?m:0; -} - -int getNumTexChannels( Material *mat ) -{ - int count = -1; - if(!mat) return -1; - - for(count =0; (count < 10) && mat->mtex[count] != 0; count++) {} - return count; -} - -BL_Material::BL_Material() -{ - rgb[0] = 0; - rgb[1] = 0; - rgb[2] = 0; - rgb[3] = 0; - IdMode = 0; - ras_mode = 0; - tile = 0; - matname = "NoMaterial"; - matcolor[0] = 0.5f; - matcolor[1] = 0.5f; - matcolor[2] = 0.5f; - matcolor[3] = 0.5f; - speccolor[0] = 1.f; - speccolor[1] = 1.f; - speccolor[2] = 1.f; - transp = 0; - hard = 50.f; - spec_f = 0.5f; - alpha = 1.f; - emit = 0.f; - mode = 0; - material = 0; - tface = 0; - material_index = 0; - amb=0.5f; - num_enabled = 0; - - int i; - for(i=0; i<4; i++) - uv[i] = MT_Point2(0.f,1.f); - - for(i=0; i -#endif // WIN32 -#ifdef __APPLE__ -#include -#include -#else -#include -#include -#endif - - -#include -#include "BL_Shader.h" -#include "BL_Material.h" - -#include "MT_assert.h" -#include "MT_Matrix4x4.h" -#include "MT_Matrix3x3.h" -#include "KX_PyMath.h" - -#include "RAS_GLExtensionManager.h" - -//using namespace bgl; -#define spit(x) std::cout << x << std::endl; - - -const bool BL_Shader::Ok()const -{ - return (mShader !=0 && mOk && mUse); -} - -BL_Shader::BL_Shader(int n, PyTypeObject *T) -: PyObjectPlus(T), - mShader(0), - mVert(0), - mFrag(0), - mPass(1), - vertProg(""), - fragProg(""), - mOk(0), - mUse(0) -{ - // if !RAS_EXT_support._ARB_shader_objects this class will not be used - - mBlending.src = -1; - mBlending.dest = -1; - mBlending.const_color[0] = 0.0; - mBlending.const_color[1] = 0.0; - mBlending.const_color[2] = 0.0; - mBlending.const_color[3] = 1.0; - - for (int i=0; i 1) - { - GLcharARB*logger = (GLcharARB*)malloc(sizeof(GLcharARB)*length); - int chars=0; - - glGetInfoLogARB(pr, length, &chars, logger); - if(chars>0) - std::cout << (logger) << std::endl; - - if(logger) - free(logger); - } -#endif//GL_ARB_shader_objects -} - -char *BL_Shader::GetVertPtr() -{ - return vertProg?vertProg:0; -} - -char *BL_Shader::GetFragPtr() -{ - return fragProg?fragProg:0; -} - -void BL_Shader::SetVertPtr( char *vert ) -{ - vertProg = vert; -} - -void BL_Shader::SetFragPtr( char *frag ) -{ - fragProg = frag; -} - -unsigned int BL_Shader::GetProg() -{ - return mShader; -} - -unsigned int BL_Shader::GetVertexShader() -{ - return mVert; -} - -unsigned int BL_Shader::GetFragmentShader() -{ - return mFrag; -} - -const uSampler* BL_Shader::getSampler(int i) -{ - MT_assert(i<=MAXTEX); - return &mSampler[i]; -} - -const uBlending *BL_Shader::getBlending( int pass ) -{ - return &mBlending; -} - - - -void BL_Shader::InitializeSampler( - int type, - int unit, - int pass, - unsigned int texture) -{ - MT_assert(unit<=MAXTEX); - mSampler[unit].glTexture = texture; - mSampler[unit].loc =-1; - mSampler[unit].pass=0; - mSampler[unit].type=type; - mSampler[unit].unit=unit; -} - -PyObject* BL_Shader::_getattr(const STR_String& attr) -{ - _getattr_up(PyObjectPlus); -} - - -PyMethodDef BL_Shader::Methods[] = -{ - // creation - KX_PYMETHODTABLE( BL_Shader, setSource ), - KX_PYMETHODTABLE( BL_Shader, delSource ), - KX_PYMETHODTABLE( BL_Shader, getVertexProg ), - KX_PYMETHODTABLE( BL_Shader, getFragmentProg ), - KX_PYMETHODTABLE( BL_Shader, setNumberOfPasses ), - KX_PYMETHODTABLE( BL_Shader, validate), - /// access functions - KX_PYMETHODTABLE( BL_Shader, isValid), - KX_PYMETHODTABLE( BL_Shader, setUniform1f ), - KX_PYMETHODTABLE( BL_Shader, setUniform2f ), - KX_PYMETHODTABLE( BL_Shader, setUniform3f ), - KX_PYMETHODTABLE( BL_Shader, setUniform4f ), - KX_PYMETHODTABLE( BL_Shader, setUniform1i ), - KX_PYMETHODTABLE( BL_Shader, setUniform2i ), - KX_PYMETHODTABLE( BL_Shader, setUniform3i ), - KX_PYMETHODTABLE( BL_Shader, setUniform4i ), - - KX_PYMETHODTABLE( BL_Shader, setUniformfv ), - KX_PYMETHODTABLE( BL_Shader, setUniformiv ), - - KX_PYMETHODTABLE( BL_Shader, setSampler ), - KX_PYMETHODTABLE( BL_Shader, setUniformMatrix4 ), - KX_PYMETHODTABLE( BL_Shader, setUniformMatrix3 ), - // KX_PYMETHODTABLE( BL_Shader, setBlending ), - - {NULL,NULL} //Sentinel -}; - - -PyTypeObject BL_Shader::Type = { - PyObject_HEAD_INIT(&PyType_Type) - 0, - "BL_Shader", - sizeof(BL_Shader), - 0, - PyDestructor, - 0, - __getattr, - __setattr, - 0, - __repr, - 0 -}; - - -PyParentObject BL_Shader::Parents[] = { - &PyObjectPlus::Type, - &BL_Shader::Type, - NULL -}; - - -KX_PYMETHODDEF_DOC( BL_Shader, setSource," setSource(vertexProgram, fragmentProgram)" ) -{ -#ifdef GL_ARB_shader_objects - if(mShader !=0 && mOk ) - { - // already set... - Py_Return; - } - - char *v,*f; - int apply=0; - if( PyArg_ParseTuple(args, "ssi", &v, &f, &apply) ) - { - vertProg = v; - fragProg = f; - if( LinkProgram() ) - { - glUseProgramObjectARB( mShader ); - mUse = apply!=0; - Py_Return; - } - else - { - vertProg = 0; - fragProg = 0; - mUse = 0; - glUseProgramObjectARB( 0 ); - } - } - return NULL; -#else - Py_Return; -#endif -} - - -KX_PYMETHODDEF_DOC( BL_Shader, delSource, "delSource( )" ) -{ -#ifdef GL_ARB_shader_objects - glDeleteObjectARB(mShader); - glDeleteObjectARB(mFrag); - glDeleteObjectARB(mVert); - mShader = 0; - mFrag = 0; - mVert = 0; - vertProg = 0; - fragProg = 0; - mOk = 0; - mUse = 0; - glUseProgramObjectARB(0); -#endif - Py_Return; - -} - - -KX_PYMETHODDEF_DOC( BL_Shader, isValid, "isValid()" ) -{ - return PyInt_FromLong( ( mShader !=0 && mOk ) ); -} - -KX_PYMETHODDEF_DOC( BL_Shader, getVertexProg ,"getVertexProg( )" ) -{ - return PyString_FromString(vertProg?vertProg:""); -} - -KX_PYMETHODDEF_DOC( BL_Shader, getFragmentProg ,"getFragmentProg( )" ) -{ - return PyString_FromString(fragProg?fragProg:""); -} - - -KX_PYMETHODDEF_DOC( BL_Shader, validate, "validate()") -{ -#ifdef GL_ARB_shader_objects - if(mShader==0) - { - PyErr_Format(PyExc_TypeError, "invalid shader object"); - return NULL; - } - - int stat = 0; - glValidateProgramARB(mShader); - glGetObjectParameterivARB(mShader, GL_OBJECT_VALIDATE_STATUS_ARB, &stat); - printInfo(mShader); - - return PyInt_FromLong((stat!=0)); -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setSampler, "setSampler(name, index)" ) -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - int index=-1; - if(PyArg_ParseTuple(args, "si", &uniform, &index)) - { - if(mShader==0) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader, uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - if(index <= MAXTEX) - { - mSampler[index].loc = loc; - }else - { - spit("Invalid texture sample index: " << index); - } - Py_Return; - } - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - -KX_PYMETHODDEF_DOC( BL_Shader, setNumberOfPasses, "setNumberOfPasses( max-pass )" ) -{ - int pass = 1; - if(!PyArg_ParseTuple(args, "i", &pass)) - return NULL; - - mPass = pass; - Py_Return; -} - -/// access functions -KX_PYMETHODDEF_DOC( BL_Shader, setUniform1f, "setUniform1f(name, fx)" ) -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - float value=0; - if(PyArg_ParseTuple(args, "sf", &uniform, &value )) - { - if( mShader==0 ) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader, uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB( mShader ); - glUniform1fARB( loc, value ); - Py_Return; - } - - } - return NULL; -#else - Py_Return; -#endif -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform2f , "setUniform2f(name, fx, fy)") -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - float array[2]={ 0,0 }; - if(PyArg_ParseTuple(args, "sff", &uniform, &array[0],&array[1] )) - { - if( mShader==0 ) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB( mShader ); - glUniform2fARB(loc, array[0],array[1] ); - Py_Return; - } - - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform3f, "setUniform3f(name, fx,fy,fz) ") -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - float array[3]={0,0,0}; - if(PyArg_ParseTuple(args, "sfff", &uniform, &array[0],&array[1],&array[2])) - { - if(mShader==0) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB(mShader); - glUniform3fARB(loc, array[0],array[1],array[2]); - Py_Return; - } - - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform4f, "setUniform4f(name, fx,fy,fz, fw) ") -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - float array[4]={0,0,0,0}; - if(PyArg_ParseTuple(args, "sffff", &uniform, &array[0],&array[1],&array[2], &array[3])) - { - if(mShader==0) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB(mShader); - glUniform4fARB(loc, array[0],array[1],array[2], array[3]); - Py_Return; - } - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform1i, "setUniform1i(name, ix)" ) -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - int value=0; - if(PyArg_ParseTuple(args, "si", &uniform, &value )) - { - if( mShader==0 ) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader, uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB( mShader ); - glUniform1iARB( loc, value ); - Py_Return; - } - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform2i , "setUniform2i(name, ix, iy)") -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - int array[2]={ 0,0 }; - if(PyArg_ParseTuple(args, "sii", &uniform, &array[0],&array[1] )) - { - if( mShader==0 ) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB( mShader ); - glUniform2iARB(loc, array[0],array[1] ); - Py_Return; - } - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform3i, "setUniform3i(name, ix,iy,iz) ") -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - int array[3]={0,0,0}; - if(PyArg_ParseTuple(args, "siii", &uniform, &array[0],&array[1],&array[2])) - { - if(mShader==0) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB(mShader); - glUniform3iARB(loc, array[0],array[1],array[2]); - Py_Return; - } - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - -KX_PYMETHODDEF_DOC( BL_Shader, setUniform4i, "setUniform4i(name, ix,iy,iz, iw) ") -{ -#ifdef GL_ARB_shader_objects - char *uniform=""; - int array[4]={0,0,0, 0}; - if(PyArg_ParseTuple(args, "siiii", &uniform, &array[0],&array[1],&array[2], &array[3] )) - { - if(mShader==0) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - glUseProgramObjectARB(mShader); - glUniform4iARB(loc, array[0],array[1],array[2], array[3]); - Py_Return; - } - } - return NULL; -#else - Py_Return; -#endif//GL_ARB_shader_objects -} - -KX_PYMETHODDEF_DOC( BL_Shader, setUniformfv , "setUniformfv( float (list2 or list3 or list4) )") -{ -#ifdef GL_ARB_shader_objects - char*uniform = ""; - PyObject *listPtr =0; - float array_data[4] = {0.f,0.f,0.f,0.f}; - - if(PyArg_ParseTuple(args, "sO", &uniform, &listPtr)) - { - if(mShader==0) - { - PyErr_Format(PyExc_ValueError, "invalid shader object"); - return NULL; - } - int loc= glGetUniformLocationARB(mShader , uniform); - if( loc==-1 ) - { - spit("Invalid uniform value: " << uniform << "."); - Py_Return; - }else - { - if(PySequence_Check(listPtr)) - { - unsigned int list_size = PySequence_Size(listPtr); - - for(unsigned int i=0; (i -#endif // WIN32 -#ifdef __APPLE__ -#include -#include -#else -#include -#include -#endif - -#include - -#include "BL_Material.h" -#include "BL_Texture.h" -#include "MT_assert.h" - -#include "DNA_texture_types.h" -#include "DNA_image_types.h" -#include "IMB_imbuf_types.h" -#include "BKE_image.h" -//#include "IMB_imbuf.h" -#include "BLI_blenlib.h" - -#include "RAS_GLExtensionManager.h" -using namespace bgl; - -#define spit(x) std::cout << x << std::endl; - -#include "MEM_guardedalloc.h" - - - -extern "C" { - // envmaps - #include "IMB_imbuf.h" - void my_envmap_split_ima(EnvMap *env); - void my_free_envmapdata(EnvMap *env); -} - -// (n&(n-1)) zeros the least significant bit of n -static int is_pow2(int num) { - return ((num)&(num-1))==0; -} -static int smaller_pow2(int num) { - while (!is_pow2(num)) - num= num&(num-1); - return num; -} - - - -BL_Texture::BL_Texture() -: mTexture(0), - mError(0), - mOk(0), - mNeedsDeleted(0), - mType(0), - mName("") -{ - // -- -} - -BL_Texture::~BL_Texture() -{ - // -- -} - -void BL_Texture::DeleteTex() -{ - if( mNeedsDeleted ) { - glDeleteTextures(1, (GLuint*)&(*mTexture)); - mNeedsDeleted = 0; - mOk = 0; - } -} - - -bool BL_Texture::InitFromImage( Image *img, bool mipmap) -{ - if(!img || img->ok==0 ) { - mError = true; - mOk = false; - return mOk; - } - if( img->ibuf==0 ) { - load_image(img, IB_rect, "", 0); - if(img->ibuf==0) { - img->ok = 0; - mError = true; - mOk = false; - return mOk; - } - } - mTexture = &img->bindcode; - mName = img->id.name; - mType = BL_TEX2D; - - // smoke em if we got em - if (*mTexture != 0) { - glBindTexture(GL_TEXTURE_2D, *mTexture ); - Validate(); - return mOk; - } - glGenTextures(1, (GLuint*)mTexture); - InitGLTex(img->ibuf->rect, img->ibuf->x, img->ibuf->y, mipmap); - Validate(); - return mOk; -} - -void BL_Texture::InitGLTex(unsigned int *pix,int x,int y,bool mipmap) -{ - if (!is_pow2(x) || !is_pow2(y) ) { - InitNonPow2Tex(pix, x,y,mipmap); - return; - } - - glBindTexture(GL_TEXTURE_2D, *mTexture ); - if( mipmap ) { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, x, y, GL_RGBA, GL_UNSIGNED_BYTE, pix ); - } - else { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix ); - } - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); -} - - -void BL_Texture::InitNonPow2Tex(unsigned int *pix,int x,int y,bool mipmap) -{ - int nx= smaller_pow2(x); - int ny= smaller_pow2(y); - - unsigned int *newPixels = (unsigned int *)malloc(nx*ny*sizeof(unsigned int)); - - gluScaleImage(GL_RGBA, x, y, GL_UNSIGNED_BYTE, pix, nx,ny, GL_UNSIGNED_BYTE, newPixels); - glBindTexture(GL_TEXTURE_2D, *mTexture ); - - if( mipmap ) { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, nx, ny, GL_RGBA, GL_UNSIGNED_BYTE, newPixels ); - } - else { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, nx, ny, 0, GL_RGBA, GL_UNSIGNED_BYTE, newPixels ); - } - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - free(newPixels); -} - - -bool BL_Texture::InitCubeMap( EnvMap *cubemap ) -{ -#ifdef GL_ARB_texture_cube_map - if(!RAS_EXT_support._ARB_texture_cube_map) { - spit("cubemaps not supported"); - mError = true; - mOk = false; - return mOk; - } - - else if(!cubemap || cubemap->ima->ok==0 ) { - mError = true; - mOk = false; - return mOk; - } - - if( cubemap->ima->ibuf==0 ) { - load_image(cubemap->ima, IB_rect, "", 0); - if(cubemap->ima->ibuf==0) { - cubemap->ima->ok = 0; - mError = true; - mOk = false; - return mOk; - } - } - - EnvMap *CubeMap = cubemap; - mNeedsDeleted = 1; - mBlankTexture = 0; - mType = BL_TEXCUBE; - mTexture = &mBlankTexture; - mName = CubeMap->ima->id.name; - - glGenTextures(1, (GLuint*)(mTexture)); - glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, *mTexture ); - bool needs_split = false; - - if(!CubeMap->cube[0]) needs_split = true; - - if(needs_split){ - // split it - my_envmap_split_ima(CubeMap); - } - - int x = cubemap->ima->ibuf->x; - int y = cubemap->ima->ibuf->y; - unsigned int *data= (unsigned int *)malloc(x*y*sizeof(unsigned int)); - - // ----------------------------------- - x = CubeMap->cube[0]->ibuf->x; - y = CubeMap->cube[0]->ibuf->y; - - // check the first image, and assume the rest - if (!is_pow2(x) || !is_pow2(y)) - { - spit("invalid envmap size please render with CubeRes @ power of two"); - free(data); - data = 0; - mError = true; - mOk = false; - return mOk; - } - memcpy(data, CubeMap->cube[0]->ibuf->rect, (x*y*sizeof(unsigned int))); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - - // ----------------------------------- - x = CubeMap->cube[1]->ibuf->x; - y = CubeMap->cube[1]->ibuf->y; - memcpy(data, CubeMap->cube[1]->ibuf->rect, (x*y*sizeof(unsigned int))); - glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - - // ----------------------------------- - x = CubeMap->cube[2]->ibuf->x; - y = CubeMap->cube[2]->ibuf->y; - memcpy(data, CubeMap->cube[2]->ibuf->rect, (x*y*sizeof(unsigned int))); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - - // ----------------------------------- - x = CubeMap->cube[3]->ibuf->x; - y = CubeMap->cube[3]->ibuf->y; - memcpy(data, CubeMap->cube[3]->ibuf->rect, (x*y*sizeof(unsigned int))); - glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - - // ----------------------------------- - x = CubeMap->cube[4]->ibuf->x; - y = CubeMap->cube[4]->ibuf->y; - memcpy(data, CubeMap->cube[4]->ibuf->rect, (x*y*sizeof(unsigned int))); - glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - - // ----------------------------------- - x = CubeMap->cube[5]->ibuf->x; - y = CubeMap->cube[5]->ibuf->y; - memcpy(data, CubeMap->cube[5]->ibuf->rect, (x*y*sizeof(unsigned int))); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - - glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_REPEAT ); - glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_REPEAT ); - - if(data) { - free(data); - data = 0; - } - - if(needs_split) { - // okay we allocated, swap back to orig and free used - cubemap->ima = CubeMap->ima; - my_free_envmapdata(CubeMap); - } - mOk = IsValid(); - return mOk; - -#else - - mError = true; - mOk = false; - return mOk; - -#endif//GL_ARB_texture_cube_map -} - - -STR_String BL_Texture::GetName() const -{ - return mName; -} - - -bool BL_Texture::IsValid() -{ - return (mTexture && *mTexture!= 0)?glIsTexture(*mTexture)!=0:false; -} - - -void BL_Texture::Validate() -{ - mOk = IsValid(); -} - - -bool BL_Texture::Ok() -{ - return ( mTexture?((!mError || mOk ) && *mTexture!= 0):0 ); -} - - -unsigned int BL_Texture::GetTextureType() const -{ - return mType; -} - - -BL_Texture::operator const unsigned int () const -{ - return mTexture? *mTexture:0; -} - -bool BL_Texture::SetGLTex(unsigned int tex) -{ - return false; -} - -extern "C" { - -void my_envmap_split_ima(EnvMap *env) -{ - ImBuf *ibuf; - Image *ima; - int dx, part; - - my_free_envmapdata(env); - - dx= env->ima->ibuf->y; - dx/= 2; - if(3*dx != env->ima->ibuf->x) { - printf("Incorrect envmap size\n"); - env->ok= 0; - env->ima->ok= 0; - } - else { - for(part=0; part<6; part++) { - ibuf= IMB_allocImBuf(dx, dx, 24, IB_rect, 0); - ima= (Image*)MEM_callocN(sizeof(Image), "image"); - ima->ibuf= ibuf; - ima->ok= 1; - env->cube[part]= ima; - } - IMB_rectop(env->cube[0]->ibuf, env->ima->ibuf, - 0, 0, 0, 0, dx, dx, IMB_rectcpy, 0); - IMB_rectop(env->cube[1]->ibuf, env->ima->ibuf, - 0, 0, dx, 0, dx, dx, IMB_rectcpy, 0); - IMB_rectop(env->cube[2]->ibuf, env->ima->ibuf, - 0, 0, 2*dx, 0, dx, dx, IMB_rectcpy, 0); - IMB_rectop(env->cube[3]->ibuf, env->ima->ibuf, - 0, 0, 0, dx, dx, dx, IMB_rectcpy, 0); - IMB_rectop(env->cube[4]->ibuf, env->ima->ibuf, - 0, 0, dx, dx, dx, dx, IMB_rectcpy, 0); - IMB_rectop(env->cube[5]->ibuf, env->ima->ibuf, - 0, 0, 2*dx, dx, dx, dx, IMB_rectcpy, 0); - env->ok= 2; - } -} - - -void my_free_envmapdata(EnvMap *env) -{ - Image *ima; - unsigned int a, part; - - for(part=0; part<6; part++) { - ima= env->cube[part]; - if(ima) { - if(ima->ibuf) IMB_freeImBuf(ima->ibuf); - - for(a=0; amipmap); a++) { - if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]); - } - MEM_freeN(ima); - env->cube[part]= 0; - } - } - env->ok= 0; -} - - -} - -unsigned int BL_Texture::mBlankTexture = 0; - diff --git a/source/gameengine/Converter/BL_Texture.h b/source/gameengine/Converter/BL_Texture.h deleted file mode 100644 index 51bf7270d2d..00000000000 --- a/source/gameengine/Converter/BL_Texture.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef __BL_TEXTURE_H__ -#define __BL_TEXTURE_H__ -#include -// -- -struct Image; -struct EnvMap; -// -- -#include "STR_String.h" - -class BL_Texture -{ -private: - // ----------------------------------- - unsigned int* mTexture; - bool mError; - bool mOk; - bool mNeedsDeleted; - unsigned int mType; - STR_String mName; - static unsigned int mBlankTexture; - std::vectormCubeMem; - // ----------------------------------- - void InitNonPow2Tex(unsigned int *p,int x,int y,bool mipmap ); - void InitGLTex(unsigned int *p,int x,int y,bool mipmap ); - -public: - BL_Texture(); - ~BL_Texture( ); - - operator const unsigned int () const; - bool Ok(); - - STR_String GetName() const; - - unsigned int GetTextureType() const; - void DeleteTex(); - bool InitFromImage( Image *img, bool mipmap); - bool InitCubeMap( EnvMap *cubemap ); - // - bool SetGLTex(unsigned int tex); - - void PopCubeMap(); - bool IsValid(); - void Validate(); -}; - -enum TexType{ - BL_TEX2D = 1, - BL_TEXCUBE = 2 -}; - - -#endif//__BL_TEXTURE_H__ diff --git a/source/gameengine/Converter/SConscript b/source/gameengine/Converter/SConscript index 65b3d9d0a9d..361aeed4120 100755 --- a/source/gameengine/Converter/SConscript +++ b/source/gameengine/Converter/SConscript @@ -18,10 +18,7 @@ source_files = ['KX_IpoConvert.cpp', 'BL_DeformableGameObject.cpp', 'BL_BlenderDataConversion.cpp', 'BL_ArmatureObject.cpp', - 'BL_ActionActuator.cpp', - 'BL_Material.cpp', - 'BL_Texture.cpp', - 'BL_Shader.cpp' + 'BL_ActionActuator.cpp' ] kx_converter_env.Append (CPPPATH = ['.', diff --git a/source/gameengine/Ketsji/BL_Material.cpp b/source/gameengine/Ketsji/BL_Material.cpp new file mode 100644 index 00000000000..b77750c1644 --- /dev/null +++ b/source/gameengine/Ketsji/BL_Material.cpp @@ -0,0 +1,132 @@ +// ------------------------------------ +#ifdef WIN32 +#include +#endif // WIN32 +#ifdef __APPLE__ +#include +#include +#else +#include +#include +#endif + + +#include + +#include "BL_Material.h" +#include "MT_assert.h" + +#include "DNA_material_types.h" +#include "DNA_texture_types.h" +#include "DNA_image_types.h" +#include "DNA_mesh_types.h" +#include "IMB_imbuf_types.h" +#include "IMB_imbuf.h" + +MTex* getImageFromMaterial(Material *mat, int index) +{ + if(!mat) return 0; + + if(!(index >=0 && index <=10) ) return 0; + + MTex *m = mat->mtex[index]; + return m?m:0; +} + +int getNumTexChannels( Material *mat ) +{ + int count = -1; + if(!mat) return -1; + + for(count =0; (count < 10) && mat->mtex[count] != 0; count++) {} + return count; +} + +BL_Material::BL_Material() +{ + rgb[0] = 0; + rgb[1] = 0; + rgb[2] = 0; + rgb[3] = 0; + IdMode = 0; + ras_mode = 0; + tile = 0; + matname = "NoMaterial"; + matcolor[0] = 0.5f; + matcolor[1] = 0.5f; + matcolor[2] = 0.5f; + matcolor[3] = 0.5f; + speccolor[0] = 1.f; + speccolor[1] = 1.f; + speccolor[2] = 1.f; + transp = 0; + hard = 50.f; + spec_f = 0.5f; + alpha = 1.f; + emit = 0.f; + mode = 0; + material = 0; + tface = 0; + material_index = 0; + amb=0.5f; + num_enabled = 0; + + int i; + for(i=0; i<4; i++) + uv[i] = MT_Point2(0.f,1.f); + + for(i=0; i +#endif // WIN32 +#ifdef __APPLE__ +#include +#include +#else +#include +#include +#endif + + +#include +#include "BL_Shader.h" +#include "BL_Material.h" + +#include "MT_assert.h" +#include "MT_Matrix4x4.h" +#include "MT_Matrix3x3.h" +#include "KX_PyMath.h" + +#include "RAS_GLExtensionManager.h" + +//using namespace bgl; +#define spit(x) std::cout << x << std::endl; + + +const bool BL_Shader::Ok()const +{ + return (mShader !=0 && mOk && mUse); +} + +BL_Shader::BL_Shader(int n, PyTypeObject *T) +: PyObjectPlus(T), + mShader(0), + mVert(0), + mFrag(0), + mPass(1), + vertProg(""), + fragProg(""), + mOk(0), + mUse(0) +{ + // if !RAS_EXT_support._ARB_shader_objects this class will not be used + + mBlending.src = -1; + mBlending.dest = -1; + mBlending.const_color[0] = 0.0; + mBlending.const_color[1] = 0.0; + mBlending.const_color[2] = 0.0; + mBlending.const_color[3] = 1.0; + + for (int i=0; i 1) + { + GLcharARB*logger = (GLcharARB*)malloc(sizeof(GLcharARB)*length); + int chars=0; + + glGetInfoLogARB(pr, length, &chars, logger); + if(chars>0) + std::cout << (logger) << std::endl; + + if(logger) + free(logger); + } +#endif//GL_ARB_shader_objects +} + +char *BL_Shader::GetVertPtr() +{ + return vertProg?vertProg:0; +} + +char *BL_Shader::GetFragPtr() +{ + return fragProg?fragProg:0; +} + +void BL_Shader::SetVertPtr( char *vert ) +{ + vertProg = vert; +} + +void BL_Shader::SetFragPtr( char *frag ) +{ + fragProg = frag; +} + +unsigned int BL_Shader::GetProg() +{ + return mShader; +} + +unsigned int BL_Shader::GetVertexShader() +{ + return mVert; +} + +unsigned int BL_Shader::GetFragmentShader() +{ + return mFrag; +} + +const uSampler* BL_Shader::getSampler(int i) +{ + MT_assert(i<=MAXTEX); + return &mSampler[i]; +} + +const uBlending *BL_Shader::getBlending( int pass ) +{ + return &mBlending; +} + + + +void BL_Shader::InitializeSampler( + int type, + int unit, + int pass, + unsigned int texture) +{ + MT_assert(unit<=MAXTEX); + mSampler[unit].glTexture = texture; + mSampler[unit].loc =-1; + mSampler[unit].pass=0; + mSampler[unit].type=type; + mSampler[unit].unit=unit; +} + +PyObject* BL_Shader::_getattr(const STR_String& attr) +{ + _getattr_up(PyObjectPlus); +} + + +PyMethodDef BL_Shader::Methods[] = +{ + // creation + KX_PYMETHODTABLE( BL_Shader, setSource ), + KX_PYMETHODTABLE( BL_Shader, delSource ), + KX_PYMETHODTABLE( BL_Shader, getVertexProg ), + KX_PYMETHODTABLE( BL_Shader, getFragmentProg ), + KX_PYMETHODTABLE( BL_Shader, setNumberOfPasses ), + KX_PYMETHODTABLE( BL_Shader, validate), + /// access functions + KX_PYMETHODTABLE( BL_Shader, isValid), + KX_PYMETHODTABLE( BL_Shader, setUniform1f ), + KX_PYMETHODTABLE( BL_Shader, setUniform2f ), + KX_PYMETHODTABLE( BL_Shader, setUniform3f ), + KX_PYMETHODTABLE( BL_Shader, setUniform4f ), + KX_PYMETHODTABLE( BL_Shader, setUniform1i ), + KX_PYMETHODTABLE( BL_Shader, setUniform2i ), + KX_PYMETHODTABLE( BL_Shader, setUniform3i ), + KX_PYMETHODTABLE( BL_Shader, setUniform4i ), + + KX_PYMETHODTABLE( BL_Shader, setUniformfv ), + KX_PYMETHODTABLE( BL_Shader, setUniformiv ), + + KX_PYMETHODTABLE( BL_Shader, setSampler ), + KX_PYMETHODTABLE( BL_Shader, setUniformMatrix4 ), + KX_PYMETHODTABLE( BL_Shader, setUniformMatrix3 ), + // KX_PYMETHODTABLE( BL_Shader, setBlending ), + + {NULL,NULL} //Sentinel +}; + + +PyTypeObject BL_Shader::Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, + "BL_Shader", + sizeof(BL_Shader), + 0, + PyDestructor, + 0, + __getattr, + __setattr, + 0, + __repr, + 0 +}; + + +PyParentObject BL_Shader::Parents[] = { + &PyObjectPlus::Type, + &BL_Shader::Type, + NULL +}; + + +KX_PYMETHODDEF_DOC( BL_Shader, setSource," setSource(vertexProgram, fragmentProgram)" ) +{ +#ifdef GL_ARB_shader_objects + if(mShader !=0 && mOk ) + { + // already set... + Py_Return; + } + + char *v,*f; + int apply=0; + if( PyArg_ParseTuple(args, "ssi", &v, &f, &apply) ) + { + vertProg = v; + fragProg = f; + if( LinkProgram() ) + { + glUseProgramObjectARB( mShader ); + mUse = apply!=0; + Py_Return; + } + else + { + vertProg = 0; + fragProg = 0; + mUse = 0; + glUseProgramObjectARB( 0 ); + } + } + return NULL; +#else + Py_Return; +#endif +} + + +KX_PYMETHODDEF_DOC( BL_Shader, delSource, "delSource( )" ) +{ +#ifdef GL_ARB_shader_objects + glDeleteObjectARB(mShader); + glDeleteObjectARB(mFrag); + glDeleteObjectARB(mVert); + mShader = 0; + mFrag = 0; + mVert = 0; + vertProg = 0; + fragProg = 0; + mOk = 0; + mUse = 0; + glUseProgramObjectARB(0); +#endif + Py_Return; + +} + + +KX_PYMETHODDEF_DOC( BL_Shader, isValid, "isValid()" ) +{ + return PyInt_FromLong( ( mShader !=0 && mOk ) ); +} + +KX_PYMETHODDEF_DOC( BL_Shader, getVertexProg ,"getVertexProg( )" ) +{ + return PyString_FromString(vertProg?vertProg:""); +} + +KX_PYMETHODDEF_DOC( BL_Shader, getFragmentProg ,"getFragmentProg( )" ) +{ + return PyString_FromString(fragProg?fragProg:""); +} + + +KX_PYMETHODDEF_DOC( BL_Shader, validate, "validate()") +{ +#ifdef GL_ARB_shader_objects + if(mShader==0) + { + PyErr_Format(PyExc_TypeError, "invalid shader object"); + return NULL; + } + + int stat = 0; + glValidateProgramARB(mShader); + glGetObjectParameterivARB(mShader, GL_OBJECT_VALIDATE_STATUS_ARB, &stat); + printInfo(mShader); + + return PyInt_FromLong((stat!=0)); +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setSampler, "setSampler(name, index)" ) +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + int index=-1; + if(PyArg_ParseTuple(args, "si", &uniform, &index)) + { + if(mShader==0) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader, uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + if(index <= MAXTEX) + { + mSampler[index].loc = loc; + }else + { + spit("Invalid texture sample index: " << index); + } + Py_Return; + } + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + +KX_PYMETHODDEF_DOC( BL_Shader, setNumberOfPasses, "setNumberOfPasses( max-pass )" ) +{ + int pass = 1; + if(!PyArg_ParseTuple(args, "i", &pass)) + return NULL; + + mPass = pass; + Py_Return; +} + +/// access functions +KX_PYMETHODDEF_DOC( BL_Shader, setUniform1f, "setUniform1f(name, fx)" ) +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + float value=0; + if(PyArg_ParseTuple(args, "sf", &uniform, &value )) + { + if( mShader==0 ) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader, uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB( mShader ); + glUniform1fARB( loc, value ); + Py_Return; + } + + } + return NULL; +#else + Py_Return; +#endif +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform2f , "setUniform2f(name, fx, fy)") +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + float array[2]={ 0,0 }; + if(PyArg_ParseTuple(args, "sff", &uniform, &array[0],&array[1] )) + { + if( mShader==0 ) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB( mShader ); + glUniform2fARB(loc, array[0],array[1] ); + Py_Return; + } + + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform3f, "setUniform3f(name, fx,fy,fz) ") +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + float array[3]={0,0,0}; + if(PyArg_ParseTuple(args, "sfff", &uniform, &array[0],&array[1],&array[2])) + { + if(mShader==0) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB(mShader); + glUniform3fARB(loc, array[0],array[1],array[2]); + Py_Return; + } + + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform4f, "setUniform4f(name, fx,fy,fz, fw) ") +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + float array[4]={0,0,0,0}; + if(PyArg_ParseTuple(args, "sffff", &uniform, &array[0],&array[1],&array[2], &array[3])) + { + if(mShader==0) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB(mShader); + glUniform4fARB(loc, array[0],array[1],array[2], array[3]); + Py_Return; + } + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform1i, "setUniform1i(name, ix)" ) +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + int value=0; + if(PyArg_ParseTuple(args, "si", &uniform, &value )) + { + if( mShader==0 ) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader, uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB( mShader ); + glUniform1iARB( loc, value ); + Py_Return; + } + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform2i , "setUniform2i(name, ix, iy)") +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + int array[2]={ 0,0 }; + if(PyArg_ParseTuple(args, "sii", &uniform, &array[0],&array[1] )) + { + if( mShader==0 ) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB( mShader ); + glUniform2iARB(loc, array[0],array[1] ); + Py_Return; + } + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform3i, "setUniform3i(name, ix,iy,iz) ") +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + int array[3]={0,0,0}; + if(PyArg_ParseTuple(args, "siii", &uniform, &array[0],&array[1],&array[2])) + { + if(mShader==0) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB(mShader); + glUniform3iARB(loc, array[0],array[1],array[2]); + Py_Return; + } + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + +KX_PYMETHODDEF_DOC( BL_Shader, setUniform4i, "setUniform4i(name, ix,iy,iz, iw) ") +{ +#ifdef GL_ARB_shader_objects + char *uniform=""; + int array[4]={0,0,0, 0}; + if(PyArg_ParseTuple(args, "siiii", &uniform, &array[0],&array[1],&array[2], &array[3] )) + { + if(mShader==0) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + glUseProgramObjectARB(mShader); + glUniform4iARB(loc, array[0],array[1],array[2], array[3]); + Py_Return; + } + } + return NULL; +#else + Py_Return; +#endif//GL_ARB_shader_objects +} + +KX_PYMETHODDEF_DOC( BL_Shader, setUniformfv , "setUniformfv( float (list2 or list3 or list4) )") +{ +#ifdef GL_ARB_shader_objects + char*uniform = ""; + PyObject *listPtr =0; + float array_data[4] = {0.f,0.f,0.f,0.f}; + + if(PyArg_ParseTuple(args, "sO", &uniform, &listPtr)) + { + if(mShader==0) + { + PyErr_Format(PyExc_ValueError, "invalid shader object"); + return NULL; + } + int loc= glGetUniformLocationARB(mShader , uniform); + if( loc==-1 ) + { + spit("Invalid uniform value: " << uniform << "."); + Py_Return; + }else + { + if(PySequence_Check(listPtr)) + { + unsigned int list_size = PySequence_Size(listPtr); + + for(unsigned int i=0; (i +#endif // WIN32 +#ifdef __APPLE__ +#include +#include +#else +#include +#include +#endif + +#include + +#include "BL_Material.h" +#include "BL_Texture.h" +#include "MT_assert.h" + +#include "DNA_texture_types.h" +#include "DNA_image_types.h" +#include "IMB_imbuf_types.h" +#include "BKE_image.h" +//#include "IMB_imbuf.h" +#include "BLI_blenlib.h" + +#include "RAS_GLExtensionManager.h" +using namespace bgl; + +#define spit(x) std::cout << x << std::endl; + +#include "MEM_guardedalloc.h" + + + +extern "C" { + // envmaps + #include "IMB_imbuf.h" + void my_envmap_split_ima(EnvMap *env); + void my_free_envmapdata(EnvMap *env); +} + +// (n&(n-1)) zeros the least significant bit of n +static int is_pow2(int num) { + return ((num)&(num-1))==0; +} +static int smaller_pow2(int num) { + while (!is_pow2(num)) + num= num&(num-1); + return num; +} + + + +BL_Texture::BL_Texture() +: mTexture(0), + mError(0), + mOk(0), + mNeedsDeleted(0), + mType(0), + mName("") +{ + // -- +} + +BL_Texture::~BL_Texture() +{ + // -- +} + +void BL_Texture::DeleteTex() +{ + if( mNeedsDeleted ) { + glDeleteTextures(1, (GLuint*)&(*mTexture)); + mNeedsDeleted = 0; + mOk = 0; + } +} + + +bool BL_Texture::InitFromImage( Image *img, bool mipmap) +{ + if(!img || img->ok==0 ) { + mError = true; + mOk = false; + return mOk; + } + if( img->ibuf==0 ) { + load_image(img, IB_rect, "", 0); + if(img->ibuf==0) { + img->ok = 0; + mError = true; + mOk = false; + return mOk; + } + } + mTexture = &img->bindcode; + mName = img->id.name; + mType = BL_TEX2D; + + // smoke em if we got em + if (*mTexture != 0) { + glBindTexture(GL_TEXTURE_2D, *mTexture ); + Validate(); + return mOk; + } + glGenTextures(1, (GLuint*)mTexture); + InitGLTex(img->ibuf->rect, img->ibuf->x, img->ibuf->y, mipmap); + Validate(); + return mOk; +} + +void BL_Texture::InitGLTex(unsigned int *pix,int x,int y,bool mipmap) +{ + if (!is_pow2(x) || !is_pow2(y) ) { + InitNonPow2Tex(pix, x,y,mipmap); + return; + } + + glBindTexture(GL_TEXTURE_2D, *mTexture ); + if( mipmap ) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, x, y, GL_RGBA, GL_UNSIGNED_BYTE, pix ); + } + else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix ); + } + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); +} + + +void BL_Texture::InitNonPow2Tex(unsigned int *pix,int x,int y,bool mipmap) +{ + int nx= smaller_pow2(x); + int ny= smaller_pow2(y); + + unsigned int *newPixels = (unsigned int *)malloc(nx*ny*sizeof(unsigned int)); + + gluScaleImage(GL_RGBA, x, y, GL_UNSIGNED_BYTE, pix, nx,ny, GL_UNSIGNED_BYTE, newPixels); + glBindTexture(GL_TEXTURE_2D, *mTexture ); + + if( mipmap ) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, nx, ny, GL_RGBA, GL_UNSIGNED_BYTE, newPixels ); + } + else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, nx, ny, 0, GL_RGBA, GL_UNSIGNED_BYTE, newPixels ); + } + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + free(newPixels); +} + + +bool BL_Texture::InitCubeMap( EnvMap *cubemap ) +{ +#ifdef GL_ARB_texture_cube_map + if(!RAS_EXT_support._ARB_texture_cube_map) { + spit("cubemaps not supported"); + mError = true; + mOk = false; + return mOk; + } + + else if(!cubemap || cubemap->ima->ok==0 ) { + mError = true; + mOk = false; + return mOk; + } + + if( cubemap->ima->ibuf==0 ) { + load_image(cubemap->ima, IB_rect, "", 0); + if(cubemap->ima->ibuf==0) { + cubemap->ima->ok = 0; + mError = true; + mOk = false; + return mOk; + } + } + + EnvMap *CubeMap = cubemap; + mNeedsDeleted = 1; + mBlankTexture = 0; + mType = BL_TEXCUBE; + mTexture = &mBlankTexture; + mName = CubeMap->ima->id.name; + + glGenTextures(1, (GLuint*)(mTexture)); + glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, *mTexture ); + bool needs_split = false; + + if(!CubeMap->cube[0]) needs_split = true; + + if(needs_split){ + // split it + my_envmap_split_ima(CubeMap); + } + + int x = cubemap->ima->ibuf->x; + int y = cubemap->ima->ibuf->y; + unsigned int *data= (unsigned int *)malloc(x*y*sizeof(unsigned int)); + + // ----------------------------------- + x = CubeMap->cube[0]->ibuf->x; + y = CubeMap->cube[0]->ibuf->y; + + // check the first image, and assume the rest + if (!is_pow2(x) || !is_pow2(y)) + { + spit("invalid envmap size please render with CubeRes @ power of two"); + free(data); + data = 0; + mError = true; + mOk = false; + return mOk; + } + memcpy(data, CubeMap->cube[0]->ibuf->rect, (x*y*sizeof(unsigned int))); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + + // ----------------------------------- + x = CubeMap->cube[1]->ibuf->x; + y = CubeMap->cube[1]->ibuf->y; + memcpy(data, CubeMap->cube[1]->ibuf->rect, (x*y*sizeof(unsigned int))); + glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + + // ----------------------------------- + x = CubeMap->cube[2]->ibuf->x; + y = CubeMap->cube[2]->ibuf->y; + memcpy(data, CubeMap->cube[2]->ibuf->rect, (x*y*sizeof(unsigned int))); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + + // ----------------------------------- + x = CubeMap->cube[3]->ibuf->x; + y = CubeMap->cube[3]->ibuf->y; + memcpy(data, CubeMap->cube[3]->ibuf->rect, (x*y*sizeof(unsigned int))); + glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + + // ----------------------------------- + x = CubeMap->cube[4]->ibuf->x; + y = CubeMap->cube[4]->ibuf->y; + memcpy(data, CubeMap->cube[4]->ibuf->rect, (x*y*sizeof(unsigned int))); + glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + + // ----------------------------------- + x = CubeMap->cube[5]->ibuf->x; + y = CubeMap->cube[5]->ibuf->y; + memcpy(data, CubeMap->cube[5]->ibuf->rect, (x*y*sizeof(unsigned int))); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + + glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_REPEAT ); + glTexParameteri( GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_REPEAT ); + + if(data) { + free(data); + data = 0; + } + + if(needs_split) { + // okay we allocated, swap back to orig and free used + cubemap->ima = CubeMap->ima; + my_free_envmapdata(CubeMap); + } + mOk = IsValid(); + return mOk; + +#else + + mError = true; + mOk = false; + return mOk; + +#endif//GL_ARB_texture_cube_map +} + + +STR_String BL_Texture::GetName() const +{ + return mName; +} + + +bool BL_Texture::IsValid() +{ + return (mTexture && *mTexture!= 0)?glIsTexture(*mTexture)!=0:false; +} + + +void BL_Texture::Validate() +{ + mOk = IsValid(); +} + + +bool BL_Texture::Ok() +{ + return ( mTexture?((!mError || mOk ) && *mTexture!= 0):0 ); +} + + +unsigned int BL_Texture::GetTextureType() const +{ + return mType; +} + + +BL_Texture::operator const unsigned int () const +{ + return mTexture? *mTexture:0; +} + +bool BL_Texture::SetGLTex(unsigned int tex) +{ + return false; +} + +extern "C" { + +void my_envmap_split_ima(EnvMap *env) +{ + ImBuf *ibuf; + Image *ima; + int dx, part; + + my_free_envmapdata(env); + + dx= env->ima->ibuf->y; + dx/= 2; + if(3*dx != env->ima->ibuf->x) { + printf("Incorrect envmap size\n"); + env->ok= 0; + env->ima->ok= 0; + } + else { + for(part=0; part<6; part++) { + ibuf= IMB_allocImBuf(dx, dx, 24, IB_rect, 0); + ima= (Image*)MEM_callocN(sizeof(Image), "image"); + ima->ibuf= ibuf; + ima->ok= 1; + env->cube[part]= ima; + } + IMB_rectop(env->cube[0]->ibuf, env->ima->ibuf, + 0, 0, 0, 0, dx, dx, IMB_rectcpy, 0); + IMB_rectop(env->cube[1]->ibuf, env->ima->ibuf, + 0, 0, dx, 0, dx, dx, IMB_rectcpy, 0); + IMB_rectop(env->cube[2]->ibuf, env->ima->ibuf, + 0, 0, 2*dx, 0, dx, dx, IMB_rectcpy, 0); + IMB_rectop(env->cube[3]->ibuf, env->ima->ibuf, + 0, 0, 0, dx, dx, dx, IMB_rectcpy, 0); + IMB_rectop(env->cube[4]->ibuf, env->ima->ibuf, + 0, 0, dx, dx, dx, dx, IMB_rectcpy, 0); + IMB_rectop(env->cube[5]->ibuf, env->ima->ibuf, + 0, 0, 2*dx, dx, dx, dx, IMB_rectcpy, 0); + env->ok= 2; + } +} + + +void my_free_envmapdata(EnvMap *env) +{ + Image *ima; + unsigned int a, part; + + for(part=0; part<6; part++) { + ima= env->cube[part]; + if(ima) { + if(ima->ibuf) IMB_freeImBuf(ima->ibuf); + + for(a=0; amipmap); a++) { + if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]); + } + MEM_freeN(ima); + env->cube[part]= 0; + } + } + env->ok= 0; +} + + +} + +unsigned int BL_Texture::mBlankTexture = 0; + diff --git a/source/gameengine/Ketsji/BL_Texture.h b/source/gameengine/Ketsji/BL_Texture.h new file mode 100644 index 00000000000..51bf7270d2d --- /dev/null +++ b/source/gameengine/Ketsji/BL_Texture.h @@ -0,0 +1,53 @@ +#ifndef __BL_TEXTURE_H__ +#define __BL_TEXTURE_H__ +#include +// -- +struct Image; +struct EnvMap; +// -- +#include "STR_String.h" + +class BL_Texture +{ +private: + // ----------------------------------- + unsigned int* mTexture; + bool mError; + bool mOk; + bool mNeedsDeleted; + unsigned int mType; + STR_String mName; + static unsigned int mBlankTexture; + std::vectormCubeMem; + // ----------------------------------- + void InitNonPow2Tex(unsigned int *p,int x,int y,bool mipmap ); + void InitGLTex(unsigned int *p,int x,int y,bool mipmap ); + +public: + BL_Texture(); + ~BL_Texture( ); + + operator const unsigned int () const; + bool Ok(); + + STR_String GetName() const; + + unsigned int GetTextureType() const; + void DeleteTex(); + bool InitFromImage( Image *img, bool mipmap); + bool InitCubeMap( EnvMap *cubemap ); + // + bool SetGLTex(unsigned int tex); + + void PopCubeMap(); + bool IsValid(); + void Validate(); +}; + +enum TexType{ + BL_TEX2D = 1, + BL_TEXCUBE = 2 +}; + + +#endif//__BL_TEXTURE_H__ diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index 924bb307da1..e2a11c0035e 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -71,7 +71,7 @@ #include "KX_Scene.h" #include "SND_DeviceManager.h" -#include "RAS_OpenGLRasterizer\RAS_GLExtensionManager.h" +#include "RAS_OpenGLRasterizer/RAS_GLExtensionManager.h" #include "KX_PyMath.h" diff --git a/source/gameengine/Ketsji/SConscript b/source/gameengine/Ketsji/SConscript index 39a74083ff4..1dc5f0f7170 100644 --- a/source/gameengine/Ketsji/SConscript +++ b/source/gameengine/Ketsji/SConscript @@ -6,7 +6,10 @@ Import ('library_env') ketsji_env = library_env.Copy () -source_files = ['KX_WorldIpoController.cpp', +source_files = ['BL_Shader.cpp', + 'BL_Material.cpp', + 'BL_Texture.cpp', + 'KX_WorldIpoController.cpp', 'KX_WorldInfo.cpp', 'KX_VisibilityActuator.cpp', 'KX_VertexProxy.cpp', -- cgit v1.2.3