一、使用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;
//如果在起始時候加載 使用該段代碼
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();