basic texturing

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

Have you tried setting GL_GENERATE_MIPMAP_SGIS = TRUE before uploading the texture image? I think it only generates the mipmaps once a new image is loaded to the base image after this flag is set. You also set GL_TEXTURE_MIN_FILTER to GL_LINEAR which is redundant as you then set it to GL_LINEAR_MIPMAP_LINEAR a few lines afterwards.

Thanks for the hint!

Basically i am trying to port an old OpenGL application (2.x) to the modern OpenGL. (4.x).
My driver does support 4.3 and i believe that the required extension is present in this version.
I did not get the idea of checking “GL_GENERATE_MIPMAP_SGIS = TRUE”. How and where should i check it ? Some hint ?
I tried it as follows:


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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);   
   glTexParameteri(target, GL_TEXTURE_MAG_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);

   glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
   
   return tex;
}

The issue has been resolved with the glGenerateMipmap instead

Thanks

Sorry, I meant putting

glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);

before

glTexImage2D(target, 0, internalformat, w, h, 0, format, GL_UNSIGNED_BYTE, data);

but glGenerateMipmap is the preferred way.