织梦CMS - 轻松建站从此开始!

罗索

opengl-es 画球

落鹤生 发布于 2012-03-13 11:37 点击:次 
#include PVRShell.h #include PVRShellAPI.h #include math.h #define PI_ 3 . 14159265358979323846 class CLesson2: public PVRShell { float DegToRad( float deg); public : virtual bool InitApplication(); virtual bool InitView(); virtual bool ReleaseView(
TAG:

#include "PVRShell.h" 
#include "PVRShellAPI.h" 
#include <math.h> 
 
#define PI_ 3.14159265358979323846 
 
class CLesson2 : public PVRShell 

    float DegToRad(float deg); 
public
    virtual bool InitApplication(); 
    virtual bool InitView(); 
    virtual bool ReleaseView(); 
    virtual bool QuitApplication(); 
    virtual bool RenderScene(); 
protected
private
}; 
 
bool CLesson2::InitApplication() 

    return true

 
bool CLesson2::QuitApplication() 

    return true

 
//这个函数抠至 gult|es 
void PlotSpherePoints(GLfloat radius, GLint stacks, GLint slices, GLfloat* v, GLfloat* n, GLfloat* t) 

    GLint i, j;  
    GLfloat slicestep, stackstep; 
 
    stackstep = ((GLfloat)PI_) / stacks; 
    slicestep = 2.0f * ((GLfloat)PI_) / slices; 
 
    for (i = 0; i < stacks; ++i)         
    { 
        GLfloat a = i * stackstep; 
        GLfloat b = a + stackstep; 
 
        GLfloat s0 =  (GLfloat)sin(a); 
        GLfloat s1 =  (GLfloat)sin(b); 
 
        GLfloat c0 =  (GLfloat)cos(a); 
        GLfloat c1 =  (GLfloat)cos(b); 
 
        for (j = 0; j <= slices; ++j)         
        { 
            GLfloat c = j * slicestep; 
            GLfloat x = (GLfloat)cos(c); 
            GLfloat y = (GLfloat)sin(c); 
 
            *n = x * s0; 
            *v = *n * radius; 
             
            n++; 
            v++; 
             
            *n = y * s0; 
            *v = *n * radius; 
             
            n++; 
            v++; 
             
            *n = c0; 
            *v = *n * radius; 
 
            n++; 
            v++; 
 
            *n = x * s1; 
            *v = *n * radius; 
 
            n++; 
            v++; 
 
            *n = y * s1; 
            *v = *n * radius; 
 
            n++; 
            v++; 
 
            *n = c1; 
            *v = *n * radius; 
 
            n++; 
            v++; 
 
            *t =  0.0f;t++; 
            *t = 0.0f;t++; 
            *t = 1.0f;t++; 
            *t = 0.0f;t++; 
            *t = 0.5f;t++; 
            *t = -1.0f;t++; 
 
        } 
    } 

 
int iS = 720
GLint slices = 100;//圆的轴数,轴越多圆弧越弯曲 
GLint stacks = 500;//饱和度,值越大,越鼓 
GLfloat radius = 0.4f;//半径 
static GLfloat* v, *n, *t; 
GLuint    m_ui32Texture; 
bool CLesson2::InitView() 

    //球顶点 
    v = (GLfloat*)malloc(stacks*(slices+1)*2*3*sizeof *v); 
    //球法线 
    n = (GLfloat*)malloc(stacks*(slices+1)*2*3*sizeof *n); 
    //球纹理 
    t =  (GLfloat*)malloc(stacks*(slices+1)*2*3*sizeof *n); 
 
    PlotSpherePoints(radius, stacks, slices, v, n,t); 
 
 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    GLfloat position[] = { 0.50.5, -1.00.0 }; 
    glEnable(GL_DEPTH_TEST); 
    glLightfv(GL_LIGHT0, GL_POSITION, position); 
/* 
    GLfloat mat[3] = {0.1745, 0.01175, 0.01175};     
    glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, mat);//环境光 
    mat[0] = 0.04136; mat[1] = 0.04136; mat[2] = 0.61424;     
    glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, mat);//散射光 
    mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959; 
    glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, mat);//聚光 
    glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 0.6*128.0); 
*/ 
    //增加纹理 
    glEnable(GL_TEXTURE_2D); 
    glGenTextures(1, &m_ui32Texture); 
    int g_i32TexSize = 128
    GLuint* pTexData = new GLuint[g_i32TexSize*g_i32TexSize]; 
    for(int i = 0; i < g_i32TexSize; ++i) 
    { 
        for(int j = 0; j < g_i32TexSize; ++j) 
        { 
            // Fills the data with a fancy pattern 
            GLuint col = (255L<<24) + ((255L-j*2)<<16) + ((255L-i)<<8) + (255L-i*2); 
 
            if( ((i*j)/8) % 2 ) 
                col = 0xffff00ff
 
            pTexData[j*g_i32TexSize+i] = col; 
        } 
    } 
 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, g_i32TexSize, g_i32TexSize, 0

, GL_RGBA, GL_UNSIGNED_BYTE, pTexData); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 
 
    return true

 
bool CLesson2::ReleaseView() 

    return true

 
PVRShell * NewDemo(void

    return new CLesson2(); 

 
bool CLesson2::RenderScene() 
{     
    GLint i, triangles;  
 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     
 
 
    glVertexPointer(3, GL_FLOAT, 0, v); 
    glNormalPointer(GL_FLOAT, 0, n); 
    glTexCoordPointer(2,GL_FLOAT,0,t); 
 
    glEnableClientState (GL_VERTEX_ARRAY); 
    glEnableClientState (GL_NORMAL_ARRAY); 
    glEnableClientState (GL_TEXTURE_COORD_ARRAY); 
 
    triangles = (slices + 1) * 2
 
    for(i = 0; i < stacks; i++) 
        glDrawArrays(GL_TRIANGLE_STRIP, i * triangles, triangles); 
 
    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_NORMAL_ARRAY); 
    glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
     
    return true

球一定要有光照,没有光照不叫球。你看太阳能看出是个球么?他更像圆盘,其实他确实是球。光照效果没有把他呈现成球。我还加了纹理效果,当然看官们可以把纹理关闭。

(cr0_3)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201203/15815.html]
本文出处:百度博客 作者:cr0_3 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容