I want to bind a texture to a quad through using ofMesh and ofVbo. But I can’t get the texture but black quad. I have searched some answers, but I still can’t figure it out. Here is my code below:
ofVbo vbo;
ofMesh myMesh;
ofShader shader;
ofImage backgroundImage;
void ofApp::setup(){
shader.load("shader");
backgroundImage.loadImage("A.jpg");
myMesh.addVertex(ofVec3f(-1.0, 1.0, 0.0));
myMesh.addTexCoord(ofVec2f(0.0, 1.0));
myMesh.addVertex(ofVec3f(1.0, 1.0, 0.0));
myMesh.addTexCoord(ofVec2f(1.0, 1.0));
myMesh.addVertex(ofVec3f(-1.0, -1.0, 0.0));
myMesh.addTexCoord(ofVec2f(0.0, 0.0));
myMesh.addVertex(ofVec3f(1.0, 1.0, 0.0));
myMesh.addTexCoord(ofVec2f(1.0, 1.0));
myMesh.addVertex(ofVec3f(-1.0, -1.0, 0.0));
myMesh.addTexCoord(ofVec2f(0.0, 0.0));
myMesh.addVertex(ofVec3f(1.0, -1.0, 0.0));
myMesh.addTexCoord(ofVec2f(1.0, 0.0));
vbo.setMesh(myMesh, GL_STATIC_DRAW);
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255);
shader.begin();
backgroundImage.getTextureReference().bind();
shader.setUniformTexture("sample", backgroundImage.getTextureReference(), 1);
vbo.draw(GL_TRIANGLES, 0, 6);
shader.end();
backgroundImage.getTextureReference().unbind();
}
and my vertex shaders:
// these come from the programmable pipeline
//uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec2 texcoord;
// texture coordinates are sent to fragment shader
out vec2 texCoordVarying;
void main()
{
texCoordVarying = texcoord;
mat4 modelViewProjectionMatrix = mat4(1.0f);
gl_Position = modelViewProjectionMatrix * position;
}
fragment shader:
// these are our textures
uniform sampler2D sample;
// this comes from the vertex shader
in vec2 texCoordVarying;
// this is the output of the fragment shader
out vec4 outputColor;
void main()
{
vec3 src = texture2D(sample, texCoordVarying).rgb;
outputColor = vec4(src, 1.0);
}