Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Problems about the construction of normalization cubemap

  1. #1
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    2

    Question Problems about the construction of normalization cubemap

    Hi, guys
    I want to implement a normalization cubemap which transforms the arbitary directional vectors into its normal form, and
    I have studied the following reference sources:

    paul's projects:
    http://www.paulsprojects.net/tutoria...implebump.html

    openGL cubemap specification:
    http://oss.sgi.com/projects/ogl-samp...e_cube_map.txt

    The problem is that I don't understand the following code snippet from paul's project for the construction of
    normalization cubemap:
    Code :
    unsigned char * data=new unsigned char[32*32*3];
    	if(!data)
    	{
    		printf("Unable to allocate memory for texture data for cube map\n");
    		return false;
    	}
     
    	//some useful variables
    	int size=32;
    	float offset=0.5f;
    	float halfSize=16.0f;
    	VECTOR3D tempVector;
    	unsigned char * bytePtr;
     
    	//positive x
    	bytePtr=data;
     
    	for(int j=0; j<size; j++)
    	{
    		for(int i=0; i<size; i++)
    		{
    			tempVector.SetX(halfSize);
    			tempVector.SetY(-(j+offset-halfSize));
    			tempVector.SetZ(-(i+offset-halfSize));
     
    			tempVector.Normalize();
    			tempVector.PackTo01();
     
    			bytePtr[0]=(unsigned char)(tempVector.GetX()*255);
    			bytePtr[1]=(unsigned char)(tempVector.GetY()*255);
    			bytePtr[2]=(unsigned char)(tempVector.GetZ()*255);
     
    			bytePtr+=3;
    		}
    	}
    	glTexImage2D(	GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
    					0, GL_RGBA8, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

    I have two questions here:
    1. We all know the first element of array which is supplied as texture data corresponds to the lower-left corner of texture image, but in this
    case the for loop just start by adding the directional vector with x > 0 , y > 0 and z > 0 to the first element of array, and the direction pointed
    by that vector is not the lower-left corner if when we think the face of cubemap as texture image and view it from positive x-axis to the
    negative x-axis. Is this code error or do I misunderstand something?

    2. Why do we need to add the offset to change the components of directional vector?

    I got stuck for this problem about two days, any suggestions or reference source will be appreciated.
    Last edited by Moonloki; 06-16-2013 at 05:49 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Moonloki View Post
    1. We all know the first element of array which is supplied as texture data corresponds to the lower-left corner of texture image,
    For a 2D texture, the first element is the texel nearest (0,0). Whether that's the "lower-left" corner is a matter of interpretation.
    For the positive-X face of a cube map texture, the first element is the texel nearest the (1,1,1) corner of the cube. Also, the s coordinate increases along the negative Z axis while the t coordinate increases along the negative Y axis. The mappings of the cube map faces are given in the specification of the ARB_texture_cube_map extension which you linked to:
    Code :
          major axis
          direction     target			       sc     tc    ma
          ----------    -------------------------------    ---    ---   ---
           +rx	    TEXTURE_CUBE_MAP_POSITIVE_X_ARB    -rz    -ry   rx
           -rx	    TEXTURE_CUBE_MAP_NEGATIVE_X_ARB    +rz    -ry   rx
           +ry	    TEXTURE_CUBE_MAP_POSITIVE_Y_ARB    +rx    +rz   ry
           -ry	    TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB    +rx    -rz   ry
           +rz	    TEXTURE_CUBE_MAP_POSITIVE_Z_ARB    +rx    -ry   rz
           -rz	    TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB    -rx    -ry   rz
    Quote Originally Posted by Moonloki View Post
    2. Why do we need to add the offset to change the components of directional vector?
    The offset is half the size of a texel, so adding the offset results in the vector containing the position of the texel's centre rather than one of its corners.

  3. #3
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    2
    Quote Originally Posted by GClements View Post
    For a 2D texture, the first element is the texel nearest (0,0). Whether that's the "lower-left" corner is a matter of interpretation.
    For the positive-X face of a cube map texture, the first element is the texel nearest the (1,1,1) corner of the cube. Also, the s coordinate increases along the negative Z axis while the t coordinate increases along the negative Y axis. The mappings of the cube map faces are given in the specification of the ARB_texture_cube_map extension which you linked to:
    Code :
          major axis
          direction     target			       sc     tc    ma
          ----------    -------------------------------    ---    ---   ---
           +rx	    TEXTURE_CUBE_MAP_POSITIVE_X_ARB    -rz    -ry   rx
           -rx	    TEXTURE_CUBE_MAP_NEGATIVE_X_ARB    +rz    -ry   rx
           +ry	    TEXTURE_CUBE_MAP_POSITIVE_Y_ARB    +rx    +rz   ry
           -ry	    TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB    +rx    -rz   ry
           +rz	    TEXTURE_CUBE_MAP_POSITIVE_Z_ARB    +rx    -ry   rz
           -rz	    TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB    -rx    -ry   rz

    The offset is half the size of a texel, so adding the offset results in the vector containing the position of the texel's centre rather than one of its corners.
    Wow, thanks for your reply, you are so clever.
    By the way, may I ask you another question, that is, how did you acquire such kind of knowledge?
    Which books or papers have you read in order to gain this kind of knowledge?
    No other meaning, I Just felt curious, because I have finished the study of openGL red book and didn't gain this kind of knowledge.

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Moonloki View Post
    Which books or papers have you read in order to gain this kind of knowledge?
    I started with the red book, but once you have a reasonable overview, the specifications are the best source for specific details.

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    Not directly related to the topic, but I think it worth mentioning.

    Normalization cube maps were very useful in the past when GPUs either didn't have instructions that could perform the normalization of a vector (pre DX9 hardware) or on GPUs that had instructions for it, yet they didn't have enough ALU power thus using normalization instructions was expensive (early DX9 hardware), but nowadays it doesn't worth using normalization cube maps, but instead you should simply normalize vectors using ALU (e.g. using the normalize built-in function of GLSL).

    This is because in the last ten years GPUs continued to increase their ALU power by a considerable amount and nowadays memory bandwidth and memory access latency is way more of a bottleneck than ALU operations. In fact, usually you can perform tens if not hundreds of ALU instructions at the same cost of a texture fetch, and while latecy hiding mechanisms do improve this ratio, it can definitely be concluded that normalization instructions will be always faster than using a normalization cube map. Not to mention that using ALU based normalization does not require additional memory and provides good precision without requiring to create large normalization cube maps with floating point components.
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog: http://www.rastergrid.com/blog/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •