这应该是非常基本的,但作为一个初学者我不知道哪里有bug。 我正在尝试使用vbos和着色器在矩形中渲染纹理。结果是背景的颜色(所以没有!)。在下面的代码中,矩形位置在向量mFloatVertex中,每个顶点有三个坐标:(x,y,z)。 我不确定纹理的坐标是否正确处理。
一些声明:
GLuint mVertexArrayID;
GLuint mVertexBufferObject;
GLuint mTextureBufferObject;
std::vector<GLfloat> mFloatVertex;
GLuint mProgramID;
unsigned char * mData;
执行加载的代码:
glGenVertexArrays(1, &mVertexArrayID);
glBindVertexArray(mVertexArrayID);
glGenBuffers(1, &mVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
//coordinates of the texture
glBufferData(GL_ARRAY_BUFFER, mFloatVertex.size() * sizeof (float), &mFloatVertex[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenTextures(1, &mTextureBufferObject);
glBindTexture(GL_TEXTURE_2D, mTextureBufferObject);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, mData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
进行抽奖的代码:
glUseProgram(mProgramID);
//retrieve the location of the sampler in memory.
int loc = glGetUniformLocation(mProgramID, "tex");
//pass the 0 value to the sampler, meaning it is to use texture unit 0
glUniform1i(loc, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTextureBufferObject);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_LINE_LOOP, 0, mFloatVertex.size()/3);
glDisableVertexAttribArray(0);
glUseProgram(0);
顶点着色器:
#version 330 core
void main(){
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}
片段着色器:
#version 330 core
uniform sampler2D tex;
void main(){
gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);
}
观点:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, maxWindowWidthHeight, maxWindowWidthHeight);
GLfloat orthoDim = maxWindowWidthHeight/100;
glOrtho((-1.0)*orthoDim, orthoDim, (-1.0)*orthoDim, orthoDim, -10.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
实际上,如果我将背景设置为绿色,那么我只会看到一个薄的白色矩形,坐标为mFloatVertex。矩形充满绿色。或者,如果在片段着色器中我只是返回红色而在绘制时根本不使用纹理,我得到一个红色矩形。所以坐标看起来很好。 可能是纹理是完全透明的吗?