Sign in to follow this  
proanim

how to texture an object with DevIL

Recommended Posts

proanim    455
I encountered a problem following step by step tutorials at [url="http://ogldev.atspace.co.uk/index.html"]http://ogldev.atspace.co.uk/index.html[/url]
I am looking for a way to apply texture mapping with DevIL and i use this code

[source lang="cpp"]bool GenerateTexture()
{
// ILuint is a 32bit unsigned integer variable texid will be used to store image name
ILuint texid;

// ILboolean is type similar to GLboolean and can equal GL_FALSE (0) or GL_TRUE (1)
// it can have different value (because it's just typedef of unsigned char), but this sould be
// avoided
// variable success will be used to determine if some function returned success or failure
ILboolean success;

GLuint image;

ilGenImages(1, &texid); // generation of one image name
ilBindImage(texid); // binding of image name

success = ilLoadImage("logo.png"); // loading of image

if (success) // if no error occured
{
// convert every colour component into unsigned byte
//if your image contains alpha channel you can replace IL_RGB with IL_RGBA
success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

if (!success) // error occured
return false;

glGenTextures(1, &image); // texture name generation
glBindTexture(GL_TEXTURE_2D, image); // binding of texture name
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // we will use linear interpolation for magnification filter
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // we will use linear interpolation for minifying filter
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
ilGetData()); // texture specification

glActiveTexture(image); // ?
glBindTexture(image, texid); // ?
}

// because we have already copied image data into texture data we can release memory
// used by image
ilDeleteImages(1, &texid);

return true;
}[/source]
and my shader for texturing is placed in the source code of the project as

[source lang="cpp"]// vertex shader
static const char* pVS = " \n\
#version 330 \n\
\n\
layout (location = 0) in vec3 Position; \n\
layout (location = 1) in vec2 TexCoord; \n\
\n\
uniform mat4 gWorld; \n\
\n\
out vec2 TexCoord0; \n\
\n\
void main() \n\
{ \n\
gl_Position = gWorld * vec4(Position, 1.0); \n\
\n\
TexCoord0 = TexCoord; \n\
}";

// fragment shader
static const char* pFS = " \n\
#version 330 \n\
\n\
in vec2 TexCoord0; \n\
\n\
out vec4 FragColor; \n\
\n\
uniform sampler2D gSampler; \n\
\n\
void main() \n\
{ \n\
FragColor = texture2D(gSampler, TexCoord0.xy); \n\
}";[/source]

At this point my object appears black, there is no lighting in my project yet so I don't think that is the problem. How can I pass the texture to the shader correctly?

Share this post


Link to post
Share on other sites
rocklobster    415
[CODE]
// bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texId);

// send texture to shader
GLuint id = glGetUniformLocation(programId, "gSampler");
glUniform1i(id, 0); // 0 for GL_TEXTURE0
[/CODE]

your frag shader looks fine Edited by rocklobster

Share this post


Link to post
Share on other sites
proanim    455
Thanks for the response, but i still can't get this to work. I fixed 'Bind texture' part in my GenerateTexture() function but I don't see the way to use

GLuint id = glGetUniformLocation(programId, "gSampler");

and i put this

glUniform1i(id, 0); // 0 for GL_TEXTURE0
in my render function

the problem is that the tutorials on the link i posted use more than one shader function to load and process shaders. So I can't get programId from outside. When I put this function inside CompileShaders function that is used in tutorials I still can't make this work. Can you help?

Share this post


Link to post
Share on other sites
rocklobster    415
[quote name='proanim' timestamp='1355502502' post='5010649']
Thanks for the response, but i still can't get this to work. I fixed 'Bind texture' part in my GenerateTexture() function but I don't see the way to use

GLuint id = glGetUniformLocation(programId, "gSampler");

and i put this

glUniform1i(id, 0); // 0 for GL_TEXTURE0
in my render function

the problem is that the tutorials on the link i posted use more than one shader function to load and process shaders. So I can't get programId from outside. When I put this function inside CompileShaders function that is used in tutorials I still can't make this work. Can you help?
[/quote]

Basically the getUniformLocation and uniform1i functions are for sending data to the shader. So to draw your textured object you need to:

- Bind the shader
- Set texture unit
- Bind texture
- Set the uniforms for the shader
- Draw the object

Make sure your function that creates your shaders returns a GLuint for the program id (this will be the return value from glCreateProgram())

then you can bind the shader, texture and set the uniforms

[CODE]
// loading the data
GLuint myShader = CreateShaders("vertex.vert", "fragment.frag");
GLuint texId = CreateTexture("logo.png");

// when drawing
glUseProgram(myShader);

glBindTexture(GL_TEXTURE_2D, texId);
glActiveTexture(GL_TEXTURE0);

GLuint loc = glGetUniformLocation(myShader, "gSampler");
glUniform1i(loc, 0);

// then just draw your mesh
glDrawArrays(GL_TRIANGLES, 0, mesh.count);
[/CODE] Edited by rocklobster

Share this post


Link to post
Share on other sites
rocklobster    415
I'd recommend these tutorials: [url="http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/"]http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/[/url] they really helped me when I was getting into shaders.

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