回答
如果你不能從GLU庫(例如,如果你洛根。基於OpenGL-ES)使用gluCylinder()
或者你必須從小平段使氣缸的兩側,
How do you draw a cylinder with OpenGLES?
但我需要一個函數來計算點之間的X和Y角度,並從第一個到另一個繪製柱面 – trrrrrrm 2009-12-10 02:26:53
對於用兩個給定點構建圓柱體,您需要矢量分析。您正在構建兩個垂直向量,這些向量被添加到每個點,並且按照sin/cos乘以半徑進行縮放。 它接受所有點(舊代碼有一個錯誤,因爲它錯過了長度的sqrt())。 現在它功能正常,並用gl例程繪製圓柱體;我在JOGL中測試過它。 要獲得更快的繪製,請將firstPerpVector/secondPerpVector/points變量移至私有最終數組字段,並在開始時對它們進行初始化。
Java代碼:
public float[] getFirstPerpVector(float x, float y, float z) {
float[] result = {0.0f,0.0f,0.0f};
// That's easy.
if (x == 0.0f || y == 0.0f || z == 0.0f) {
if (x == 0.0f)
result[0] = 1.0f;
else if (y == 0.0f)
result[1] = 1.0f;
else
result[2] = 1.0f;
}
else {
// If xyz is all set, we set the z coordinate as first and second argument .
// As the scalar product must be zero, we add the negated sum of x and y as third argument
result[0] = z; //scalp = z*x
result[1] = z; //scalp = z*(x+y)
result[2] = -(x+y); //scalp = z*(x+y)-z*(x+y) = 0
// Normalize vector
float length = 0.0f;
for (float f : result)
length += f*f;
length = (float) Math.sqrt(length);
for (int i=0; i<3; i++)
result[i] /= length;
}
return result;
}
public void drawCylinder(GL gl, float x1, float y1, float z1, float x2, float y2, float z2) {
final int X = 0,
Y = 1,
Z = 2;
// Get components of difference vector
float x = x1-x2,
y = y1-y2,
z = z1-z2;
float[] firstPerp = getFirstPerpVector(x,y,z);
// Get the second perp vector by cross product
float[] secondPerp = new float[3];
secondPerp[X] = y*firstPerp[Z]-z*firstPerp[Y];
secondPerp[Y] = z*firstPerp[X]-x*firstPerp[Z];
secondPerp[Z] = x*firstPerp[Y]-y*firstPerp[X];
// Normalize vector
float length = 0.0f;
for (float f : secondPerp)
length += f*f;
length = (float) Math.sqrt(length);
for (int i=0; i<3; i++)
secondPerp[i] /= length;
// Having now our vectors, here we go:
// First points; you can have a cone if you change the radius R1
final int ANZ = 32; // number of vertices
final float FULL = (float) (2.0f*Math.PI),
R1 = 4.0f; // radius
float[][] points = new float[ANZ+1][3];
for (int i=0; i<ANZ; i++) {
float angle = FULL*(i/(float) ANZ);
points[i][X] = (float) (R1*(Math.cos(angle)*firstPerp[X]+Math.sin(angle)*secondPerp[X]));
points[i][Y] = (float) (R1*(Math.cos(angle)*firstPerp[Y]+Math.sin(angle)*secondPerp[Y]));
points[i][Z] = (float) (R1*(Math.cos(angle)*firstPerp[Z]+Math.sin(angle)*secondPerp[Z]));
}
// Set last to first
System.arraycopy(points[0],0,points[ANZ],0,3);
gl.glColor3f(1.0f,0.0f,0.0f);
gl.glBegin(GL.GL_TRIANGLE_FAN);
gl.glVertex3f(x1,y1,z1);
for (int i=0; i<=ANZ; i++) {
gl.glVertex3f(x1+points[i][X],
y1+points[i][Y],
z1+points[i][Z]);
}
gl.glEnd();
gl.glBegin(GL.GL_TRIANGLE_FAN);
gl.glVertex3f(x2,y2,z2);
for (int i=0; i<=ANZ; i++) {
gl.glVertex3f(x2+points[i][X],
y2+points[i][Y],
z2+points[i][Z]);
}
gl.glEnd();
gl.glBegin(GL.GL_QUAD_STRIP);
for (int i=0; i<=ANZ; i++) {
gl.glVertex3f(x1+points[i][X],
y1+points[i][Y],
z1+points[i][Z]);
gl.glVertex3f(x2+points[i][X],
y2+points[i][Y],
z2+points[i][Z]);
}
gl.glEnd();
}
採用目前公認的答案發現的錯誤。結束了我自己的想法。你可以用一些簡單的數學做:
http://www.thjsmith.com/40/cylinder-between-two-points-opengl-c
把代碼放在你的答案;不要只鏈接到某個網站。 – 2013-08-03 23:39:50
此解決方案假定您已經結合了3D矢量和4D四元數成一個單一的實用工具類,一些通用的攝像頭的代碼。因此,對於世界空間中的兩個3D點A和B,從A到B繪製柱面的一般解決方案是:
void draw_cylinder
(
const vector3d& A
,const vector3d& B
,const double radius
)
{
// STEP 1: calculate the distance between the points A and B
double height = (B-A).length();
// STEP 2: put a camera object at point A
camera C(A); // quat rotation assumed to be facing -Z axis
// STEP 3: tell the camera to look directly at point B
C.look_at(B);
// STEP 4: save the current OpenGl matrix state
glPushMatrix();
// STEP 5: give the camera transform to OpenGL
C.apply_transform_model_to_world();
// STEP 6: draw your cylinder from (0,0,0) to (0,0,-height)
gluCylinder
(
pqobj //GLUquadric*
,radius //GLdouble base
,radius //GLdouble top
,height //GLdouble
,8 //GLint slices
,8 //GLint stacks
);
// STEP 7: restore the OpenGl matrix stack
glPopMatrix();
}
- 1. 如何在Android上的OpenGL-es中繪製圓柱體?
- 2. 如何在y或x軸上繪製圓柱體opengl
- 3. OpenGL:三角帶環 - 如何繪製圓柱體?
- 4. 如何用OpenGLES繪製圓柱體?
- 5. 現代OpenGL:繪製球體和圓柱體
- 6. 在Opengl中繪製橢圓球體
- 7. 如何在OpenTK(.Glu.Cylinder)中繪製圓柱體?
- 8. OpenGL:環繞圓柱體的紋理
- 9. 的OpenGL渲染半圓柱體
- 10. 在openGL中繪製一個9個點的圓圈?
- 11. 如何使用gl_polygon繪製三維圓柱體
- 12. 根據兩點繪製一個圓
- 13. 用A型框在2個點之間繪製一個圓柱
- 14. 連接球體頂點OpenGL
- 15. 用WPF中的圓柱體加入兩個球體3D
- 16. 在DirectX中試圖通過D3DXCreateCylinder繪製圓柱體
- 17. 如何繪製虛線連接的點?
- 18. OpenGL繪製橢圓而不是圓形
- 19. 如何通過兩點繪製一個圓(直徑末端)?
- 20. 在圓柱體中生成隨機點
- 21. 在Matplotlib中繪製一個以飛機爲中心的實心圓柱體
- 22. 使用OpenGL繪製連接的線
- 23. 通過兩點繪製精確圓圈
- 24. 在openGL中用Android繪製圓圈
- 25. 使用OpenGL繪製實心圓作爲一個頂點數組?
- 26. 如何在OpenGL中紋理繪製柱面圖?
- 27. 如何繪製圓周上的點R
- 28. 將圓柱體模型繪製爲粗3d線
- 29. 在opengl中繪製點
- 30. 如何動畫在OpenGL中繪製的圓?
我認爲您將必須更加具體。 – bmargulies 2009-12-10 02:19:40
有無數個圓柱包含這兩點。你能描述一下你希望氣缸相對於點的方向嗎?我懷疑你需要一個單元(或用戶指定的)半徑的圓柱體,它的兩端都在其中心線上。但你需要具體... – 2009-12-15 04:34:08