|
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (DC == 0 || RC == 0)
return;
openGL.glClearColor((float)globals.m_Bred / 255.0f, (float)globals.m_Bgreen / 255.0f, (float)globals.m_Bblue/255.0f, 1.0f);
WGL.wglMakeCurrent(DC, RC);
openGL.glClear(openGL.GL_COLOR_BUFFER_BIT | openGL.GL_DEPTH_BUFFER_BIT);
openGL.glClearDepth(1.0f);
openGL.glEnable(openGL.GL_DEPTH_TEST);
openGL.glDepthFunc(openGL.GL_LEQUAL);
openGL.glTexEnvf(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_ENV_MODE, openGL.GL_BLEND);
openGL.glEnable(openGL.GL_TEXTURE_2D);
openGL.glBindTexture(openGL.GL_TEXTURE_2D, globals.intexture[0]);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_MAG_FILTER, (int)openGL.GL_NEAREST);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_MIN_FILTER, (int)openGL.GL_NEAREST);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_WRAP_S, (int)openGL.GL_CLAMP);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_WRAP_T, (int)openGL.GL_CLAMP);
openGL.glEnable(openGL.GL_LINE_SMOOTH);//反走样
openGL.glEnable(openGL.GL_BLEND);
openGL.glBlendFunc(openGL.GL_SRC_ALPHA, openGL.GL_ONE_MINUS_SRC_ALPHA);//alpha模式下反走样
openGL.glHint(openGL.GL_LINE_SMOOTH_Hint, openGL.GL_DONT_CARE);
openGL.glShadeModel(openGL.GL_SMOOTH);
openGL.glPushMatrix();
openGL.glLoadIdentity();
openGL.glTranslatef(globals.m_fMoveX, globals.m_fMoveY, -3.5f);
openGL.glRotatef(globals.m_fAngleX, 1.0f, 0.0f, 0.0f);
openGL.glRotatef(globals.m_fAngleY, 0.0f, 1.0f, 0.0f);
openGL.glRotatef(globals.m_fAngleZ, 0.0f, 0.0f, 1.0f);
openGL.glScalef(globals.m_fGlobalScale, globals.m_fGlobalScale, globals.m_fGlobalScale);
if (globals.g_imgbmpWidth2 > 0 && globals.g_imgbmpHeight2 > 0)
drawPic();
if (globals.g_terbmpWidth1 > 0 && globals.g_terbmpHeight1 > 0)
{
openGL.glBindTexture(openGL.GL_TEXTURE_2D, globals.intexture[1]);
//IntPtr np = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (uint)m_symbol.Length);
//if (np == IntPtr.Zero)
//{
// throw new OutOfMemoryException();
//}
//else
//{
// Marshal.Copy(m_symbol, 0, np, m_symbol.Length);
//}
openGL.glTexSubImage2D(openGL.GL_TEXTURE_2D, 0, 0, 0, globals.g_terbmpWidth1, globals.g_terbmpHeight1, openGL.GL_BGRA_EXT, openGL.GL_UNSIGNED_byte, m_symbol); //?
//LocalFree(np);
openGL.glEnable(openGL.GL_LIGHTING); //20090923
openGL.glEnable(openGL.GL_LIGHT0); //20090923
float[] light_position ={ 0.0f, 6.0f, 5.5f, 0.0f }; //20090923
float[] light_ambient ={ 1.0f, 1.0f, 1.0f, 0.0f }; //20090923
float[] mat_env ={ 0.75f, 0.75f, 0.75f, 0.0f }; //20090923
openGL.glLightfv(openGL.GL_LIGHT0, openGL.GL_POSITION, light_position); //20090923
openGL.glLightfv(openGL.GL_LIGHT0, openGL.GL_AMBIENT, light_ambient); //20090923
openGL.glMaterialfv(openGL.GL_FRONT, openGL.GL_AMBIENT, mat_env); //20090923
draw();
openGL.glDisable(openGL.GL_LIGHTING); //20090923
}
openGL.glDisable(openGL.GL_TEXTURE_2D);
if (m_Corrd == 1)
drawCoord(-2.0f, globals.g_bmpPicY, 2.0f, 4.1f, 2.0f, 4.1f);//分别是坐标轴零点的openGL坐标值和对应坐标轴长度(xyz)
if (m_mouse == (int)ALTER.SELECT && m_bLBtnDown == true)
rubberDraw();
openGL.glPopMatrix();
openGL.glFinish();
WGL.wglSwapBuffers(DC);
}
protected virtual void InitializeGL(object sender, EventArgs e)
{
m_uint_HWND = (uint)this.Handle.ToInt32();
m_uint_DC = WGL.GetDC(m_uint_HWND);
// CAUTION: Not doing the following WGL.wglSwapBuffers() on the DC will
// result in a failure to subsequently create the RC.
WGL.wglSwapBuffers(m_uint_DC);
//Get the pixel format
WGL.PIXELFORMATDESCRIPTOR pfd = new WGL.PIXELFORMATDESCRIPTOR();
WGL.ZeroPixelDescriptor(ref pfd);
pfd.nVersion = 1;
pfd.dwFlags = (WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL | WGL.PFD_DOUBLEBUFFER);
pfd.iPixelType = (byte)(WGL.PFD_TYPE_RGBA);
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.cAlphaBits = 0;
pfd.iLayerType = (byte)(WGL.PFD_MAIN_PLANE);
int pixelFormatIndex = 0;
pixelFormatIndex = WGL.ChoosePixelFormat(m_uint_DC, ref pfd);
if (pixelFormatIndex == 0)
{
MessageBox.Show("Unable to retrieve pixel format");
return;
}
if (WGL.SetPixelFormat(m_uint_DC, pixelFormatIndex, ref pfd) == 0)
{
MessageBox.Show("Unable to set pixel format");
return;
}
//Create rendering context
m_uint_RC = WGL.wglCreateContext(m_uint_DC);
if (m_uint_RC == 0)
{
MessageBox.Show("Unable to get rendering context");
return;
}
if (WGL.wglMakeCurrent(m_uint_DC, m_uint_RC) == 0)
{
MessageBox.Show("Unable to make rendering context current");
return;
}
//Set up OpenGL related characteristics
ResizeGL(null, null);
//100319注:此处必须这样设置,否则相同的alpha值,图像的明暗仍会随着图像缩放变化
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_MAG_FILTER, (int)openGL.GL_NEAREST);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_MIN_FILTER, (int)openGL.GL_NEAREST);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_WRAP_S, (int)openGL.GL_CLAMP);
openGL.glTexParameteri(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_WRAP_T, (int)openGL.GL_CLAMP);
openGL.glTexEnvi(openGL.GL_TEXTURE_2D, openGL.GL_TEXTURE_ENV_MODE, (int)openGL.GL_DECAL);
openGL.glEnable(openGL.GL_TEXTURE_2D);
openGL.glShadeModel(openGL.GL_SMOOTH);
openGL.glEnable(openGL.GL_BLEND);//
// glEnable(GL_ALPHA_TEST);//
//设置融合方式
openGL.glBlendFunc(openGL.GL_SRC_ALPHA, openGL.GL_ONE_MINUS_SRC_ALPHA);//
}
protected virtual void ResizeGL(object sender, EventArgs e)
{
if (m_uint_DC == 0 || m_uint_RC == 0)
return;
if (this.Width == 0 || this.Height == 0)
return;
WGL.wglMakeCurrent(m_uint_DC, m_uint_RC);
openGL.glViewport(0, 0, this.Width, this.Height);
openGL.glMatrixMode(openGL.GL_PROJECTION);
openGL.glLoadIdentity();
GLU.gluPerspective(45.0, ((double)(this.Width) / (double)(this.Height)), 0.1, 10.0);
openGL.glMatrixMode(openGL.GL_MODELVIEW);
openGL.glLoadIdentity();
}
在rubberDraw()中计算坐标值:
{
int[] viewport=new int[4];
double[] modelview=new double[16];
double[] projection=new double[16];
double winX = 0.0f, winY = 0.0f, winZ = 0.0f;
double posX = 0.0f, posY = 0.0f, posZ = 0.0f;
openGL.glGetIntegerv(openGL.GL_VIEWPORT, viewport);
openGL.glGetDoublev(openGL.GL_MODELVIEW_MATRIX, modelview);
openGL.glGetDoublev(openGL.GL_PROJECTION_MATRIX, projection);
winX = (float)mouse_x;
winY = (float)viewport[3] - (float)mouse_y;
openGL.glReadPixels((int)winX, (int)winY, 1, 1, openGL.GL_DEPTH_COMPONENT, openGL.GL_DOUBLE, ref winZ);
GLU.gluUnProject(winX, winY, winZ, modelview, projection, viewport, ref posX, ref posY, ref posZ);
...
}
为什么glReadPixels()计算的z值一直是0.0? |
|