当前位置:服务支持 >  软件文章 >  OpenGL计算机图形学相关算法收集与整理资源分享

OpenGL计算机图形学相关算法收集与整理资源分享

阅读数 7
点赞 0
article_banner

LangFox
球体生成算法,King_jinjing提供。


int dtheta = 15; //SKYBOX球体的尺度
int dphi = 15; //SKYBOX球体的尺度
int radius = 2; //SKYBOX球体的半径


int N=0;
for( int phi=0; phi <= 180 - dphi; phi += (int)dphi )//圆球体
{
for( int theta=0; theta <= 360 - dtheta; theta += (int)dtheta)
{
pVertices[N].position = D3DXVECTOR3 (
radius * sinf(phi*(D3DX_PI/180)) * cosf((D3DX_PI/180)*theta),
radius * sinf(phi*(D3DX_PI/180)) * sinf((D3DX_PI/180)*theta),
radius * cosf(phi*(D3DX_PI/180))
);
pVertices[N].color = 0x00ff0000;
N++;


pVertices[N].position = D3DXVECTOR3 (
radius * sinf((phi+dphi)*(D3DX_PI/180)) * cosf(theta*(D3DX_PI/180)),
radius * sinf((phi+dphi)*(D3DX_PI/180)) * sinf(theta*(D3DX_PI/180)),
radius * cosf((phi+dphi)*(D3DX_PI/180))
);
pVertices[N].color = 0xffffffff;
N++;


pVertices[N].position = D3DXVECTOR3 (
radius * sinf((D3DX_PI/180)*phi) * cosf((D3DX_PI/180)*(theta+dtheta)),
radius * sinf((D3DX_PI/180)*phi) * sinf((D3DX_PI/180)*(theta+dtheta)),
radius * cosf((D3DX_PI/180)*phi)
);
pVertices[N].color = 0xffffffff;
N++;


if (phi > -180 && phi < 180)//圆球体
{
pVertices[N].position = D3DXVECTOR3 (
radius * sinf((phi+dphi)*(D3DX_PI/180)) * cosf((D3DX_PI/180)*(theta+dtheta)),
radius * sinf((phi+dphi)*(D3DX_PI/180)) * sinf((D3DX_PI/180)*(theta+dtheta)),
radius * cosf((phi+dphi)*(D3DX_PI/180))
);
pVertices[N].color = 0xffffffff;
N++;
}
}


}


--------------------------------------------------------------------------------


zyw_z
直接调用gluShere不就完了


这个算法好象是经纬线算法?没仔细看,呵呵
有兴趣的话可以参考3dsmax sdk中的算法


--------------------------------------------------------------------------------


zyw_z
西西:)应该减少运算量,如
float rad = PI/180;
float x = r*cos(rad*30).......


--------------------------------------------------------------------------------


LangFox
// 弹簧长度 弹簧圈数 弹簧内半径 弹簧外半径
void DrawSpring(GLfloat length, GLfloat cycles, GLfloat r1, GLfloat r2)
{
GLfloat x, y, z;
GLfloat periodlength = length / cycles;


GLfloat v = 0.0f;
glBegin(GL_LINE_STRIP);
{
for(GLfloat u = 0.0f; u <= pi_2 * cycles; u += 0.1f)
{
x = (1 - r1 * cos(v)) * cos(u);
y = (1 - r1 * cos(v)) * sin(u);
z = r2 * (sin(v) + periodlength * u / pi);
glColor3f(sin(x), sin(y), sin(z));
glVertex3f(x, y, z);
}
}
glEnd();
}


--------------------------------------------------------------------------------


LangFox
void CalOval(GLfloat &a, GLfloat &b)
{
GLfloat x, y, C;


for (GLint angle = 0; angle < 360; angle += 6)
{
glRotatef(angle, 1, 0, 0);
glBegin(GL_POINTS);
{
for (GLfloat t = 0.0f; t <= pi_2; t += 0.1f)
{
C = a * a * cos(2.0f * t) + sqrt(b * b * b * b - a * a * sin(2.0f * t) * sin(2.0f * t));
x = cos(t) * sqrt(C);
y = sin(t) * sqrt(C);
// glColor3f(x, y, 0.0f);
glVertex2f(x, y);
}
for (t = 0.0f; t <= pi_2; t += 0.1f)
{
C = a * a * cos(2.0f * t) - sqrt(b * b * b * b - a * a * sin(2.0f * t) * sin(2.0f * t));
x = cos(t) * sqrt(C);
y = sin(t) * sqrt(C);
// glColor3f(x, y, 0.0f);
glVertex2f(x, y);
}
}
glEnd();
}


if (turn)
a -= 0.01f;
else
a += 0.01f;


if ( (a > 1.1f) || (a < 0.0f) )
turn = !turn;
}


--------------------------------------------------------------------------------


LangFox
void CalShell()
{
GLint n = 2; //number of spirals
GLfloat a = 3.0f; //final shell radius
GLfloat b = 10.0f; //height
GLfloat c = 2.0f; //inner radius


GLfloat x, y, z;
glNewList(1, GL_COMPILE);
{
glBegin(GL_LINE_STRIP);
{
for(GLfloat t = 0.0f; t <= pi_2; t += 0.1f)
{
for(GLfloat s = 0.0f; s <= pi_2; s += 0.1f)
{
x = a * (1 - t / pi_2) * cos(n * t) * (1 + cos(s)) + c * cos(n * t);
y = a * (1 - t / pi_2) * sin(n * t) * (1 + cos(s)) + c * sin(n * t);
z = b * t / pi_2 + a * (1 - t / pi_2) * sin(s);
glColor3f(sin(x), sin(y), sin(z));
glVertex3f(x, y, z);
}
}
}
glEnd();
}
glEndList();
}


--------------------------------------------------------------------------------


LangFox
void CalSpring()
{
GLfloat r1 = 0.5f, r2 = 0.5f;
GLfloat periodlength=2.0f, cycles = 5.0f;;
GLfloat x, y, z;


glNewList(2, GL_COMPILE);
{
for(GLfloat u = 0.0f; u <= pi_2 * cycles; u += 0.1f)
{
glBegin(GL_LINE_STRIP);
{
for(GLfloat v = 0.0f; v <= pi_2 * cycles; v += 0.1f)
{
x = (1 - r1 * cos(v)) * cos(u);
y = (1 - r1 * cos(v)) * sin(u);
z = r2 * (sin(v) + periodlength * u / pi);
glColor3f(sin(x), sin(y), sin(z));
glVertex3f(x, y, z);
}
}
glEnd();
}
}
glEndList();
}


--------------------------------------------------------------------------------


LangFox
void CalTear()
{
GLfloat x, y, z;


glNewList(3, GL_COMPILE);
{
glBegin(GL_POINTS);
{
for (GLfloat theta1 = 0.0f; theta1 <= pi_2; theta1 += 0.1f)
{
for (GLfloat theta2 = 0.0f; theta2 <= pi; theta2 += 0.1f)
{
x = 0.5f * (1 - cos(theta2)) * sin(theta2) * cos(theta1);
y = 0.5f * (1 - cos(theta2)) * sin(theta2) * sin(theta1);
z = cos(theta2);


glVertex3f(x, y, z);
}
}
}
glEnd();
}
glEndList();
}


--------------------------------------------------------------------------------


King_jinjing
改良版……


int dtheta = 15;
int dphi = 15;
int radius = 1; //SKYBOX球体的半径


int NumVertices = (int)((360/dtheta)*(90/dphi)*4);//半球体


//int NumVertices = (int)((360/dtheta)*(180/dphi)*4);//圆球体
int N = 0;
float vx,vy,vz,mag;


for( int phi=0; phi <= 90 - dphi; phi += (int)dphi ) //半球体
//for( int phi=0; phi <= 180 - dphi; phi += (int)dphi )//圆球体
{
for( int theta=0; theta <= 360 - dtheta; theta += (int)dtheta)
{
pVertices[N].position = D3DXVECTOR3 (
radius * sinf(phi*(D3DX_PI/180)) * cosf((D3DX_PI/180)*theta),
radius * sinf(phi*(D3DX_PI/180)) * sinf((D3DX_PI/180)*theta),
radius * cosf(phi*(D3DX_PI/180))
);


//纹理坐标计算公式
vx = pVertices[N].position.x;
vy = pVertices[N].position.y;
vz = pVertices[N].position.z;


mag = (float)sqrtf( vx*vx+vy*vy+vz*vz );
vx /= mag;
vy /= mag;
vz /= mag;


pVertices[N].tu = (float)(atan2(vx,vz)/(D3DX_PI*2))+0.5f;
pVertices[N].tv = (float)(asin(vy)/D3DX_PI)+0.5f;


pVertices[N].color = 0xffffffff;
N++;


pVertices[N].position = D3DXVECTOR3 (
radius * sinf((phi+dphi)*(D3DX_PI/180)) * cosf(theta*(D3DX_PI/180)),
radius * sinf((phi+dphi)*(D3DX_PI/180)) * sinf(theta*(D3DX_PI/180)),
radius * cosf((phi+dphi)*(D3DX_PI/180))
);


vx = pVertices[N].position.x;
vy = pVertices[N].position.y;
vz = pVertices[N].position.z;


mag = (float)sqrtf( vx*vx+vy*vy+vz*vz );
vx /= mag;
vy /= mag;
vz /= mag;


pVertices[N].tu = (float)(atan2(vx,vz)/(D3DX_PI*2))+0.5f;
pVertices[N].tv = (float)(asin(vy)/D3DX_PI)+0.5f;


pVertices[N].color = 0xffffffff;
N++;


pVertices[N].position = D3DXVECTOR3 (
radius * sinf((D3DX_PI/180)*phi) * cosf((D3DX_PI/180)*(theta+dtheta)),
radius * sinf((D3DX_PI/180)*phi) * sinf((D3DX_PI/180)*(theta+dtheta)),
radius * cosf((D3DX_PI/180)*phi)
);


vx = pVertices[N].position.x;
vy = pVertices[N].position.y;
vz = pVertices[N].position.z;


mag = (float)sqrtf( vx*vx+vy*vy+vz*vz );
vx /= mag;
vy /= mag;
vz /= mag;


pVertices[N].tu = (float)(atan2(vx,vz)/(D3DX_PI*2))+0.5f;
pVertices[N].tv = (float)(asin(vy)/D3DX_PI)+0.5f;


pVertices[N].color = 0xffffffff;
N++;


if (phi > -90 && phi < 90) //半球体
//if (phi > -180 && phi < 180)//圆球体
{
pVertices[N].position = D3DXVECTOR3 (
radius * sinf((phi+dphi)*(D3DX_PI/180)) * cosf((D3DX_PI/180)*(theta+dtheta)),
radius * sinf((phi+dphi)*(D3DX_PI/180)) * sinf((D3DX_PI/180)*(theta+dtheta)),
radius * cosf((phi+dphi)*(D3DX_PI/180))
);


vx = pVertices[N].position.x;
vy = pVertices[N].position.y;
vz = pVertices[N].position.z;


mag = (float)sqrtf( vx*vx+vy*vy+vz*vz );
vx /= mag;
vy /= mag;
vz /= mag;


pVertices[N].tu = (float)(atan2(vx,vz)/(D3DX_PI*2))+0.5f;
pVertices[N].tv = (float)(asin(vy)/D3DX_PI)+0.5f;


pVertices[N].color = 0xffffffff;
N++;
}
}


}
for (int i=0; i < NumVertices-2; i++)
{
if (pVertices.tu - pVertices[i+1].tu > 0.9f)
pVertices[i+1].tu += 1.0f;
if (pVertices[i+1].tu - pVertices.tu > 0.9f)
pVertices.tu += 1.0f;
if (pVertices.tu - pVertices[i+2].tu > 0.9f)
pVertices[i+2].tu += 1.0f;
if (pVertices[i+2].tu - pVertices.tu > 0.9f)
pVertices.tu += 1.0f;
if (pVertices[i+1].tu - pVertices[i+2].tu > 0.9f)
pVertices[i+2].tu += 1.0f;
if (pVertices[i+2].tu - pVertices[i+1].tu > 0.9f)
pVertices[i+1].tu += 1.0f;
if (pVertices.tv - pVertices[i+1].tv > 0.8f)
pVertices[i+1].tv += 1.0f;
if (pVertices[i+1].tv - pVertices.tv > 0.8f)
pVertices.tv += 1.0f;
if (pVertices.tv - pVertices[i+2].tv > 0.8f)
pVertices[i+2].tv += 1.0f;
if (pVertices[i+2].tv - pVertices.tv > 0.8f)
pVertices.tv += 1.0f;
if (pVertices[i+1].tv - pVertices[i+2].tv > 0.8f)
pVertices[i+2].tv += 1.0f;
if (pVertices[i+2].tv - pVertices[i+1].tv > 0.8f)
pVertices[i+1].tv += 1.0f;
}


--------------------------------------------------------------------------------


千里马肝
void CalShell()
{
GLint n = 2; //number of spirals
GLfloat a = 3.0f; //final shell radius
GLfloat b = 10.0f; //height
GLfloat c = 2.0f; //inner radius


GLfloat x, y, z;
glNewList(1, GL_COMPILE);
{
glBegin(GL_LINE_STRIP);
{
for(GLfloat t = 0.0f; t <= pi_2; t += 0.1f)
{
for(GLfloat s = 0.0f; s <= pi_2; s += 0.1f)
{
x = a * (1 - t / pi_2) * cos(n * t) * (1 + cos(s)) + c * cos(n * t);
y = a * (1 - t / pi_2) * sin(n * t) * (1 + cos(s)) + c * sin(n * t);
z = b * t / pi_2 + a * (1 - t / pi_2) * sin(s);
glColor3f(sin(x), sin(y), sin(z));
glVertex3f(x, y, z);
}
}
}
glEnd();
}
glEndList();
}


叫个好~~~~~~~~~~~~~~~~~~


--------------------------------------------------------------------------------


maczone
全收了,多谢 :p ,呵


--------------------------------------------------------------------------------


toweryee
我是新来的,多多多多指教


--------------------------------------------------------------------------------


jrc96
谢谢了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`


--------------------------------------------------------------------------------


skyonsky
我的算法,呵呵
1+1 = 2


--------------------------------------------------------------------------------


supervendy
通用算法,按平面曲线绕轴旋转得到 引自Opengl编程实例 (自已写了一个用圆柱细分而得的,感觉没这个好,所以发这个上来) :D
void torus(GLouble innerRadius,GLdouble OuterRadius,int numc,int numt)
{
int i,j,k;
double s,t,x,y,z,xn,yn,zn,twopi,temp;
double innerR,outerR;
innerR=innerRadius;
outerR=OuterRadius;
if(outerRtemp=outerR;
outerR=innerR;
innerR=temp;
}
twopi =2*PI;
for(i=0;iglBegin(GL_QUAD_STRIP);
for(j=0;jfor(k=1;k>=0;k--){
s=(i+k)%numc+0.5;
t=j%numt;


xn=cos(s*twopi/numc)*cos(t*twopi/numt);
yn=cos(s*twopi/numc)*sin(t*twopi/numt);
zn=sin(s*twopi/numc);


x=(outerR+innerR*cos(s*twopi/numc))*cos(t*twopi/numt);
y=(outerR+innerR*cos(s*twopi/numc))*sin(t*twopi/numt);
z=innerR*sin(s*twopi/numc);
glNormal3d(xn,yn,zn);
glVertex3d(x,y,z);
}
}
glEnd();
}
}


--------------------------------------------------------------------------------


supervendy
sibyl 写的,贴在这里好一点


--------------------------------------------------------------------------------


以下代码在delphi编译通过
unit BZLineMain;


interface


uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
CgWindow, CgTypes, CgTexture, GL, ExtCtrls;


type
TBForm = class(TCGForm)
Timer: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
BForm: TBForm;


const
ctrlpoints: array [0..3,0..2] of GLfloat = //定义控制点
(( -4.0, -4.0, 0.0), ( -2.0, 4.0, 0.0),
(2.0, -4.0, 0.0), (4.0, 4.0, 0.0));
implementation


{$R *.DFM}


procedure TBForm.FormCreate(Sender: TObject);
begin


InitGL;
glClearColor(0.0, 0.0, 0.0, 1.0);


glEnable(GL_MAP1_VERTEX_3);
glShadeModel(GL_FLAT);
{
// Remember these two if you want to use lighting:
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
end;


procedure TBForm.FormPaint(Sender: TObject);
var
i,j,K:integer;
m_vertex:Array[0..360] of Array[0..4] of TCGVector;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4,@ctrlpoints[0][0]);
glBegin(GL_LINE_STRIP);
for i := 0 to 30 do
glEvalCoord1f(i/30.0);
glEnd;


for j := 0 to 360 do begin
for i:=0 to 3 do begin
m_vertex[j].x :=ctrlpoints[0]+0.8{radius}*cos(PI*j*4/360.0{细分控制数目});
m_vertex[j].y :=ctrlpoints[1]+0.8{radius}*sin(PI*j*4/360.0{细分控制数目});
m_vertex[j].z :=ctrlpoints[2];
end;
for i:=0 to 3 do begin
glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,4,4,@M_VERTEX[j][0]);
glBegin(GL_LINE_STRIP);
for k:=0 to 300 do begin
glEvalCoord1f(k/300);
end;
glEnd;
end;
end;
// 下面一段代码将控制点显示为黄色的点
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for i := 0 to 3 do
glVertex3fv(@ctrlpoints[0]);
glEnd;
PageFlip;
end;
procedure TBForm.FormResize(Sender: TObject);
var
w, h:integer;
begin
w := ClientWidth;
h := ClientHeight;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) then
glOrtho(-5.0, 5.0, -5.0*h/w,5.0*h/w, -5.0, 5.0)
else
glOrtho(-5.0*w/h, 5.0*w/h, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
end;


end.


--------------------------------------------------------------------------------


zhang_x_c
感谢各位的辛苦工作,致敬!


--------------------------------------------------------------------------------


batro
//height:高度,cycles:圈数 real minorRadius:弹簧内半径,real majorRadius 外半径
void DrawSpring(GLfloat height, GLfloat cycles, real minorRadius,real majorRadius)
{
/*
此注释内为单线弹簧
real x, y, z;


real periodHeight=height / cycles;


glBegin(GL_LINE_STRIP);
{
for(GLfloat u = 0.0f; u <= 2*M_PI * cycles; u += 0.1f)
{
x = OuterR * cos(u);
y = OuterR * sin(u);
z = periodHeight * u / 2 / M_PI;


glVertex3f(x, y, z);
}
}
glEnd();
*/


int numMajor =32 ;
int numMinor = 16;
int totalMajor = 32 * cycles ;


real periodHeight=height / cycles;


double majorStep =2.0f * M_PI/numMajor;
double minorStep =2.0f * M_PI/numMinor;
int i, j;


// glColor3fv(color);
for(i=0; i{
real a0 = i * majorStep;
real a1 = a0 + majorStep;


real x0 = (real) cos(a0);
real y0 = (real) sin(a0);
real z0 = periodHeight * i / numMajor ;


real x1 = (real) cos(a1);
real y1 = (real) sin(a1);
real z1 = periodHeight * (i+1) / numMajor ;


glBegin(GL_TRIANGLE_STRIP);
for(j=0; j<= numMinor; ++j)
{
real b = j * minorStep;
real c = (real)cos(b);
real r = minorRadius * c + majorRadius;
real z = minorRadius * (real)sin(b);


glNormal3f(x0*c, y0*c, z/minorRadius);


glVertex3f(x0*r, y0*r, z+z0);


glNormal3f(x1*c, y1*c, z/minorRadius);
glVertex3f(x1*r, y1*r, z+z1);
}
glEnd();
}
}
现在在做管道设计软件,手头上有相关的工程源代码,跟我联系啊,大家交个朋友
QQ:33099024 MSN:batlv@hotmail.com
效果图http://www.tced.com/lvdong/tanhuang.jpg


--------------------------------------------------------------------------------


wineswan
:) :) :)


--------------------------------------------------------------------------------


shliwan
我也自己写了些绘图函数,诸如旋转体(台体,球冠)之类的东西,其实只要有比较好的基础类库,这些函数都不难。不过如果gl的库里面的函数可以满足要求的话就没有必要自己些,毕竟库是可以信赖的。


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空