GPU:
vertex shader
uniform mediump mat4 uViewProjMatrix;
varying lowp vec4 vColor;
void main(void)
{
gl_Position = uViewProjMatrix * vec4(aPosition.x, aPosition.y, 0.0, 1.0);
vColor = vec4(aPosition.x * 0.5 + 0.5, aPosition.y * 0.5 + 0.5, 0.0, 1.0);
}
frag shader
#version 100
precision highp float;
varying lowp vec4 vColor;
void main(void)
{
gl_FragColor = vColor;
}
cpu:
init()
initRendering(void) {
NV_APP_BASE_SHARED_INIT();
NvAssetLoaderAddSearchPath("tutorial/Basic");
mProgram = NvGLSLProgram::createFromFiles("shaders/plain.vert", "shaders/plain.frag");
static const float position[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
//-----------基于索引绘制
static const GLushort plane_indices[] =
{
0, 1, 2, 3
};
glGenBuffers(1, &_index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(plane_indices), plane_indices, GL_STATIC_DRAW);
//--------------
glGenBuffers(1, &_quad_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(position), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(position), position);
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
GLint naPosition = mProgram->getAttribLocation("aPosition");
glVertexAttribPointer(naPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
}
draw()
draw(void)
{
CHECK_GL_ERROR();
glClearColor(0.0f, 0.25f, 0.3f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
CHECK_GL_ERROR();
nv::matrix4f projection_matrix;
nv::perspective(projection_matrix, 3.14f * 0.25f, m_width/(float)m_height, 0.1f, 30.0f);
nv::matrix4f camera_matrix = projection_matrix * m_transformer->getModelViewMat();
CHECK_GL_ERROR();
mProgram->enable();
mProgram->setUniformMatrix4fv("uViewProjMatrix", camera_matrix._array, 1, GL_FALSE);
CHECK_GL_ERROR();
//-----------------------------------------
glBindVertexArray(_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _index_buffer);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT,NULL);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//------------------------------------------
CHECK_GL_ERROR();
mProgram->disable();
}