Hi forum,
I am stuck with the problem of basic texturing. Check the following snippet:
void startup()
{
//now load the floor texture
char *imagePath = sdkFindFilePath("floortile.ppm",0);
if(imagePath == NULL)
{
std::cerr << "Error finding floor image file." << std::endl;
exit(EXIT_FAILURE);
}
//load the floor image texture
floorTex = loadTexture(imagePath);
std::vector<tdogl::Shader> floorShaders;
floorShaders.push_back(tdogl::Shader::shaderFromFile("floor-vertex-shader.txt",GL_VERTEX_SHADER));
floorShaders.push_back(tdogl::Shader::shaderFromFile("floor-fragment-shader.txt",GL_FRAGMENT_SHADER));
floorProgram = new tdogl::Program(floorShaders);
}
GLuint loadTexture(char *filename)
{
unsigned char *data = 0;
unsigned int width, height;
sdkLoadPPM4ub(filename, &data, &width, &height);
if (!data)
{
std::cerr << "Error opening file:" << filename << std::endl;
return 0;
}
std::cerr << "Loaded: "
<< filename << " "
<< width << " "
<< height << " "
<< std::endl;
return createTexture(GL_TEXTURE_2D, GL_RGBA8, GL_RGBA, width, height, data);
}
GLuint createTexture(GLenum target, GLint internalformat, GLenum format, int w, int h, void *data)
{
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(target, tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(target, 0, internalformat, w, h, 0, format, GL_UNSIGNED_BYTE, data);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
return tex;
}
void loadGeometry()
{
glGenVertexArrays(1,&gVAO);
glBindVertexArray(gVAO);
glGenBuffers(1,&gVBO);
glBindBuffer(GL_ARRAY_BUFFER,gVBO);
GLfloat vertexData[] = {0.0f,0.8f,0.0f, 0.5f,1.0f,
-0.8f,-0.8f,0.0f, 0.0f,0.0f,
0.8f,-0.8f,0.0f , 1.0f,0.0f};
glBufferData(GL_ARRAY_BUFFER,sizeof(vertexData),vertexData,GL_STATIC_DRAW);
//connect the xyz of the vertexData to the "position" attribute of the vertex shader
glEnableVertexAttribArray(floorProgram->attrib("position"));
glVertexAttribPointer(floorProgram->attrib("position"),3,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),NULL);
//connect the uv coordinates to the "posTexCoord" attribute of the vertex shader
glEnableVertexAttribArray(floorProgram->attrib("posTexCoord"));
glVertexAttribPointer(floorProgram->attrib("posTexCoord"),2,GL_FLOAT,GL_TRUE,
5*sizeof(GLfloat),
(const GLvoid*)(3 * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
}
void render(double time)
{
static const GLfloat one = 1.0f;
glViewport(0,0,winWidth,winHeight);
glClearBufferfv(GL_COLOR,0,backgroundcolor);
glClearBufferfv(GL_DEPTH,0,&one);
floorProgram->use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,floorTex);
floorProgram->setUniform("tex",0);
glBindVertexArray(gVAO);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D,0);
floorProgram->stopUsing();
}
i can see the triangle on screen if i disable the texturing. With texturing i see a blank screen . Any hint?
Now goes the vertex and fragment shader:
////////////// vertex shader ///////////////////////
#version 430 core
in vec3 position;
in vec2 posTexCoord;
out vec2 fragTexCoord;
void main()
{
gl_Position = vec4(position,1);
//pass the tex coordinate straight through to the fragment shader
fragTexCoord = posTexCoord;
}
////////////// fragment shader ///////////////////////
#version 430 core
uniform sampler2D tex;
in vec2 fragTexCoord;
out vec4 finalColor;
void main()
{
finalColor = texture(tex,fragTexCoord);
//finalColor = vec4(1.0,1.0,1.0,1.0);
}
Thanks