Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Multitexturing - Problems setting vertex color

  1. #1
    Newbie Newbie
    Join Date
    Jul 2018
    Location
    Porter, Texas
    Posts
    22

    Multitexturing - Problems setting vertex color

    I have two textures that I am attempting to blend together, in the demo application I am trying to set the vertex color of the primitive that I am drawing with 'glColor4ub(255, 0, 0, 255);' So the primitive should be red when being rendered.

    The first texture is red, but the second texture is not and instead remains as full white (255, 255, 255). So I basically want that second texture to be red as the first one.

    I've been looking into various ways to blend but all have failed and at this point I am stumped. I've also tried GL_MODULATE in place of GL_ADD but that instead makes the final result way too dark.

    Here is the code to draw and display the multi-textured polygon:

    Code :
    void render( void )
    {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     
    	glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    	glTranslatef( 0.0f, 0.0f, -4.0f );
     
    	//
    	// STAGE 0
    	//
     
    	glActiveTextureARB( GL_TEXTURE0_ARB );
    	glColor4ub(255, 0, 0, 255);
    	glBindTexture( GL_TEXTURE_2D, g_textureID_0 );
     
    	//
    	// STAGE 1
    	//
     
    	glActiveTextureARB( GL_TEXTURE1_ARB );
    	glColor4ub(255, 0, 0, 255);
     
     
    	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
     
    	glBindTexture( GL_TEXTURE_2D, g_textureID_1 );
    	glEnable( GL_TEXTURE_2D );
     
    	//
    	// Render our quad with two sets of texture coordinates...
    	//
     
    	glBegin( GL_QUADS );
    	{
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 0.0f );
    		glVertex3f( -1.0f, -1.0f, 0.0f );
     
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 1.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 1.0f );
    		glVertex3f( -1.0f, 1.0f, 0.0f );
     
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 1.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 1.0f );
    		glVertex3f( 1.0f,  1.0f, 0.0f );
     
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 0.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 0.0f );
    		glVertex3f( 1.0f, -1.0f, 0.0f );
    	}
    	glEnd();
     
    	SwapBuffers( g_hDC );
    }

    Full link to the test application can be found here:
    http://www.speedyshare.com/743514512.html


    So below are the two textures that I am trying to blend together:

    Texture 1:


    Texture 2:


    And here is the result:



  2. #2
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Multitexturing - Problems setting vertex color

    After reading your code, I think some glTexEnv function call are missing to make multi-texturing work like you want. Look at the glTexEnv documentation in the SDK.

    Here is a sample code that use multi-texturing to interpolate between one texture (TEXTURE0) and a constant color.
    Code :
            glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,this->constColor);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
    glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPOLATE);
    glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE);
    glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
    glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_PREVIOUS);
    glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
    glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE2_RGB,GL_CONSTANT);
    glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);

  3. #3
    Newbie Newbie
    Join Date
    Jul 2018
    Location
    Porter, Texas
    Posts
    22

    Re: Multitexturing - Problems setting vertex color

    I've noticed you mentioned GL_SOURCE2_RGB and GL_OPERAND2_RGB in the mix. Does that mean I will need to setup three textures to achieve the desired effect?

  4. #4
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Multitexturing - Problems setting vertex colo

    No, I use only one texture unit (TEXTURE0) in my example, I forget to add before all glTexEnv call glActiveTexture(GL_TEXTURE0). So if your effect need two texture unit you have to do something like this:

    setup for texture unit 0
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,texture0_id);
    some glTexEnv() calls.

    setup for texture unit 1
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE2D,texture1_id);
    some glTexEnv calls.








  5. #5
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Multitexturing - Problems setting vertex colo

    If your card permit the use of vertex and fragment shaders, use it. This will avoid you to work with the not so easy fixed function multi-texturing.

  6. #6
    Newbie Newbie
    Join Date
    Jul 2018
    Location
    Porter, Texas
    Posts
    22

    Re: Multitexturing - Problems setting vertex colo

    I am using the code you mentioned which I call after the first and second texture units, but now the second texture unit won't even show up. Also glColor4ub(255, 0, 0, 255); doesn't seem to have no effect on the polygon at all now.

    Even after looking at the docs for glTexEnv, I am still failing to understand how this works.

    Code :
    void render( void )
    {
    	float blah[4] = { 1.0f, 0.0f, 0.0f, 1.0f };	//Red
     
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     
    	glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    	glTranslatef( 0.0f, 0.0f, -4.0f );
     
    	//
    	// STAGE 0
    	//
     
    	glActiveTextureARB( GL_TEXTURE0_ARB );
     
            glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    	glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR, blah);
     
    	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
    	glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPOLATE);
    	glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE);
    	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
    	glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_PREVIOUS);
    	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
    	glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE2_RGB,GL_CONSTANT);
    	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);
     
    	glBindTexture( GL_TEXTURE_2D, g_textureID_0 );
     
    	//
    	// STAGE 1
    	//
     
    	glActiveTextureARB( GL_TEXTURE1_ARB );
     
     
     
    	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    	glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR, blah);
     
    	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
    	glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPOLATE);
    	glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE);
    	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
    	glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_PREVIOUS);
    	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
    	glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE2_RGB,GL_CONSTANT);
    	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_ALPHA);
     
     
    	glBindTexture( GL_TEXTURE_2D, g_textureID_1 );
    	glEnable( GL_TEXTURE_2D );
     
     
     
    	//
    	// Render our quad with two sets of texture coordinates...
    	//
     
    	glBegin( GL_QUADS );
    	{
    		glColor4ub(255, 0, 0, 255);
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 0.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 0.0f );
    		glVertex3f( -1.0f, -1.0f, 0.0f );
     
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 0.0f, 1.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 0.0f, 1.0f );
    		glVertex3f( -1.0f, 1.0f, 0.0f );
     
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 1.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 1.0f );
    		glVertex3f( 1.0f,  1.0f, 0.0f );
     
    		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, 1.0f, 0.0f );
    		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, 1.0f, 0.0f );
    		glVertex3f( 1.0f, -1.0f, 0.0f );
    	}
    	glEnd();
     
    	SwapBuffers( g_hDC );
    }

  7. #7
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Multitexturing - Problems setting vertex colo

    Also glColor4ub(255, 0, 0, 255); doesn't seem to have no effect on the polygon at all now.
    glColor4ub(255, 0, 0, 255) have no effect because the source GL_SOURCE0 of the texture unit 0 is GL_TEXTURE not GL_PREVIOUS. For texture unit 0 the previous source is the incomming fragment. For texture unit 1 GL_PREVIOUS is the incoming fragment from texture unit 0.

    Give me a couple of minutes and I will try to give you a sample code that will do something like that:
    (color of the polygon)*(color from texture unit 0) * (color from texture unit 1).

  8. #8
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Multitexturing - Problems setting vertex colo

    Here the textures setup code:
    Code :
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
        glActiveTexture(GL_TEXTURE0);
        glGenTextures(1,&texID);
        glBindTexture(GL_TEXTURE_2D,texID);
     
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
     
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
        glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULATE);
        glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOUS);
        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
        glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_TEXTURE);
        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
     
     glTexImage2D(GL_TEXTURE_2D,0,image->getInternalTextureFormat(),image->s(),
                     image->t(),0,image->getPixelFormat(),image->getDataType(),
                     image->data());  
     
    glGenTextures(1,&texID2);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D,texID2);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
     
        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
        glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULATE);
        glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOUS);
        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
        glTexEnvf(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_TEXTURE);
        glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
     
        glTexImage2D(GL_TEXTURE_2D,0,image2->getInternalTextureFormat(),image2->s(),
                     image2->t(),0,image2->getPixelFormat(),image2->getDataType(),
                              image2->data());

    Here is the drawing code:
    Code :
     glEnable(GL_TEXTURE_2D);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D,texID);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D,texID2);
        glBegin(GL_QUADS);
        glColor4f(1.0,1.0,1.0,1.0);//Change this color at your will
        glMultiTexCoord2f(GL_TEXTURE0,0.0,0.0);
        glMultiTexCoord2f(GL_TEXTURE1,0.0,0.0); 
        glVertex3f(-1.0,-1.0,-5.0);
        glMultiTexCoord2f(GL_TEXTURE0,0.0,1.0);
        glMultiTexCoord2f(GL_TEXTURE1,0.0,1.0);
        glVertex3f(1.0,-1.0,-5.0);
        glMultiTexCoord2f(GL_TEXTURE0,1.0,1.0);
        glMultiTexCoord2f(GL_TEXTURE1,1.0,1.0);
        glVertex3f(1.0,1.0,-5.0);
        glMultiTexCoord2f(GL_TEXTURE0,1.0,0.0);
        glMultiTexCoord2f(GL_TEXTURE1,1.0,0.0);
        glVertex3f(-1.0,1.0,-5.0);
        glEnd();
        glDisable(GL_TEXTURE_2D);

    Like I said that code will do (color of the quad)*(color of the texture at texture unit 0)*(color of the texture at texture unit 1)

  9. #9
    Newbie Newbie
    Join Date
    Jul 2018
    Location
    Porter, Texas
    Posts
    22

    Re: Multitexturing - Problems setting vertex colo

    That basically gave me the same effect that I got from the very beginning when I first toyed around with multitexturing. While the coloring is working, but the texture itself is so dark that the whole texture is almost pitch black.

    I also tried increasing the modulation by x4 with:

    'glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
    glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, 4);'

    but the texture still has a lot of dark spots which just isn't easy on the eyes. If there's no other solution, then I'll just have to manually blit the two textures together to form one.

  10. #10
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Multitexturing - Problems setting vertex colo

    For texture unit 1 replace glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULAT E); with glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_ADD);

    Edit: What kind of effect you want? It is something like this (polygon color)*(texture0 + texture1). If it's that effect you want then you have to proceed like you write before, or use shaders.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problems setting font color
    By Harukoxd in forum Vulkan
    Replies: 4
    Last Post: 04-23-2017, 12:08 PM
  2. Replies: 3
    Last Post: 09-20-2005, 03:50 PM
  3. setting color key in OpenGL
    By mdive in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-06-2003, 08:17 PM
  4. Setting up color index mode
    By srbox in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-21-2002, 06:22 AM
  5. Problems with setting current color
    By daviem01 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-12-2001, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean