一、使用list创造:
m_billboardRectList = GL.glGenLists(1);
GL.glNewList(m_billboardRectList, GL.GL_COMPILE);
{可以加入任何的创造物体!!}
GL.glEndList();
如下:
(1)创造点
private void createPoint()
{
m_billboardRectList = GL.glGenLists(1);
GL.glNewList(m_billboardRectList, GL.GL_COMPILE);
foreach (GLPoint p in m_array)
{
//convert the mouse coordinate into geocentric (OpenGL) coordinate system
//draw the converted point on the surface of the globe
GL.glPointSize(15.0f);
GL.glColor3ub(255, 0, 0);
GL.glBegin(GL.GL_POINTS);
GL.glVertex3f((float)p.glX, (float)p.glY, (float)p.glZ);
GL.glEnd();
}
GL.glEndList();
m_bDrawPoint = true;
}
(2)创造图像
private void CreateDisplayLists()
{
try
{
//打开位图文件
System.Drawing.Bitmap bitmap = new Bitmap(@"D:/22.gif");
if (null == bitmap) throw new Exception("Error initializing bitmap");
//创建贴图
uint texId = OpenGLTool.CreateTexture(bitmap);
if (0 == texId) throw new Exception("Error generating texture");
//the quad size is set to 1 unit. Therefor you will have to scale it
//each time before drawing.
m_billboardRectList = GL.glGenLists(1);
GL.glNewList(m_billboardRectList, GL.GL_COMPILE);
GL.glPushMatrix();
GL.glAlphaFunc(GL.GL_GREATER, 0.5f);
//向左1/2单位的平移,使位置剧中
GL.glTranslatef(-0.5f, 0.0f, 0.0f);
//设置openGL标志
GL.glDisable(GL.GL_LIGHTING);
GL.glEnable(GL.GL_BLEND);
GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
GL.glDepthFunc(GL.GL_LEQUAL);
//绑定贴图
GL.glEnable(GL.GL_TEXTURE_2D);
GL.glBindTexture(GL.GL_TEXTURE_2D, texId);
//create the geometry (quad) and specify the texture coordinates
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(0.0f, 0.0f, 0.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(0.0f, 1.0f, 0.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(1.0f, 1.0f, 0.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(1.0f, 0.0f, 0.0f);
GL.glEnd();
GL.glPopMatrix();
GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL);
//Set off the OpenGL flags
GL.glEndList();
m_bDisplayListCreated = true;
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
}
(3)创造三维物体
public void CreateDisplayList(IMarker3DSymbol marker3DSymbol)
{
m_globeDisplay.DirectOpenGLDraw = true;
GL.glMatrixMode(GL.GL_MODELVIEW);
IDisplay display = (IDisplay)m_globeDisplay;
//如果在起始时候加载 使用该段代码
/*计算3DMAX尺寸
IGlobeDisplayRendering globeDisplayRendering = (IGlobeDisplayRendering)m_globeDisplay;
double globeRadiusMeters = globeDisplayRendering.GlobeRadius;
//double scale = 1.0 / globeRadiusMeters; //Normalized by the Globe Radius.
*/
double scale = 0.0000068;
m_billboardRectList = GL.glGenLists(1);
GL.glNewList(m_billboardRectList, GL.GL_COMPILE);
{
// GL.glDisable(GL.GL_COLOR_MATERIAL);
GL.glPushMatrix();
{
GL.glScaled(scale, scale, scale);
display.SetSymbol((ISymbol)marker3DSymbol);
}
GL.glPopMatrix();
// GL.glEnable(GL.GL_COLOR_MATERIAL);
}
GL.glEndList();
m_globeDisplay.DirectOpenGLDraw = false;
// return intSymbolDisplayList;
}
二、使用GL.glCallList(m_billboardRectList);将物体绘制调出
(1)点或图像
private void ShowImage()
{
try
{
//为了图片面向观众的初始矩阵
//get OpenGL model matrix which is required in order to billboard the icons
GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, m_modelViewMatrix);
//populate the billboard matrix
m_billboardMatrix[0] = m_modelViewMatrix[0];
m_billboardMatrix[1] = m_modelViewMatrix[4];
m_billboardMatrix[2] = m_modelViewMatrix[8];
m_billboardMatrix[3] = 0;
m_billboardMatrix[4] = m_modelViewMatrix[1];
m_billboardMatrix[5] = m_modelViewMatrix[5];
m_billboardMatrix[6] = m_modelViewMatrix[9];
m_billboardMatrix[7] = 0;
m_billboardMatrix[8] = m_modelViewMatrix[2];
m_billboardMatrix[9] = m_modelViewMatrix[6];
m_billboardMatrix[10] = m_modelViewMatrix[10];
m_billboardMatrix[11] = 0;
m_billboardMatrix[12] = 0;
m_billboardMatrix[13] = 0;
m_billboardMatrix[14] = 0;
m_billboardMatrix[15] = 1;
//如果是画点
// m_bDrawPoint = true;
if (true == m_bDrawPoint)
{
//draw the converted point on the surface of the globe
GL.glPushMatrix();
GL.glCallList(m_billboardRectList);
GL.glPopMatrix();
}
else
//draw the point in the points array
if (m_array.Count > 0)
{
//for each item, we need to get the distance from the camera (in geocentric units) in order to scale it
double dblObsX, dblObsY, dblObsZ, dMagnitude, scale;
ICamera camera = m_sceneViwer.Camera;
camera.Observer.QueryCoords(out dblObsX, out dblObsY);
dblObsZ = camera.Observer.Z;
//draw the static points
foreach (GLPoint p in m_array)
{
//get the distance from the camera to the drawn item.
//This distance will determine whteher to draw the item as a dot or as
//a full icon.
m_vector3D.SetComponents(dblObsX - p.glX, dblObsY - p.glY, dblObsZ - p.glZ);
dMagnitude = m_vector3D.Magnitude;
scale = 0.04 * dMagnitude;
GL.glPushMatrix();
//translate to the items location
GL.glTranslatef((float)p.glX, (float)p.glY, (float)p.glZ);
//orient the icon so that it'll face the camera
OrientBillboard();
//scale the item (original size is 1 ubit)
GL.glScaled(scale, scale, 1.0);
GL.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//draw the item
GL.glCallList(m_billboardRectList);
GL.glPopMatrix();
}
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
}
(2)调出三维:
private void Show3DS()
{
// IGlobeDisplayRenderingPtr ipGlobeDisplayRend(pGlobeDisplay);
double dblSymbolAngle=90;
WKSPointZ wksSymbolOrientation;
foreach (GLPoint p in m_array)
{
//Translate the object into place.
GL.glPushMatrix();
//Get the object’s OpenGL coordinate.
//Calculate the object’s orientation.
CalculateSymbolOrientation(this.m_sceneViwer, p.glX, p.glY, p.glZ,
out wksSymbolOrientation);
//Translate the object into place and orient it.
TranslateAndRotate(p.glX, p.glY, p.glZ, wksSymbolOrientation, dblSymbolAngle);
//可以设置为标量(缩小放大,物体不变大小).
// double dObjScale = this.getScale(p);
// GL.glScaled(dObjScale, dObjScale, dObjScale);
//Draw the object.
GL.glCallList(m_billboardRectList);
GL.glPopMatrix();
}
}
二、在OnAfterDraw中调用以上两个函数
createPoint();
this.ShowImage();
//Show3DS();