⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texture.cpp

📁 这是在s60第五版上用Open GL开发的软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * ==============================================================================
 *  Name        : Texture.cpp
 *  Part of     : OpenGLEx / Texture
 *
 *  Copyright (c) 2004-2006 Nokia Corporation.
 *  This material, including documentation and any related
 *  computer programs, is protected by copyright controlled by
 *  Nokia Corporation.
 * ==============================================================================
 */

// INCLUDE FILES
#include <e32std.h>
#include "Texture.h"

// MACROS
#define LIGHT_MAX    (1 << 16)
#define LIGHTCOLOR(r, g, b, a)       \
       (GLfixed)(r * LIGHT_MAX),     \
       (GLfixed)(g * LIGHT_MAX),     \
       (GLfixed)(b * LIGHT_MAX),     \
       (GLfixed)(a * LIGHT_MAX)

/* Define global ambient light. */
static const GLfixed globalAmbient[4]      = { LIGHTCOLOR(0.0, 0.0, 0.0, 1.0) };

/* Define lamp parameters. */
static const GLfixed lightDiffuseLamp[4]   = { LIGHTCOLOR(1.0, 1.0, 1.0, 1.0) };
static const GLfixed lightAmbientLamp[4]   = { LIGHTCOLOR(0.3, 0.3, 0.3, 1.0) };
static const GLfixed lightSpecularLamp[4]  = { LIGHTCOLOR(0.5, 0.5, 0.5, 1.0) };
static const GLfixed lightPositionLamp[4]  = { 0, 0, 10, 0 };

/* Define vertice coordinates for the cube
   Duplication of vertices needed for texturing every surface of the cube */
static const GLbyte vertices[24 * 3] =
{
    /* top */
    -1,  1,  1,
    1,  1,  1,
    1, -1,  1,
    -1, -1,  1,

    /* front */
    1,  1,  1,
    1,  1, -1,
    1, -1, -1,
    1, -1,  1,

    /* right */
    -1,  1,  1,
    -1,  1, -1,
    1,  1, -1,
    1,  1,  1,

    /* left */
    1, -1,  1,
    1, -1, -1,
    -1, -1, -1,
    -1, -1,  1,

    /* back */
    -1, -1,  1,
    -1, -1, -1,
    -1,  1, -1,
    -1,  1,  1,

    /* bottom */
    -1,  1, -1,
    1,  1, -1,
    1, -1, -1,
    -1, -1, -1
};

/* Define normals for the cube */
static const GLbyte normals[24 * 3] =
{
    /* top */
    0,0,1,
    0,0,1,
    0,0,1,
    0,0,1,

    /* front */
    1,0,0,
    1,0,0,
    1,0,0,
    1,0,0,

    /* right */
    0,1,0,
    0,1,0,
    0,1,0,
    0,1,0,

    /* left */
    0,-1,0,
    0,-1,0,
    0,-1,0,
    0,-1,0,

    /* back */
    -1,0,0,
    -1,0,0,
    -1,0,0,
    -1,0,0,

    /* bottom */
    0,0,-1,
    0,0,-1,
    0,0,-1,
    0,0,-1
};

/* Define indices for drawing the triangles ( 5 sides of the cube )
   Nasa Hubble texture is drawn on these sides. */
static const GLubyte triangles[10 * 3] =
{
    /* top */
     1,0,3,
     1,3,2,

     /* front */
     5,4,7,
     5,7,6,

     /* right */
     9,8,11,
     9,11,10,

     /* left */
     13,12,15,
     13,15,14,

     /* back */
     17,16,19,
     17,19,18
};


/* The 6th side of the cube.
   ogles.jpg texture is drawn on this side. */
static const GLubyte triangles2[2 * 3] =
{
     1,0,3,
     1,3,2,
};

/* The 6th side of the cube.
   ogles.jpg texture is drawn on this side. */
static const GLubyte triangles3[2 * 3] =
{
     5,4,7,
     5,7,6,
};


/* The 6th side of the cube.
   ogles.jpg texture is drawn on this side. */
static const GLubyte triangles4[2 * 3] =
{
     /* back */
      17,19,18,
     17,16,19

};

/* The 6th side of the cube.
   ogles.jpg texture is drawn on this side. */
static const GLubyte triangles5[2 * 3] =
{
     /* back */
    21,22,23,
    21,23,20
};

/* Macro for changing the input texture coordinate values from
   GLubyte [0,255] to GLbyte [-128,127]. See more info below. */
#define tex(u,v) (GLbyte)( (u) - 128 ) , (GLbyte)( (v) - 128 )


/* Input texture coordinates for each of the object vertices.
   The coordinates are given in GLbyte [-128,127] format.
   Since the texture picture coordinates are between values
   [0,255] for both width and height, we have defined a macro
   to change the input coordinates into a appropriate form.
   It is easier to think the texture coordinates as corresponding
   image pixel coordinates. This alone is not enough because
   if texture coordinates are not given between values [0,1],
   texture wrapping will occur. Therefore we need to
   scale the texture coordinates with appropriate texture matrix,
   which is defined in AppInit(). */
static const GLbyte nokTexCoords[24 * 2] =
{
    /* top, whole texture nasa_hubble.h */
    tex(0,0),
    tex(255,0),
    tex(255,255),
    tex(0,255),

    tex(0,0),
    tex(255,0),
    tex(255,255),
    tex(0,255),
        
    tex(0,0),
    tex(255,0),
    tex(255,255),
    tex(0,255),
    
    tex(0,0),
    tex(255,0),
    tex(255,255),
    tex(0,255),
    
    tex(0,0),
    tex(255,0),
    tex(255,255),
    tex(0,255),
    
    tex(0,0),
    tex(255,0),
    tex(255,255),
    tex(0,255),

};


// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CTexture::CTexture
// C++ default constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
//
CTexture::CTexture( TUint aWidth, TUint aHeight ): CFiniteStateMachine()
{
    iScreenWidth  = aWidth;
    iScreenHeight = aHeight;
}


// -----------------------------------------------------------------------------
// CTexture::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTexture::ConstructL( void )
{
}


// -----------------------------------------------------------------------------
// CTexture::NewL
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTexture* CTexture::NewL( TUint aWidth, TUint aHeight )
{
    /* Symbian 2-phase constructor. Calls both the default
       C++ constructor and Symbian ConstructL methods */
    CTexture* self = new (ELeave) CTexture( aWidth, aHeight );
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop();

    return self;
}


// Destructor
CTexture::~CTexture()
{
}


// -----------------------------------------------------------------------------
// CTexture::AppInit
// OpenGL ES initialization code. Set the required OpenGL ES state
// -----------------------------------------------------------------------------
//
void CTexture::AppInitL()
{
	  // Construct a texture manager that uses the application's private
	  // directory as the location for all textures.
    iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight,
                                              FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR,
                                              this );

    // Initialize viewport and projection.
		SetScreenSize( iScreenWidth, iScreenHeight );

    /* Set the screen background color. */
    glClearColor( 0.f, 0.f, 0.f, 1.f );

    /* Enable back face culling, texturing, and normalization. */
    glEnable( GL_CULL_FACE  );
    glEnable( GL_TEXTURE_2D );
    glEnable( GL_NORMALIZE  );

    /* Initialize appropriate texture matrix. First we have to translate the
       input texture coordinate values to be within a range of [0,255]. Hence
       we translate the x- and y-coordinate values by 128. Recall that the
       values in nokTexCoords are between [-128,127], now they are in a range
       of [0,255]. After that we scale the values by 1/255 to make the values
       to be in range [0,1]. */
    glMatrixMode( GL_TEXTURE );
    glLoadIdentity();
    glScalef(     1.0f/255.0f, 1.0f/255.0f, 1.0f );
    glTranslatef( 128.0f,      128.0f,      0.0f );

    /* Remember to change the matrix mode to modelview. */
    glMatrixMode( GL_MODELVIEW );

    /* Enable vertex and texture arrays. */
    glEnableClientState( GL_VERTEX_ARRAY        );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -