It is currently Mon Jun 26, 2017 10:44 am

All times are UTC - 5 hours




 Page 1 of 1 [ 12 posts ] 
Author Message
 Post subject: Shader VMK 44 - Texture Shader
PostPosted: Sun Apr 27, 2014 7:35 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
In this VMK I fill in the missing pieces in the Shader Manager so that we can finally render an image on a rectangular surface. The texture vertex and texture fragment shaders are shown and by the end of the video you will know how to work with the Image2d mko file format.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Wed Dec 31, 2014 11:58 pm 

Joined: Fri Aug 22, 2014 3:54 pm
Posts: 17
I seem to have some sort of issue in rendering the texture. It appears to load the texture properly and it attempts to display it. The only thing I see though is a black texture with a slight alpha channel (See image for more details). I've googled the issue, but many of the results are referring to deprecated OGL functions. The few I found with shaders don't seem to solve the issue.

The image of my program running is here: http://postimg.org/image/wm9sgad6f/

I've attached the relevant code areas. I keep going over them and I can't find the error. I'm hoping another pair of eye could help me out, thanks.

AssetStorage.cpp : http://pastebin.com/XEtBmY0X
Image2d.cpp : http://pastebin.com/BjJW5Agd
Game.cpp : http://pastebin.com/asvM6unq
MkoFileReader.cpp : http://pastebin.com/EagG70qy
basicTexture.vert : http://pastebin.com/xhDkxa3f
basicTexture.frag : http://pastebin.com/CdckUD4u


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Thu Jan 01, 2015 2:17 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
I looked at your source and in the AssetStorage::add() function towards the bottom I noticed your if statement had the argument texture.generateMipMap then you call your glGenerateMipmaps( GL_TEXTURE_2D ); I'm not sure how far along you are but in my source code I have

    if ( texture.generateMipMap && openglVersion.x >=3 ) {
        glGenerateMipmaps( GL_TEXTURE_2D );
    }


However, later on we add a v2.0 to using a second constructor, plus we use the TextureFileReader to create or read in a texture file as well and later we add inheritance into the structure for the Image2d class. Other then that you code for your AssetStorage and Image2D looks okay. So your current code is bound to change as you progress through the videos.

Now in your MkoFileReader::createMkoAsset() you have this:
    float fScale = 1.0f / static_cast<float>(data.scale);
    pAsset = new Image2d(data.width*data.scale,
                                      data.height*data.scale,
                                      static_cast<TextureInfo::FilterQuality>(data.bitfield.filterQuality),                               
                                      data.bitfield.generateMipMap,
                                      strTextureFilename, strId);
    }
    return pAsset;
}


and what I see here is you are not using the scale factor that you calculated above. Here is what mine looks like

      float fScale = 1.0f / static_cast<float>( data.scale );

      pAsset = new Image2d( data.width * fScale,
         data.height * fScale,
         static_cast<TextureInfo::FilterQuality>( data.bitfield.filterQuality ),
         data.bitfield.generateMipMap,
         strTextureFilename,
         strId );
   } else {
      struct ImageData {
         unsigned short usOriginX;
         unsigned short usOriginY;
         unsigned short usWidth;
         unsigned short usHeight;
      } data;

      _fileStream.read( reinterpret_cast<char*>( &data ), sizeof( ImageData ) );
      if ( _fileStream.fail() ) {
         throwError( __FUNCTION__ + std::string( " invalid mko file, missing ImageData" ) );
      }

      pAsset = new Image2d( glm::uvec2( data.usOriginX, data.usOriginY ),
                       glm::uvec2( data.usWidth, data.usHeight ),
                       strTextureFilename, strId );
   }
   return pAsset;
}


I'm not sure but I think the else { section might of been added later on, but your scale factor isn't being updated from what I see, you are still using the existing scale value within the structure and failing to multiply your width and height being passed into the Image2d constructor by the current scales inverse. Other then that the rest of this class method looks good.

Both your shaders appear to be okay.

Finally your game.cpp code seems to be okay.

The one I would try to work on first is fixing the scale factor, then you could try adding in the second argument into the if state for the opengl version. Hope this helps.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Thu Jan 01, 2015 4:04 am 

Joined: Fri Aug 22, 2014 3:54 pm
Posts: 17
Thanks for finding the scaling issue. That was a bug, but it sadly did not fix the issue. It still appears black on screen with the alpha intact. This is truly confusing. I've managed to solve all my issues since the last fiasco where I asked for help, but this one has me stumped once again. Any other idea by chance? Or any idea where in the code it would cause this?


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Thu Jan 01, 2015 8:29 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
What happens when you change the following line of code in your fragment shader:

outFragColor = exColor * texture(texture0Sampler2d, exTextureCoord0);


To

outFragColor = vec4(1,1,1,exColor.a) * texture(texture0Sampler2d, exTextureCoord0);


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Thu Jan 01, 2015 12:30 pm 

Joined: Fri Aug 22, 2014 3:54 pm
Posts: 17
That line fixed it! So the question now becomes, how do I fix the exColor variable to get it displaying properly?

As a temporary fix, I've done this in the render function.

Add the two lines below before rendering...
glm::vec4 v4Color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
_pShaderManager->SetAttribute(A_COLOR, v4Color);


I don't see this in your code in the video though, so is there a default area I might have missed?


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Thu Jan 01, 2015 2:07 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Inside the Image2d constructor you have the following which should be setting the color to white.
_pShaderManager->SetAttribute(A_COLOR, COLOR_WHITE);


Perhaps you have a typo in the definition of COLOR_WHITE?


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Thu Jan 01, 2015 3:52 pm 

Joined: Fri Aug 22, 2014 3:54 pm
Posts: 17
Good call Marek! I had WHITE and BLACK mixed up. My COLOR_BLACK was set as (1.0f,1.0f,1.0f,1.0f) and WHITE was (0.0f,0.0f,0.0f,1.0f).

Thanks for your help.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Sun Apr 12, 2015 8:11 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
In my second pass of this series where I am working in VS2013, I am at the point in this video where you comment out the code that sets the attributes to the ShaderManager within the Image2d class, all of my code appears to be correct, I have check about 4 or 5 times and don't see any apparent errors, however I am getting an exception
 Unhandled exception at 0x01022DC2 in Game_debug.exe: 0xC0000005: Access violation reading location 0x0000004C.
that is breaking in the file xhash.

I set a break point in the Image2d constructor and stepped through and it fails on this line in the constructor
 m_textureInfo = textureFileReader.getOrCreateTextureInfo( filterQuality, generateMipMap, false );
so I stop the debugger set a break point there and step into the code, then within the TextureFileReader class within the getOrCreateTextureInfo() function it fails on this line:
    TextureInfo textureInfo = m_pAssetStorage->getTextureInfo( m_strFilenameWithPath );
I set another break point at this point stop the debugger and run the debugger again and step into this function, and it takes me into AssetStorage's getTextureInfo() and this point it fails on this line of code:
 MapTextureInfos::const_iterator itTexture = m_textureInfos.find( strFilename );
and doesn't seem to return cend() at all so it is not going into the if statement to return a default TextureInfo() object, and because of this the AssetStorage::add() is never getting called. While I was stepping through the debugger within the file xhash right before it throws the error the value that it is looking for does have the correct std::string filename. I do not know if MS changed any kind of implementations within the STL for unordered maps or not from VS2012 to VS2013, but this const iterator is failing on me.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Sun Apr 12, 2015 8:17 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
I went a head and tried something to see what would happen. In the Image2d constructor I commented out these two lines of code:
   // Save TextureID
   TextureFileReader textureFileReader( strTextureFilename );
    m_textureInfo = textureFileReader.getOrCreateTextureInfo( filterQuality, generateMipMap, false );
cleaned my solution, rebuilt and ran the code and now I am getting a 2D white image rendering on the screen. However I still do not know why the const_iterator searching through the map failed, even if it was empty it should still return a const pointer to the end of the map.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Sun Apr 12, 2015 11:26 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
After taking a break from trying to debug this, when I came back to it about an hour or so later, I had discovered something that I had overlooked. When I was following the video for the TextureFileReader class in the header file I had a pointer to our AssetStorage as private member within this class to resolve the underline object in TextureFileReader's .cpp file. Later on in the next video, we added this as a static pointer in the protected section of FileHandler so that all derived classes would have access to it. What was happening here in this situation, the compiler was using the derived member variable instead of the one from the base class. Once I removed the private member variable in the TextureFileReader to the AssetStorage and did clean build without commenting out the two lines of code that I mentioned in the above comment, it now runs correctly without throwing an exception.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 44 - Texture Shader
PostPosted: Mon Apr 13, 2015 1:36 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
I also noticed something else of importance. When I was rendering the two testImage side by side and then scaled them by changing the scale factor to 2 from the original 1; I noticed that the size of my images were not right. At first I thought that maybe my resolution was set different to what you have in the video but it wasn't. It was set at 1024 x 768. I kept hunting and hunting. I went back in the early videos and checked the Engine code for the calculations on the screen size, game pixel size and the setup of the viewport, and everything was good there. Then when I was looking inside the Game constructor I was looking at the calculation that we save into our member variable for the projection matrix and I seen the glm::perspective call. Then it had dawned on me, about a comment that I had made in another post thread on the forums about GLM Math Library with their newer versions forcing radians as opposed to degrees. So this is what your video has using the older GLM library
   const glm::uvec2 gamePixelSize = m_pSettings->getGameSize();
   float fAspectRatio = gamePixelSize.x / static_cast<float>( gamePixelSize.y );
   m_m4Projection = glm::perspective( 45.0f, fAspectRatio, 0.1f, 100.0f );
which is correct, however with GLM 0.9.5.1 they inquire about using their flag to force radians for their math library function calls. In think in version 0.9.5.5 and above this flag is automatically set by default. And in version 0.9.6.0+ all functions that used degrees are deprecated and now use radians. So If you still want to use degrees for angles with GLM 0.9.5.1+ or preferably 0.9.6.2 or 0.9.6.3 then your source should look like this instead:
   const glm::uvec2 gamePixelSize = m_pSettings->getGameSize();
   float fAspectRatio = gamePixelSize.x / static_cast<float>( gamePixelSize.y );
   m_m4Projection = glm::perspective( glm::radians( 45.0f ), fAspectRatio, 0.1f, 100.0f );
I just want to stress this fact so that others do not have rendering versus scaling problems along with rotations when we get deeper into the shader engine series and start to do animations with multiple objects in the game's render window. I would suggest using the newer library but make sure every function call that takes in an angle value for its parameter is either already defined in radians, or simply pass your degree of angle into glm::radians( your angle of degree ) right into your calculations or glm methods.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 12 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron