首先申明一点,我利用的是ndk进行开发(做毕业设计),目前利用ndk调用opengl es进行基本物体的绘制
因此,所绘制的代码采用c语言进行编写,已经把正方体,曲线,圆的绘制函数编写出来了,球的比较麻烦,最近参考了很多资料,才弄出来,
发个帖子,希望共同学习,不会的可以借鉴
好了,步入正文
绘制球的基本思路,我参考了外国网站上一篇文章,希望大家能阅读一下思想很好。(http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/)
我用的就是第一个思路,经纬分割吧。把球按经纬分割成小正方形,每个正方形按四个顶点进行绘制
c语言实现的代码如下:
/*
Create a unit sphere centered at the origin
This code illustrates the concept rather than implements it efficiently
It is called with two arguments, the theta and phi angle increments in degrees
Note that at the poles only 3 vertex facet result
while the rest of the sphere has 4 point facets
*/
typedef struct {
double x,y,z;
} XYZ;
void CreateUnitSphere(int dtheta,int dphi)
{
int n;
int theta,phi;
XYZ p[4];
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
n = 0;
p[n].x = cos(theta*DTOR) * cos(phi*DTOR);
p[n].y = cos(theta*DTOR) * sin(phi*DTOR);
p[n].z = sin(theta*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
if (theta > -90 && theta < 90) {
p[n].x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin(theta*DTOR);
n++;
}
/* Do something with the n vertex facet p */
}
}
}
这是非常关键的函数实现代码。珍藏!
然后就是绘制。我直接把jni.c端的代码全部发上了,如下:
#include <jni.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GLES/gl.h>
typedef unsigned char byte;
typedef struct {
GLfloat x,y,z;
} XYZ;
float rotateQuad;
#define PI 3.14159265
#define DTOR PI/180
static byte indices[8]={0,1,1,2,2,3,3,0}; //索引数组
void CreateUnitSphere(int dtheta,int dphi)
{
int n;
int theta,phi;
XYZ p[4];
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
n = 0;
p[n].x = cos(theta*DTOR) * cos(phi*DTOR);
p[n].y = cos(theta*DTOR) * sin(phi*DTOR);
p[n].z = sin(theta*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
p[n].x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin((theta+dtheta)*DTOR);
n++;
if (theta >=-90 && theta <= 90) {
p[n].x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
p[n].y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
p[n].z = sin(theta*DTOR);
n++;
}
/* Do something with the n vertex facet p */
glVertexPointer(3, GL_FLOAT, 0, p);
glDrawElements(GL_LINES, 8, GL_UNSIGNED_BYTE, indices);
}
}
}
void Java_com_ldj_hellondk_NdkGLRenderer_onNdkSurfaceCreated (JNIEnv* env, jobject obj)
{
// 启用阴影平滑
glShadeModel(GL_SMOOTH);
// 黑色背景
glClearColor(0, 0, 0, 0);
// 设置深度缓存
glClearDepthf(1.0f);
// 启用深度测试
glEnable(GL_DEPTH_TEST);
// 所作深度测试的类型
glDepthFunc(GL_LEQUAL);
// 告诉系统对透视进行修正
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
}
void Java_com_ldj_hellondk_NdkGLRenderer_onNdkSurfaceChanged (JNIEnv* env, jobject obj, jint width, jint height)
{
//图形最终显示到屏幕的区域的位置、长和宽
glViewport (0,0,width,height);
//指定矩阵
glMatrixMode (GL_PROJECTION);
//将当前的矩阵设置为glMatrixMode指定的矩阵
glLoadIdentity ();
glOrthof (-2, 2, -2, 2, -2, 2);
}
void Java_com_ldj_hellondk_NdkGLRenderer_onNdkDrawFrame (JNIEnv* env, jobject obj)
{
//启用顶点设置功能,之后必须要关闭功能
glEnableClientState (GL_VERTEX_ARRAY);
//清屏
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glFrontFace(GL_CW);
glRotatef(rotateQuad, 1.0f, 0.5f, 0.0f);//旋转效果
CreateUnitSphere(10,10);
//关闭顶点设置功能
glDisableClientState(GL_VERTEX_ARRAY);
rotateQuad -= 0.5f;
}
ok,这就绘制成功了,效果图如下,绘制实体的话,改一下drawelement的参数就行了
可以去下载源代码,给个链接吧
(http://download.csdn.net/source/3219224)
我是菜鸟,最近做毕设做的郁闷,查资料难啊,所以共享自己的东西,希望研究这方面的人和我交流学习!