virtual void onInit()
{
//清空窗口为黑色
glClearColor(0,0,0,1);
//设置视口的位置和大小--尝试改变视口的位置和大小
glViewport(0,0,(GLint)_winWidth,(GLint)_winHeight);
//创建VBO-顶点缓冲区
glGenBuffers(1, &_vertexId);//若创建多个,第二个参数应该是数组
//绑定并指定buffer类型为VBO
glBindBuffer(GL_ARRAY_BUFFER,_vertexId);
float x = 100;
float y = 100;
float w = 100;
float h = 100;
CELL::float2 pos[] =
{
CELL::float2(x, y),
CELL::float2(x + w, y),
CELL::float2(x + w, y + h),
CELL::float2(y, y + h),
};
//将内存中的顶点数据传送进显存
glBufferData(GL_ARRAY_BUFFER,sizeof(pos),pos,GL_STATIC_DRAW);
//切换状态
glBindBuffer(GL_ARRAY_BUFFER, 0);
//创建索引缓冲区
glGenBuffers(1, &_indexId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_indexId);
//指定顶点索引
unsigned short arIndex[] =
{
0, 1, 2, 3, 0
};
//数据传递到显卡
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(arIndex), arIndex, GL_STATIC_DRAW);
//切换状态
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}
渲染
virtual void render()
{
//清除深度缓冲和颜色缓冲
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//指定视口大小
glViewport(0,0,_winWidth,_winHeight);
//创建一个投影矩阵
CELL::matrix4 screenProj = CELL::ortho<float>(0, float(_winWidth), float(_winHeight), 0, -100.0f, 100);
glBindBuffer(GL_ARRAY_BUFFER,_vertexId);
_shader.begin();//启用顶点相关属性
{
glUniformMatrix4fv(_shader._MVP,1,false,screenProj.data());
glUniform4f(_shader._color,1,0,0,1);
glVertexAttribPointer(_shader._position, 2, GL_FLOAT, false, sizeof(CELL::float2), 0);
unsigned short arIndex[] =
{
0, 1, 2, 3, 0
};
glDrawElements(GL_LINE_STRIP,5,GL_UNSIGNED_SHORT,arIndex);
}
_shader.end();//关闭顶点相关属性
glBindBuffer(GL_ARRAY_BUFFER,0);
eglSwapBuffers(_display,_surface);
}