Sign in to follow this  
Davidtse

OpenGL I really need some help getting texturing to work

Recommended Posts

I have been trying for days to get a texturing to work in opengl, and I just cant figure out the problem. I have worked with opengl before in java and had no problems with texturing so I kind of know what im doing. If someone could please look at my code and tell me what I am doing wrong. I cannot get texture2D to return anything other than black(I also tried using just texture(sampler, coords)). This just renders all the meshes as solid black texture2D returns black whatever I try, I dont think its a problem with the texture coordinates because I dont think it would be solid black if the texture coords where screwed up. I really dont like posting chunks of code and asking someone to find what im doing wrong but I just cant for the life of me figure out what I am doing wrong.

Frag Shader:

[code]#version 400

uniform sampler2D Texture0 ;
in vec2 out_MultiTextureCoord1;
in vec4 ex_Color;
out vec4 out_Color;

void main(void)
{
out_Color = texture(Texture0, out_MultiTextureCoord1);
}[/code]

Vert Shader:

[code]#version 400

layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
layout(location=2) in vec4 in_Normal;
layout(location=3) in vec2 in_MultiTextureCoord1;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 WorldMatrix;
out vec2 out_MultiTextureCoord1;
out vec4 ex_Color;


void main(void)
{
gl_Position = (ProjectionMatrix * ViewMatrix * WorldMatrix) * in_Position;
ex_Color = in_Color;
out_MultiTextureCoord1 = in_MultiTextureCoord1;
}[/code]

Texture Loading Code:
[code]GLuint CResourceManager::LoadTexture(std::string fileName)
{
ILuint imageID; // Create an image ID as a ULuint
GLuint textureID; // Create a texture ID as a GLuint
ILboolean success; // Create a flag to keep track of success/failure
ILenum error; // Create a flag to keep track of the IL error state
ilGenImages(1, &imageID); // Generate the image ID
ilBindImage(imageID); // Bind the image
success = ilLoadImage(fileName.c_str()); // Load the image file

// If we managed to load the image, then we can start to do things with it...
if (success)
{
// If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
ILinfo ImageInfo;
iluGetImageInfo(&ImageInfo);
if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
{
iluFlipImage();
}

// Convert the image into a suitable format to work with
// NOTE: If your image contains alpha channel you can replace IL_RGB with IL_RGBA
success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

// Quit out if we failed the conversion
if (!success)
{
error = ilGetError();
std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
exit(-1);
}

// Generate a new texture
glGenTextures(1, &textureID);

// Bind the texture to a name
glBindTexture(GL_TEXTURE_2D, textureID);

glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Set texture interpolation method to the highest visual quality it can be:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D); // Note: This requires OpenGL 3.0 or higher
// Specify the texture specification
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
ilGetInteger(IL_IMAGE_BPP), // Image colour depth
ilGetInteger(IL_IMAGE_WIDTH), // Image width
ilGetInteger(IL_IMAGE_HEIGHT), // Image height
0, // Border width in pixels (can either be 1 or 0)
ilGetInteger(IL_IMAGE_FORMAT), // Image format (i.e. RGB, RGBA, BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
ilGetData()); // The actual image data itself
}
else // If we failed to open the image file in the first place...
{
error = ilGetError();
std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
exit(-1);
}

ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image.
std::cout << "Texture creation successful." << std::endl;
return textureID; // Return the GLuint to the texture so you can use it!
}[/code]

Render code:


[code] CMatrixManager::GetInstance()->SetShaderMatrices(m_Shader);
glUseProgram(m_Shader);
for(int x = 0; x < m_MeshCount; ++x)
{
glUniform1i(glGetUniformLocation(m_Shader, "Texture0"), 0);
glActiveTexture( GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, m_Meshes[x].m_Textures[0]);
glUseProgram(m_Shader);
//CRenderingSystem::GetInstance()->RenderMesh(&m_Meshes[x]);
glBindVertexArray(m_Meshes[x].m_VertexArrayID);
glDrawElements(m_Meshes[x].m_RenderType, m_Meshes[x].m_PrimitiveCount, GL_UNSIGNED_INT, 0);
}[/code]

If there is any other part of the code that may be causing the problem please tell me so I can post it

Thank you,
David

Share this post


Link to post
Share on other sites
You're not checking OpenGL errors.

When I dabbled in OpenGL stuff, I had the following stuff

[CODE]
void report_gl_error(const char* file, int line_number)
{
GLenum error = glGetError();
if(error != GL_NO_ERROR)
{
log("ERROR: OGL (%s@%d) : %s\n", file, line_number,
gluErrorString(error));
}
}
#define GLE report_gl_error(__FILE__, __LINE__);
[/CODE]

and after every OpenGL call, I put in GLE e.g.

[CODE]
glGenBuffers(1, &buffer_handle);
GLE
glBindBuffer(GL_ARRAY_BUFFER, buffer_handle);
GLE
[/CODE]

Share this post


Link to post
Share on other sites
Thanks, I error checked all my gl functions and got a few errors.

I got invalid enumerant where the texture is created and replaced the DevIl stuff with some test enums and it stoped the gl error but did not fix the texturing problem

after replacement of Devil stuff:

[code] glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
GL_RGBA8, // Image colour depth
200, // Image width
200, // Image height
0, // Border width in pixels (can either be 1 or 0)
GL_BGRA, // Image format (i.e. RGB, RGBA, BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
ilGetData()); // The actual image data itself
GLE[/code]

I get Invalid Enumerant after some glew and DevIL init functions:

[code] //Initialize GLEW
GLenum GlewInitResult = glewInit();GLE//INVALID ENUMERANT
if (GLEW_OK != GlewInitResult) { fprintf( stderr, "ERROR: %s\n", glewGetErrorString(GlewInitResult) );
exit(EXIT_FAILURE);}

//Init DevIL
ilInit();GLE
iluInit();GLE
ilutInit();GLE///INVALID ENUMERANT
ilutRenderer(ILUT_OPENGL);GLE//INVALID ENUMERANT[/code]

I also get an invalid opperation right at the begining of the main display loop even after I call glGetError() right before glutMainLoop();

Then I get Invalid opperations after all 3 of my glUniformMatrix4fv calls for the view world and proj. But they are still getting passed to the shader fine and work the way they are supposed to, but I can get rid of the invalid opperation by passing a float array, so I dont think this is causing the texture problem.

I think the problem is probobly with the actualy texture loading and DevIl, but I am not sure. I am going to try some other texture loading to see if that is really the problem, if any of you guys could offer some insight into my problems I would really appreciate it.

Thank you,
David

Share this post


Link to post
Share on other sites
Ok devil was the problem i switched to SOIL and everything is working perfectly, but i still get an invalid enumerant after the glewInit() for some reason.

Thanks james

Share this post


Link to post
Share on other sites
I'm using DevIL. It's fine for image loading. I assume you've initialised it, but you don't post that code:



[CODE]
// Initialize IL

ilInit();

// Initialize ILU

iluInit();

// Set global origin function for loading images (dds, for example, have a top, left origin).

ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this