I am having a problem to upgrade a program to shaders in OGLES 2.
This is the data structure:
namespace packt {
class GraphicsService {
public:
...
struct ESMatrix{ GLfloat m[4][4]; };
typedef struct
{ // Handle to a program object
GLuint programObject;
// Attribute locations
GLint positionLoc;
// Uniform locations
GLint mvpLoc;
// Vertex data
GLfloat *vertices;
GLuint *indices;
int numIndices;
// Rotation angle
GLfloat angle;
// MVP matrix
ESMatrix mvpMatrix;
} UserData;
...
}
}
In android_main the context is set:
packt::GraphicsService lGraphicsService(pApplication,&lTimeService);
packt::Context lContext = { &lGraphicsService, &lTimeService };
But then instead of using glColor4f, which is not part of OGLES 2, now we are trying to replace that command by shaders. Here is how it goes to the setup method:
void GraphicsService::setup() {
glEnable(GL_TEXTURE_2D);
GLbyte vShaderStr[] =
"uniform mat4 u_mvpMatrix; \n"
"attribute vec4 a_position; \n"
"void main() \n"
"{ \n"
" gl_Position = u_mvpMatrix * a_position; \n"
"} \n";
GLbyte fShaderStr[] =
"precision mediump float; \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n"
"} \n";
// Load the shaders and get a linked program object
// replaced :: //glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
UserData *userData = mContext->userData;
userData->programObject = esLoadProgram (vShaderStr, fShaderStr);
glDisable(GL_DEPTH_TEST);
}
Which gives the following compiler’s message:
error: UserData *userData = mContext->userData;
- 'void*' is not a pointer-to-object type
error: userData->programObject = esLoadProgram (vShaderStr, fShaderStr);
- invalid conversion from 'GLbyte*' to 'const char*'
- initializing argument 1 of 'GLuint esLoadProgram(const char*, const char*)'
- initializing argument 2 of 'GLuint esLoadProgram(const char*, const char*)'
Any suggestion how to fix it? All comments are highly appreciated.
1.
The context object, pointed to by
mContext, contains a “userData” field, which is probably declared to be avoid*. You are storing a pointer to your actualUserDataobject in that field. Conversion fromUserData*tovoid*works implicitly, but to get aUserData*from avoid*, you need an explicit cast:Note that
reinterpret_castis potentially dangerous; when you use it, make sure that yourvoid*really came from aUserData*in the first place, otherwise Bad Things Will Happen.2.
The error message says it all:
esLoadProgramexpects arguments of typeconst char*, and you are passing arguments of typeGLbyte*. Declare yourvShaderStrandfShaderStrappropriately, as in: