我在将程序升级到OGLES 2中的着色器时遇到问题。
这是数据结构:
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;
...
}
}
在android_main中设置了上下文:
packt::GraphicsService lGraphicsService(pApplication,&lTimeService);
packt::Context lContext = { &lGraphicsService, &lTimeService };
但是现在我们尝试用着色器替换该命令,而不是使用不属于OGLES 2的glColor4f。以下是设置方法的用法:
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);
}
其中给出了以下编译器的消息:
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*)'
有任何建议如何解决?所有评论都非常感谢。
答案 0 :(得分:1)
1
mContext
指向的上下文对象包含一个“userData”字段,可能被声明为void*
。您正在存储指向该字段中实际UserData
对象的指针。从UserData*
到void*
的转换是隐式的,但要从UserData*
获得void*
,您需要一个明确的演员:
UserData *userData = reinterpret_cast<UserData*>( mContext->userData );
请注意,reinterpret_cast
具有潜在危险性;当您使用它时,请确保您的void*
首先来自UserData*
,否则糟糕的事情将会发生。
2
错误消息说明了一切:esLoadProgram
期望类型为const char*
的参数,并且您传递的类型为GLbyte*
。适当地声明您的vShaderStr
和fShaderStr
,如:
const char *vShaderStr =
"uniform ...
答案 1 :(得分:0)
我按照你的建议,在必要时更改了它:
UserData *userData = static_cast<UserData*>( esContext->userData );
...
const char vShaderStr[] ={
"uniform mat4 u_mvpMatrix; \n"
"attribute vec4 a_position; \n"
"void main() \n"
"{ \n"
" gl_Position = u_mvpMatrix * a_position; \n"
"} \n"};
非常感谢!