Jump to content

  • Log In with Google      Sign In   
  • Create Account


how to texture an object with DevIL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 proanim   Members   -  Reputation: 437

Like
0Likes
Like

Posted 12 December 2012 - 08:41 PM

I encountered a problem following step by step tutorials at http://ogldev.atspace.co.uk/index.html
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 shaderstatic 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 shaderstatic 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?

Sponsor:

#2 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 13 December 2012 - 10:52 PM

// 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

your frag shader looks fine

Edited by rocklobster, 13 December 2012 - 10:54 PM.


#3 proanim   Members   -  Reputation: 437

Like
0Likes
Like

Posted 14 December 2012 - 10:28 AM

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?

#4 Waterlimon   Crossbones+   -  Reputation: 2350

Like
0Likes
Like

Posted 14 December 2012 - 11:28 AM

I switched to FreeImage because it felt more modern and was simpler to use.

Waterlimon (imagine this is handwritten please)


#5 rocklobster   Members   -  Reputation: 415

Like
1Likes
Like

Posted 17 December 2012 - 12:44 AM

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?


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

// 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);

Edited by rocklobster, 17 December 2012 - 12:46 AM.


#6 proanim   Members   -  Reputation: 437

Like
0Likes
Like

Posted 17 December 2012 - 06:18 PM

Thanks for the help, i wonder why there are no more people that can explain things this way in their tutorials.

#7 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 18 December 2012 - 01:22 AM

I'd recommend these tutorials: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ they really helped me when I was getting into shaders.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS