Sign in to follow this  
Davidtse

OpenGL I really need some help getting texturing to work

Recommended Posts

Davidtse    300
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
jameszhao00    271
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
Davidtse    300
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
Davidtse    300
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
RobinsonUK    108
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  

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now