2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[blender.git] / source / gameengine / Ketsji / BL_Shader.cpp
index 60cb288..88d9200 100644 (file)
@@ -729,9 +729,9 @@ void BL_Shader::SetUniform(int uniform, const int* val, int len)
 }
 
 
-PyObject* BL_Shader::_getattr(const char *attr)
+PyObject* BL_Shader::py_getattro(PyObject *attr)
 {
-       _getattr_up(PyObjectPlus);
+       py_getattro_up(PyObjectPlus);
 }
 
 
@@ -772,24 +772,28 @@ PyAttributeDef BL_Shader::Attributes[] = {
 };
 
 PyTypeObject BL_Shader::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
+       PyObject_HEAD_INIT(NULL)
                0,
                "BL_Shader",
-               sizeof(BL_Shader),
+               sizeof(PyObjectPlus_Proxy),
                0,
-               PyDestructor,
+               py_base_dealloc,
                0,
-               __getattr,
-               __setattr,
                0,
-               __repr,
-               0
+               0,
+               0,
+               py_base_repr,
+               0,0,0,0,0,0,
+               py_base_getattro,
+               py_base_setattro,
+               0,0,0,0,0,0,0,0,0,
+               Methods
 };
 
 
 PyParentObject BL_Shader::Parents[] = {
-       &PyObjectPlus::Type,
        &BL_Shader::Type,
+       &PyObjectPlus::Type,
        NULL
 };
 
@@ -803,7 +807,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setSource," setSource(vertexProgram, fragmentProg
        }
        char *v,*f;
        int apply=0;
-       if( PyArg_ParseTuple(args, "ssi", &v, &f, &apply) )
+       if( PyArg_ParseTuple(args, "ssi:setSource", &v, &f, &apply) )
        {
                vertProg = v;
                fragProg = f;
@@ -854,7 +858,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, validate, "validate()")
                Py_RETURN_NONE;
        }
        if(mShader==0) {
-               PyErr_Format(PyExc_TypeError, "invalid shader object");
+               PyErr_SetString(PyExc_TypeError, "shader.validate(): BL_Shader, invalid shader object");
                return NULL;
        }
        int stat = 0;
@@ -886,7 +890,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setSampler, "setSampler(name, index)" )
 
        const char *uniform="";
        int index=-1;
-       if(PyArg_ParseTuple(args, "si", &uniform, &index)) 
+       if(PyArg_ParseTuple(args, "si:setSampler", &uniform, &index)) 
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1) {
@@ -911,7 +915,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setSampler, "setSampler(name, index)" )
 KX_PYMETHODDEF_DOC( BL_Shader, setNumberOfPasses, "setNumberOfPasses( max-pass )" )
 {
        int pass = 1;
-       if(!PyArg_ParseTuple(args, "i", &pass))
+       if(!PyArg_ParseTuple(args, "i:setNumberOfPasses", &pass))
                return NULL;
 
        mPass = 1;
@@ -927,7 +931,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform1f, "setUniform1f(name, fx)" )
 
        const char *uniform="";
        float value=0;
-       if(PyArg_ParseTuple(args, "sf", &uniform, &value ))
+       if(PyArg_ParseTuple(args, "sf:setUniform1f", &uniform, &value ))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -951,7 +955,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform2f , "setUniform2f(name, fx, fy)")
        }
        const char *uniform="";
        float array[2]={ 0,0 };
-       if(PyArg_ParseTuple(args, "sff", &uniform, &array[0],&array[1] ))
+       if(PyArg_ParseTuple(args, "sff:setUniform2f", &uniform, &array[0],&array[1] ))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -975,7 +979,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform3f, "setUniform3f(name, fx,fy,fz) ")
        }
        const char *uniform="";
        float array[3]={0,0,0};
-       if(PyArg_ParseTuple(args, "sfff", &uniform, &array[0],&array[1],&array[2]))
+       if(PyArg_ParseTuple(args, "sfff:setUniform3f", &uniform, &array[0],&array[1],&array[2]))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1000,7 +1004,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform4f, "setUniform4f(name, fx,fy,fz, fw) "
        }
        const char *uniform="";
        float array[4]={0,0,0,0};
-       if(PyArg_ParseTuple(args, "sffff", &uniform, &array[0],&array[1],&array[2], &array[3]))
+       if(PyArg_ParseTuple(args, "sffff:setUniform4f", &uniform, &array[0],&array[1],&array[2], &array[3]))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1024,7 +1028,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform1i, "setUniform1i(name, ix)" )
        }
        const char *uniform="";
        int value=0;
-       if(PyArg_ParseTuple(args, "si", &uniform, &value ))
+       if(PyArg_ParseTuple(args, "si:setUniform1i", &uniform, &value ))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1048,7 +1052,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform2i , "setUniform2i(name, ix, iy)")
        }
        const char *uniform="";
        int array[2]={ 0,0 };
-       if(PyArg_ParseTuple(args, "sii", &uniform, &array[0],&array[1] ))
+       if(PyArg_ParseTuple(args, "sii:setUniform2i", &uniform, &array[0],&array[1] ))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1073,7 +1077,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform3i, "setUniform3i(name, ix,iy,iz) ")
 
        const char *uniform="";
        int array[3]={0,0,0};
-       if(PyArg_ParseTuple(args, "siii", &uniform, &array[0],&array[1],&array[2]))
+       if(PyArg_ParseTuple(args, "siii:setUniform3i", &uniform, &array[0],&array[1],&array[2]))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1096,7 +1100,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniform4i, "setUniform4i(name, ix,iy,iz, iw) "
        }
        const char *uniform="";
        int array[4]={0,0,0, 0};
-       if(PyArg_ParseTuple(args, "siiii", &uniform, &array[0],&array[1],&array[2], &array[3] ))
+       if(PyArg_ParseTuple(args, "siiii:setUniform4i", &uniform, &array[0],&array[1],&array[2], &array[3] ))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1121,7 +1125,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformfv , "setUniformfv( float (list2 or lis
        PyObject *listPtr =0;
        float array_data[4] = {0.f,0.f,0.f,0.f};
 
-       if(PyArg_ParseTuple(args, "sO", &uniform, &listPtr))
+       if(PyArg_ParseTuple(args, "sO:setUniformfv", &uniform, &listPtr))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)
@@ -1171,7 +1175,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformfv , "setUniformfv( float (list2 or lis
                                        }break;
                                default:
                                        {
-                                               PyErr_Format(PyExc_TypeError, "Invalid list size");
+                                               PyErr_SetString(PyExc_TypeError, "shader.setUniform4i(name, ix,iy,iz, iw): BL_Shader. invalid list size");
                                                return NULL;
                                        }break;
                                }
@@ -1181,7 +1185,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformfv , "setUniformfv( float (list2 or lis
        return NULL;
 }
 
-KX_PYMETHODDEF_DOC( BL_Shader, setUniformiv, "setUniformiv( int (list2 or list3 or list4) )")
+KX_PYMETHODDEF_DOC( BL_Shader, setUniformiv, "setUniformiv( uniform_name, (list2 or list3 or list4) )")
 {
        if(mError) {
                Py_RETURN_NONE;
@@ -1190,70 +1194,84 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformiv, "setUniformiv( int (list2 or list3
        PyObject *listPtr =0;
        int array_data[4] = {0,0,0,0};
 
-       if(PyArg_ParseTuple(args, "sO", &uniform, &listPtr))
+       if(!PyArg_ParseTuple(args, "sO:setUniformiv", &uniform, &listPtr))
+               return NULL;
+       
+       int loc = GetUniformLocation(uniform);
+       
+       if(loc == -1) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformiv(...): BL_Shader, first string argument is not a valid uniform value");
+               return NULL;
+       }
+       
+       if(!PySequence_Check(listPtr)) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformiv(...): BL_Shader, second argument is not a sequence");
+               return NULL;
+       }
+       
+       unsigned int list_size = PySequence_Size(listPtr);
+       
+       for(unsigned int i=0; (i<list_size && i<4); i++)
        {
-               int loc = GetUniformLocation(uniform);
-               if(loc != -1)
+               PyObject *item = PySequence_GetItem(listPtr, i);
+               array_data[i] = PyInt_AsLong(item);
+               Py_DECREF(item);
+       }
+       
+       if(PyErr_Occurred()) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformiv(...): BL_Shader, one or more values in the list is not an int");
+               return NULL;
+       }
+       
+       /* Sanity checks done! */
+       
+       switch(list_size)
+       {
+       case 2:
                {
-                       if(PySequence_Check(listPtr))
-                       {
-                               unsigned int list_size = PySequence_Size(listPtr);
-                               
-                               for(unsigned int i=0; (i<list_size && i<4); i++)
-                               {
-                                       PyObject *item = PySequence_GetItem(listPtr, i);
-                                       array_data[i] = PyInt_AsLong(item);
-                                       Py_DECREF(item);
-                               }
-                               switch(list_size)
-                               {
-                               case 2:
-                                       {
-                                               int array2[2] = { array_data[0],array_data[1]};
+                       int array2[2] = { array_data[0],array_data[1]};
 #ifdef SORT_UNIFORMS
-                                               SetUniformiv(loc, BL_Uniform::UNI_INT2, array2, sizeof(int)*2);
+                       SetUniformiv(loc, BL_Uniform::UNI_INT2, array2, sizeof(int)*2);
 #else
-                                               SetUniform(loc, array2, 2);                                             
+                       SetUniform(loc, array2, 2);                                             
 #endif
-                                               Py_RETURN_NONE;
-                                       } break;
-                               case 3:
-                                       {
-                                               int array3[3] = { array_data[0],array_data[1],array_data[2] };
+                       Py_RETURN_NONE;
+               } break;
+       case 3:
+               {
+                       int array3[3] = { array_data[0],array_data[1],array_data[2] };
 #ifdef SORT_UNIFORMS
-                                               SetUniformiv(loc, BL_Uniform::UNI_INT3, array3, sizeof(int)*3);
-                                               
+                       SetUniformiv(loc, BL_Uniform::UNI_INT3, array3, sizeof(int)*3);
+                       
 #else
-                                               SetUniform(loc, array3, 3);     
+                       SetUniform(loc, array3, 3);     
 #endif
-                                               Py_RETURN_NONE;
-                                       }break;
-                               case 4:
-                                       {
-                                               int array4[4] = { array_data[0],array_data[1],array_data[2],array_data[3] };
+                       Py_RETURN_NONE;
+               }break;
+       case 4:
+               {
+                       int array4[4] = { array_data[0],array_data[1],array_data[2],array_data[3] };
 #ifdef SORT_UNIFORMS
-                                               SetUniformiv(loc, BL_Uniform::UNI_INT4, array4, sizeof(int)*4);
-                                               
+                       SetUniformiv(loc, BL_Uniform::UNI_INT4, array4, sizeof(int)*4);
+                       
 #else
-                                               SetUniform(loc, array4, 4);     
+                       SetUniform(loc, array4, 4);     
 #endif
-                                               Py_RETURN_NONE;
-                                       }break;
-                               default:
-                                       {
-                                               PyErr_Format(PyExc_TypeError, "Invalid list size");
-                                               return NULL;
-                                       }break;
-                               }
-                       }
-               }
+                       Py_RETURN_NONE;
+               }break;
+       default:
+               {
+                       PyErr_SetString(PyExc_TypeError, "shader.setUniformiv(...): BL_Shader, second argument, invalid list size, expected an int list between 2 and 4");
+                       return NULL;
+               }break;
        }
-       return NULL;
+       
+       Py_RETURN_NONE;
 }
 
 
 KX_PYMETHODDEF_DOC( BL_Shader, setUniformMatrix4, 
-"setUniformMatrix4(uniform-name, mat-4x4, transpose(row-major=true, col-major=false)" )
+"setUniformMatrix4(uniform_name, mat-4x4, transpose(row-major=true, col-major=false)" )
 {
        if(mError) {
                Py_RETURN_NONE;
@@ -1269,33 +1287,38 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformMatrix4,
        const char *uniform="";
        PyObject *matrix=0;
        int transp=1; // MT_ is row major so transpose by default....
-       if(PyArg_ParseTuple(args, "sO|i",&uniform, &matrix,&transp))
-       {
-               int loc = GetUniformLocation(uniform);
-               if(loc != -1)
-               {
-                       if (PyObject_IsMT_Matrix(matrix, 4))
-                       {
-                               MT_Matrix4x4 mat;
-                               if (PyMatTo(matrix, mat))
-                               {
+       
+       if(!PyArg_ParseTuple(args, "sO|i:setUniformMatrix4",&uniform, &matrix,&transp))
+               return NULL;
+
+       int loc = GetUniformLocation(uniform);
+       
+       if(loc == -1) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformMatrix4(...): BL_Shader, first string argument is not a valid uniform value");
+               return NULL;
+       }
+       
+       MT_Matrix4x4 mat;
+       
+       if (!PyMatTo(matrix, mat)) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformMatrix4(...): BL_Shader, second argument cannot be converted into a 4x4 matrix");
+               return NULL;
+       }
+       
+       /* Sanity checks done! */
+
 #ifdef SORT_UNIFORMS
-                                       mat.getValue(matr);
-                                       SetUniformfv(loc, BL_Uniform::UNI_MAT4, matr, (sizeof(float)*16), (transp!=0) );
+       mat.getValue(matr);
+       SetUniformfv(loc, BL_Uniform::UNI_MAT4, matr, (sizeof(float)*16), (transp!=0) );
 #else
-                                       SetUniform(loc,mat,(transp!=0));
+       SetUniform(loc,mat,(transp!=0));
 #endif
-                                       Py_RETURN_NONE;
-                               }
-                       }
-               }
-       }
-       return NULL;
+       Py_RETURN_NONE;
 }
 
 
 KX_PYMETHODDEF_DOC( BL_Shader, setUniformMatrix3,
-"setUniformMatrix3(uniform-name, list[3x3], transpose(row-major=true, col-major=false)" )
+"setUniformMatrix3(uniform_name, list[3x3], transpose(row-major=true, col-major=false)" )
 {
        if(mError) {
                Py_RETURN_NONE;
@@ -1310,29 +1333,32 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformMatrix3,
        const char *uniform="";
        PyObject *matrix=0;
        int transp=1; // MT_ is row major so transpose by default....
-       if(PyArg_ParseTuple(args, "sO|i",&uniform, &matrix,&transp))
-       {
-               int loc = GetUniformLocation(uniform);
-               if(loc != -1)
-               {
-                       if (PyObject_IsMT_Matrix(matrix, 3))
-                       {
-                               MT_Matrix3x3 mat;
-                               if (PyMatTo(matrix, mat))
-                               {
+       if(!PyArg_ParseTuple(args, "sO|i:setUniformMatrix3",&uniform, &matrix,&transp))
+               return NULL;
+       
+       int loc = GetUniformLocation(uniform);
+       
+       if(loc == -1) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformMatrix3(...): BL_Shader, first string argument is not a valid uniform value");
+               return NULL;
+       }
+       
+       
+       MT_Matrix3x3 mat;
+       
+       if (!PyMatTo(matrix, mat)) {
+               PyErr_SetString(PyExc_TypeError, "shader.setUniformMatrix3(...): BL_Shader, second argument cannot be converted into a 3x3 matrix");
+               return NULL;
+       }
+       
+
 #ifdef SORT_UNIFORMS
-                                       mat.getValue(matr);
-                                       SetUniformfv(loc, BL_Uniform::UNI_MAT3, matr, (sizeof(float)*9), (transp!=0) );
+       mat.getValue(matr);
+       SetUniformfv(loc, BL_Uniform::UNI_MAT3, matr, (sizeof(float)*9), (transp!=0) );
 #else
-                                       SetUniform(loc,mat,(transp!=0));
+       SetUniform(loc,mat,(transp!=0));
 #endif
-                                       Py_RETURN_NONE;
-
-                               }
-                       }
-               }
-       }
-       return NULL;
+       Py_RETURN_NONE;
 }
 
 KX_PYMETHODDEF_DOC( BL_Shader, setAttrib, "setAttrib(enum)" )
@@ -1340,18 +1366,20 @@ KX_PYMETHODDEF_DOC( BL_Shader, setAttrib, "setAttrib(enum)" )
        if(mError) {
                Py_RETURN_NONE;
        }
+       
        int attr=0;
-       if(PyArg_ParseTuple(args, "i", &attr )) {
-               if(mShader==0) {
-                       PyErr_Format(PyExc_ValueError, "invalid shader object");
-                       return NULL;
-               }
-               mAttr=SHD_TANGENT;
-               glUseProgramObjectARB(mShader);
-               glBindAttribLocationARB(mShader, mAttr, "Tangent");
-               Py_RETURN_NONE;
+       
+       if(!PyArg_ParseTuple(args, "i:setAttrib", &attr ))
+               return NULL;
+       
+       if(mShader==0) {
+               PyErr_SetString(PyExc_ValueError, "shader.setAttrib() BL_Shader, invalid shader object");
+               return NULL;
        }
-       return NULL;
+       mAttr=SHD_TANGENT; /* What the heck is going on here - attr is just ignored??? - Campbell */
+       glUseProgramObjectARB(mShader);
+       glBindAttribLocationARB(mShader, mAttr, "Tangent");
+       Py_RETURN_NONE;
 }
 
 
@@ -1363,7 +1391,7 @@ KX_PYMETHODDEF_DOC( BL_Shader, setUniformDef, "setUniformDef(name, enum)" )
 
        const char *uniform="";
        int nloc=0;
-       if(PyArg_ParseTuple(args, "si",&uniform, &nloc))
+       if(PyArg_ParseTuple(args, "si:setUniformDef",&uniform, &nloc))
        {
                int loc = GetUniformLocation(uniform);
                if(loc != -1)