Return back to marek-knows.com Member Community
Game Development, 3D Modeling, Art, Sound Effects and more


GameDev VMK 19 - Node Texture

 
Post new topic   Reply to topic    Member Community Forum Index -> OpenGL Game Engine VMK
View previous topic :: View next topic  
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Sun Oct 29, 2006 7:08 am    Post subject: Math VMK 7 - Dot Product Reply with quote Back to top

The NodeTexture class is used to load a TGA file from the hard drive into memory. The TGA file can then be used as a texture on any object in the game engine.
 
View user's profile Send private message
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Sun Oct 29, 2006 7:49 am    Post subject: Re: GameDev VMK 19 - Node Texture Reply with quote Back to top

After uploading VMK 19c I noticed that there are some audio echo problems in the VMK. I had many problems creating this video because the file size is so large. I hope everyone can still understand the video.
 
View user's profile Send private message
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Thu Nov 02, 2006 7:50 am    Post subject: Re: GameDev VMK 19 - Node Texture Reply with quote Back to top

I found a bad bug in the code when using the std::map that stores all the textures in the Scene class. You will only see this bug if you end up using SelectTextureID. If you only use SelectTexture then everything will be okay.

I posted VMK19D to show the corrections needed to get rid of the bug.


Last edited by Marek on Mon Apr 07, 2008 6:34 pm; edited 2 times in total
 
View user's profile Send private message
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Fri Jun 22, 2007 1:10 pm    Post subject: Re: GameDev VMK 19 - Node Texture Reply with quote Back to top

I just discovered a memory leak in the Scene::LoadTexture function.

To fix it, you will need to add:
Code:

//clean up memory allocated to local copy of szFilename
SAFE_DELETE_ARRAY(szFile);


after this:
Code:

m_mpTextures[szFile] = pTexture;


Your Scene::LoadTexture code should now look like this:

Code:

.... more stuff above ....

//add this texture to map
m_mpTextures[szFile] = pTexture;

//clean up memory allocated to local copy of szFilename
SAFE_DELETE_ARRAY(szFile);

//get return value   
uiReturn = pTexture->m_uiID;   

... more stuff below ....


Last edited by Marek on Sun Apr 13, 2008 3:59 pm; edited 4 times in total
 
View user's profile Send private message
ir2pi



Joined: 04 Jan 2008
Posts: 72

 PostPosted: Sun Apr 06, 2008 4:12 pm    Post subject: 'map' : is not a member of 'std' Reply with quote Back to top

i'm using VS 2005 pro and std::map doesn't seem to be available...std::vector works but std::map gives the error:

..\scenelib\scene.h(75) : error C2039: 'map' : is not a member of 'std'
 
View user's profile Send private message
codeslasher



Joined: 23 Jun 2007
Posts: 144

 PostPosted: Mon Apr 07, 2008 3:26 am    Post subject: Reply with quote Back to top

Did you include the header file?

maps are defined in <map>
 
View user's profile Send private message Visit poster's website
BugHunter



Joined: 06 Aug 2008
Posts: 180
Location: Russia

 PostPosted: Mon Sep 22, 2008 1:22 pm    Post subject: So much for Cristos © Desperado © Reply with quote Back to top

Continuing to climb Game Engine "mount improbable" I've found myself observing the next excerpt from GameDev-VMK019A-NodeTexture - NodeTexture4.swf
Code:

NodeTexture::~NodeTexture() {
    if (m_pImageData) {
        SAFE_DELETE_ARRAY(m_pImageData);
    }
}

Then, the definition of SAFE_DELETE_ARRAY is:
Code:

#define SAFE_DELETE_ARRAY(p)        { if(p) { delete [] (p); (p) = NULL; } }

Hence, if we make the macro substitution, we will get:
Code:

...
if (m_pImageData) {
  if (m_pImageData) {
    if (m_pImageData) { // ...inside "operator delete [] ()"
        ...
    }
  }
}

Strike me blind, Marek, if you don't overdo playing for safety. Very Happy Don't be offended! Wink
 
View user's profile Send private message
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Mon Sep 22, 2008 2:10 pm    Post subject: Re: So much for Cristos © Desperado © Reply with quote Back to top

BugHunter wrote:

Strike me blind, Marek, if you don't overdo playing for safety. Very Happy Don't be offended! Wink


Very Happy None taken. The code in the videos come from a number of sources so sometimes I forget the exact implementation that I am working with and repetitive code like this creeps into the videos. Embarassed Thanks for pointing it out.
 
View user's profile Send private message
BugHunter



Joined: 06 Aug 2008
Posts: 180
Location: Russia

 PostPosted: Mon Nov 03, 2008 3:48 pm    Post subject: The the fastest way to check that the number is a power of 2 Reply with quote Back to top

Preamble.
OpenGL glTexImage2D function works only with the images, whose width & height is the power of two (some extensions however make it possible to work with almost arbitrary sizes). That's why the image loader (e.g. TGA) have to check the size of a texture.

The originally proposed way, with some modifications, is:
Code:

inline bool IsPower2( UINT32 value )
{
    if (0x8000000 < value) return false; // check top limit - prevents infinite looping

    UINT32 pow2 = 1;
    while( pow2 < value )
        pow2 <<= 1;
    return ( pow2 == value );
}


Code.
There is however a faster solution (sadly not mine). The idea is based on two simple facts:
1) if x is power of two it's binary representation has only one bit = 1, namely the bit in the position log2( x ) - 1.
2) x is power of two if and only if 0 == x & (x - 1).
For example:
x = 4 = 0100, x - 1 = 3 = 0011. 4 & 3 = 0100 & 0011 = 0000.
x = 5 = 0101, x - 1 = 4 = 0100. 5 & 4 = 0101 & 0100 = 0100,
etc.
Code:

inline bool IsPowerOfTwo( const unsigned x ) // Beware: this does not work for 0.
{
    return 0 == (x & (x - 1));
}

This algo is superb: no loops, platform independence, it accepts any integer of any size (32, 64 etc).
VC++ generates for this function the swiftest inline code (just two op-codes):
Code:

    lea     eax, DWORD PTR [edx-1] // x in edx, eax = x - 1
    test    eax, edx               // now result is in ZF


Later I've noticed, that this code does not work properly for x = 0 (obviously, 0 is not a power of two). So, my improvement was:
Code:

inline bool IsPowerOfTwoStrict( const unsigned x ) // 0 is not a power of 2
{
    return x && !(x & (x - 1));
}

Fortunately for extra condition the VC2008 generates only two additional op-codes (bare minimum):
Code:

    test    edx, edx                 // is x == 0 ?
    je      some_label               // jump if zero
    lea     eax, DWORD PTR [edx-1]
    test    eax, edx



OK, guys, I fully aware that an optimization of rarely used code is usually a waste of time. So, consider these exercises only as a sort of self training.
From the other side, this function can be moved in the general purpose library, thus it should be optimal.

Below is a garbage, that I've produced in attempts to find the best solution. It can be curious. In particular, I exploited x86 instruction: BSF (Bit Scan Forward) and BSR (reverse).

Code:

// GeUtility.h

//------------------------------------------------------------------------------
// Check whether the integer is power of 2
//------------------------------------------------------------------------------

inline bool IsPower2( const unsigned int x )
{
    return x && !(x & (x - 1));
}

/*******************************************************************************

#ifdef GAME_ENGINE_USE_ASM_X86

inline
__declspec( naked ) // suppress compiler from prolog & epilog code generation
bool
__cdecl
IsPower2(
    UINT32 value
    )
{
    __asm
    {
        //
        // prolog
        //
        push    ebp
        mov     ebp, esp
        push    ebx
        push    ecx
        //
        // body
        //
        mov     eax, value
        bsr     ecx, eax    // find leftmost (L) nonzero bit
        jz      END         // if value = 0 then return (eax = value = 0)
        bsf     ebx, eax    // find rightmost (R) nonzero bit
        cmp     ebx, ecx    // if L == R then value is power of 2
        sete    al          // if L == R eax = 1 otherwise eax = 0
    END:
        //
        // epilog
        //
        pop     ecx
        pop     ebx
        mov     esp, ebp
        pop     ebp
        ret                 //  return - result in AL
    }
}

//
// "in_uint32_value" must be local value of type "UINT32"
// "out_bool_result" must be local value of type "bool"
//
#define IS_POWER_TWO( in_uint32_value, out_bool_result ) \
    __asm   push    eax                     \
    __asm   push    ecx                     \
    __asm   xor     eax, eax                \
    __asm   mov     out_bool_result, al     \
    __asm   bsr     ecx, in_uint32_value    \
    __asm   jz      end                     \
    __asm   bsf     eax, in_uint32_value    \
    __asm   cmp     eax, ecx                \
    __asm   sete    out_bool_result         \
    __asm   end:                            \
    __asm   pop     ecx                     \
    __asm   pop     eax                     \



// inline bool IsPower2( UINT32 value )
// {
//     bool result;
//     __asm {
//         xor     ecx, ecx
//         mov     eax, value
//         xor     edx, edx
//         bsr     ecx, eax // find the first 1 from the right, so in ecx is exponent
//         bts     edx, ecx // edx = pow of 2
//         cmp     edx, eax
//         sete    result // result = 1 if edx == eax
//     }
//     return result;
// }

#else // !GAME_ENGINE_USE_ASM_X86

inline bool IsPower2( UINT32 value )
{
    if (0x8000000 < value) return false; // check top limit

    UINT32 pow2 = 1;
    while( pow2 < value )
        pow2 <<= 1;
    return ( pow2 == value );
}

inline void IS_POWER_TWO( UINT32 in_value, bool & out_bool_result )
{
    out_bool_result = IsPower2( in_value );
};

#endif

*******************************************************************************/
 
View user's profile Send private message
endasil



Joined: 12 Oct 2007
Posts: 25

 PostPosted: Fri Nov 14, 2008 3:05 pm    Post subject: Reply with quote Back to top

I get some ugly distortions when i change to another image to use for the ground. How would i go around this problem if i still want to tile images and not simply have one large image in the size of the terrain?


 
View user's profile Send private message
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Fri Nov 14, 2008 4:31 pm    Post subject: Reply with quote Back to top

how many divisions are you using for your ground terrain?

You've got two things to play with in the FlatGrid class. You have the size (width & depth) and you also have the number of divisions in X and Z. If you have to many division then your texture will tile but it will be crunched to closely together. To fix this you would need to enlarge the grid, or decrease the number of divisions.
 
View user's profile Send private message
skilz80



Joined: 16 Aug 2008
Posts: 58

 PostPosted: Tue Nov 25, 2008 7:52 am    Post subject: Reply with quote Back to top

When rendering textures: I keep getting a bug. When I render the ground with clovers it covers the houses. But when I change the ground to ColorTest it works fine. ??? I checked the code and I don't know what is going on.
 
View user's profile Send private message
skilz80



Joined: 16 Aug 2008
Posts: 58

 PostPosted: Tue Nov 25, 2008 9:16 am    Post subject: Reply with quote Back to top

I noticed int the NodeTexture.cpp file Under the MipMap And Filtering section that if you use GL_LINEAR for the GL_TEXTURE_MAG_FILTER
everything works fine. If you use GL_NEAREST, the colvers.tga file covers the houses, and you can not use both house.tga files at the same time.
This took me a while to figure it out, but I hope it helps others in the future so they don't have to spend hours figuring out what is going wrong like I did.
 
View user's profile Send private message
Groggy



Joined: 04 Sep 2009
Posts: 18

 PostPosted: Sun Oct 04, 2009 12:35 pm    Post subject: Reply with quote Back to top

A couple of questions:

1). Why was tga chosen? Was it arbitrary or is it easier to work with than other formats? Works better with games?

2). Why unsigned int variables?

Thanks for any info.
 
View user's profile Send private message
Marek
Site Admin


Joined: 11 Feb 2007
Posts: 515
Location: Ontario Canada

 PostPosted: Sun Oct 04, 2009 2:50 pm    Post subject: Reply with quote Back to top

I'm using TGA files because the file format specification is well documented here and creating a TGA file loader is very easy (no 3rd party libraries are required). TGA files also support an alpha channel which is required when making sprites or certain textures for games.

Signed integers are not required since we know all the values are supposed to be positive.
 
View user's profile Send private message
AdamD



Joined: 18 Apr 2010
Posts: 8

 PostPosted: Sun May 23, 2010 1:45 pm    Post subject: Reply with quote Back to top

Well, its been a while since I've asked a question, but all good streaks must end.

When I compile my project at the end of 19a, I get 108 errors starting with:

"error C2146: syntax error : missing ';' before identifier 'hWnd'"

This is in the core.h file, and all I added was the SAFE_DELETE_ARRAY. I have no idea why this is happening. I didn't modify WindowsParameters at all. Why is this happening now? I'm truly stumped. I know its probably just something stupid.


And one other thing, this isn't an immediate problem, but the program won't run on other computers. Is that expected? When I try to, it gives me the message "This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix the problem."

EDIT: Ok, I've kind of fixed it. I'm guessing it was just compiling in the wrong order, or I forgot to include something somewhere. I added an "#include "stdafx.h"" in the core.h (it hadn't compiled windows.h yet (where HWND is defined)). Its just weird because I don't remember editing the file at all, so I don't know what screwed it up. But oh well, it works now.
 
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Member Community Forum Index -> OpenGL Game Engine VMK All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum