OpenGL ES画球
#include"PVRShell.h"
#include"PVRShellAPI.h"
#include
#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.5, 0.5, -1.0, 0.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);
glV ertexPointer(3, GL_FLOA T, 0, v);
glNormalPointer(GL_FLOA T, 0, n);
glTexCoordPointer(2,GL_FLOA T,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;
}
球一定要有光照,没有光照不叫球。你看太阳能看出是个球么?他更像圆盘,其实他确实是球。光照效果没有把他呈现成球。我还加了纹理效果,当然看官们可以把纹理关闭。