• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
proanim

how to texture an object with DevIL

6 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
[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
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[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
1

Share this post


Link to post
Share on other sites
Thanks for the help, i wonder why there are no more people that can explain things this way in their tutorials.
0

Share this post


Link to post
Share on other sites
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.
0

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  
Followers 0