It is currently Mon Apr 24, 2017 2:02 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 16 posts ] 
Author Message
 Post subject: Math VMK 7 - Dot Product
PostPosted: Sun Oct 29, 2006 7:08 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
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.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 19 - Node Texture
PostPosted: Sun Oct 29, 2006 7:49 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
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.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 19 - Node Texture
PostPosted: Thu Nov 02, 2006 7:50 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
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.

Offline
 Profile  
 
 Post subject: Re: GameDev VMK 19 - Node Texture
PostPosted: Fri Jun 22, 2007 1:10 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
I just discovered a memory leak in the Scene::LoadTexture function.

To fix it, you will need to add:
//clean up memory allocated to local copy of szFilename
SAFE_DELETE_ARRAY(szFile);


after this:
m_mpTextures[szFile] = pTexture;


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

.... 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.

Offline
 Profile  
 
 Post subject: 'map' : is not a member of 'std'
PostPosted: Sun Apr 06, 2008 4:12 pm 

Joined: Fri Jan 04, 2008 2:11 pm
Posts: 72
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'


Offline
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 3:26 am 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Did you include the header file?

maps are defined in <map>


Offline
 Profile  
 
 Post subject: So much for Cristos © Desperado ©
PostPosted: Mon Sep 22, 2008 1:22 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
Continuing to climb Game Engine "mount improbable" I've found myself observing the next excerpt from GameDev-VMK019A-NodeTexture - NodeTexture4.swf
NodeTexture::~NodeTexture() {
    if (m_pImageData) {
        SAFE_DELETE_ARRAY(m_pImageData);
    }
}

Then, the definition of SAFE_DELETE_ARRAY is:
#define SAFE_DELETE_ARRAY(p)        { if(p) { delete [] (p); (p) = NULL; } }

Hence, if we make the macro substitution, we will get:
...
if (m_pImageData) {
  if (m_pImageData) {
    if (m_pImageData) { // ...inside "operator delete [] ()"
        ...
    }
  }
}

Strike me blind, Marek, if you don't overdo playing for safety. :D Don't be offended! ;)


Offline
 Profile  
 
 Post subject: Re: So much for Cristos © Desperado ©
PostPosted: Mon Sep 22, 2008 2:10 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
BugHunter wrote:
Strike me blind, Marek, if you don't overdo playing for safety. :D Don't be offended! ;)


:D 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. :oops: Thanks for pointing it out.


Offline
 Profile  
 
 Post subject: The the fastest way to check that the number is a power of 2
PostPosted: Mon Nov 03, 2008 3:48 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
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:
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.
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):
    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:
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):
    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).

// 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

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


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2008 3:05 pm 

Joined: Fri Oct 12, 2007 4:41 pm
Posts: 24
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?


ImageImage


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2008 4:31 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
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.


Offline
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 7:52 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
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.


Offline
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 9:16 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
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.


Offline
 Profile  
 
 Post subject:
PostPosted: Sun Oct 04, 2009 12:35 pm 

Joined: Fri Sep 04, 2009 3:24 am
Posts: 30
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.


Offline
 Profile  
 
 Post subject:
PostPosted: Sun Oct 04, 2009 2:50 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
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.


Offline
 Profile  
 
 Post subject:
PostPosted: Sun May 23, 2010 1:45 pm 

Joined: Sun Apr 18, 2010 11:46 am
Posts: 8
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.


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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