Jump to content
  • Advertisement
Sign in to follow this  
BlackDragon777

Cel-Shading with OpenGL

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I recently worked through the tutorial on Cel-Shading. However I have a small problem now. In that tutorial, the Author uses only the shader texture for his model. I would like to add a character texture that has a face, shirt, pants, etc for my model. On top of that I would like to use cel-shading. However I haven't been successful with this. Perhaps my understanding in cell shading is still weak. I have tried redrawing the polygons with their textues respectively on top of each other however the cel-shading doesn't show through. How can I be successful in this project? Thanks! -- Brandon Fogerty http://www.jujikasoft.com GOD Bless you Always my Friends!!!!! Shuku fuku ga arimasu you ni!!!!!!!

Share this post


Link to post
Share on other sites
Advertisement
I recommend multitexturing. Or multipass blending, though on any modern card, multitexturing is fine.

*EDIT* To explain better.
You need to have the original texture, then have the shading texture. Using multitexturing, combine the two, defining the texcoords for each the same way as you would separately, just switching texture units. That should combine the textures much like most terrain that has detail textures on top.

Share this post


Link to post
Share on other sites
Can you tell me where I can find more information or example code on the above two solutions listed? Thanks in advance!

-- Brandon Fogerty
http://www.JujikaSoft.com


GOD Bless you Always!!!!

Share this post


Link to post
Share on other sites
NEHE actually doesn't have a tutorial for multitexturing. In the OpenGL Game Programming book is where I learned it, though this website should have an article on it. Or google for it. Keywords are "OpenGL Multitexture" It is an extension more likely than not, atleast on windows. Search for a good article, try it, then if it doesn't work, at least the texturing part(not the cel-shade), post the code and ask for help. You should be able to though.

Share this post


Link to post
Share on other sites
I just want to thank kburkhart84 and _nomad_ very much! Thank you very much kburkhart84 for your insite on how I could solve my problem (I am still learning OpenGL), and I want to thank _nomad_ for his excellent tutorials!

I will try it out as soon as I can and tell you my results.

Thanks again!

-- Brandon Fogerty
http://www.jujikasoft.com

GOD Bless you Always my Friends!!!!!!

Share this post


Link to post
Share on other sites
Well I tried your tutorial _nomad_. I got multitexturing working but I still am unable to combine both my Cel Shading texture (1D texture) and my Model's texture (2D texture).

The code is working somewhat correctly. Here is why I say that.
This is a snapshot of three tests I performed.

http://www.jujikasoft.com/projects/opengl_temp/example.JPG

The first is a combination of both cel-shading and drawing my character's texture. However as you can see, only the model's texture can be seen. I guess the cell-shaded texture is hidden.

The second picture to the right shows only the cell shaded version of my model. I did not draw the model's texture at all.

The last picture to the very far end shows that multitexturing is working because I colored my model with a shade of blue and textured it with its texture and both are blended together nicely. (Sorry for the poor image quality)

So I am not really sure what I am doing wrong here.

Here is some source for my rendering function.

Please keep in mind that my cel shading texture is 1D and my model's skin texture is a 2D texture. All three example pictures that I displayed were done using multitexturing.

Here is the code:


void display()
{
static float spin = 0.0;

float TmpShade;
Matrix TmpMatrix;
Vector TmpVector, TmpNormal;


if(spin<=360.0)
spin++;
else
spin=0.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);

// Cell Shading
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_1D, shaderTexture[0]);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, ObjModel.num_texture[0]);

glEnable(GL_TEXTURE_1D);
glEnable(GL_TEXTURE_2D);

glRotatef(spin,0.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);
glGetFloatv(GL_MODELVIEW_MATRIX,TmpMatrix.Data);

glBegin(GL_TRIANGLES);
for(int i=0; i<PolyNum; i++)
{
for(int j=0; j<3; j++)
{

TmpNormal.x = polyData.vert[j].norm.x;
TmpNormal.y = polyData.vert[j].norm.y;
TmpNormal.z = polyData.vert[j].norm.z;

RotateVector(TmpMatrix, TmpNormal ,TmpVector);
Normalize(TmpVector);

TmpShade = DotProduct(TmpVector, lightAngle);

if(TmpShade<0.0f)
{
TmpShade = 0.0f;
}

switch(j)
{
case 0:
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,ObjModel.object.mapcoord[ObjModel.uv_index.a].u,
ObjModel.object.mapcoord[ObjModel.uv_index.a].v);

// glTexCoord2f(ObjModel.object.mapcoord[ObjModel.uv_index.a].u,
// ObjModel.object.mapcoord[ObjModel.uv_index.a].v);
break;

case 1:
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,ObjModel.object.mapcoord[ObjModel.uv_index.b].u,
ObjModel.object.mapcoord[ObjModel.uv_index.b].v);

// glTexCoord2f(ObjModel.object.mapcoord[ObjModel.uv_index.b].u,
// ObjModel.object.mapcoord[ObjModel.uv_index.b].v);
break;

case 2:
glMultiTexCoord2fARB(GL_TEXTURE1_ARB,ObjModel.object.mapcoord[ObjModel.uv_index.c].u,
ObjModel.object.mapcoord[ObjModel.uv_index.c].v);

// glTexCoord2f(ObjModel.object.mapcoord[ObjModel.uv_index.c].u,
// ObjModel.object.mapcoord[ObjModel.uv_index.c].v);
break;
}

//glTexCoord1d(TmpShade);
glMultiTexCoord1fARB(GL_TEXTURE0_ARB,TmpShade);
glVertex3f(polyData.vert[j].pos.x,polyData.vert[j].pos.y,polyData.vert[j].pos.z);
//glVertex3fv(&polyData.vert[j].pos.x);
}
}

glEnd();
glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_2D);

// Outline of Model
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDepthFunc(GL_LEQUAL);

glPolygonMode(GL_BACK, GL_LINE);

glLineWidth(2.1);

glCullFace(GL_FRONT);

glColor3f(0.0,0.0,0.0);

glBegin(GL_TRIANGLES);

for(int i=0; i<PolyNum; i++)
{
for(int j=0; j<3; j++)
{
glVertex3fv(&polyData.vert[j].pos.x);
}
}

glEnd();

glDepthFunc(GL_LESS);

glCullFace(GL_BACK);
glPolygonMode(GL_BACK, GL_FILL);
glDisable(GL_BLEND);

glFlush();
glutSwapBuffers();
glutPostRedisplay();
}


If I comment out
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, ObjModel.num_texture[0]);
glEnable(GL_TEXTURE_2D);

The cell shading works however if I leave these three lines
of code in my source, only the model's skin texture can
be seen.

Any help would be highly appreciated!


Thanks in advance!

-- Brandon Fogerty
http://www.jujikasoft.com

GOD Bless you Always my Friends!!!!!

Share this post


Link to post
Share on other sites
A quick look over and this:
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_1D, shaderTexture[0]);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, ObjModel.num_texture[0]);

glEnable(GL_TEXTURE_1D);
glEnable(GL_TEXTURE_2D);


Should probably be:
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_1D, shaderTexture[0]);
glEnable(GL_TEXTURE_1D);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, ObjModel.num_texture[0]);
glEnable(GL_TEXTURE_2D);

Share this post


Link to post
Share on other sites
Hey OrangyTang! Thanks a lot! That was my problem!
I didn't realize that you had to enable GL_TEXTURE_1D and GL_TEXTURE_2D
right after binding them respectively. Thanks a lot! =)

Why is that exactly? Is it because OpenGL is a state machine?
(Not sure)

That fixed my problem! YAY!

:: Does a Happy Dance! ::

-- Brandon Fogerty
http://www.jujikasoft.com

GOD Bless you Always!!!!!!!!

Share this post


Link to post
Share on other sites
Yup, OpenGL is a state machine. Lots of stuff defaults to not enabled(textures) so you have to enable them. If you don't it won't be done for you. That also means that for any texture unit, if you enable texturing(GL_TEXTURE_2D or whatever), it will stay on until you disable it again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!