在windows窗体程序中用opengl画三维图,把代码蓝色部分注释掉就是动图,不然就是一个静态的图,问题出在哪了啊!!求路过的大神救助!!!

Geozzher 2017-05-25 01:20:22

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存
glLoadIdentity(); // 重置模型观察矩阵
glColor3f(1.0,1.0,1.0);
glColorMaterial(GL_FRONT,GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glLoadIdentity();
glTranslatef(X_Move,-Y_Move,distance_z);/////////////////////////////////////////////////////////
//旋转成右手笛卡尔坐标系,方便认知
glRotatef(45.0,1.0,1.0,0.0);
//把当前矩阵压栈
glPushMatrix();
//绘制红色太阳
glColor3f(1.0,1.0,0.0);
glutWireSphere(2.0,50,50);
//////绘制地球!!!此时栈里是右手笛卡尔坐标,y向屏幕里,z朝上,x朝右
glColor3f(0.0,0.0,1.0);
// 绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 5.0, 64, 64);
glRotatef(-90,1.0,0,0.0);
// 设置地球公转速度,绕Y轴旋转,(此时的坐标)
glRotatef(rot0,0.0,1.0,0.0);
// 设置地球位置,向右移5个单位
glTranslatef(5.0,0.0,0.0);
// 设置地球自转速度,自传看不出来,故不让其自传
//glRotatef(rot1,0.0,1.0,0.0);
// 绘制地球
glColor3f(0.0,0.0,1.0);
glutSolidSphere(0.4,32,32);
/////// 绘制地球的卫星-月亮
glColor3f(0.5,0.6,0.5);
// 抵消地球自转影响
//glRotatef(-rot1,0.0,1.0,0.0);
glRotatef(rot9,0.0,1.0,0.0);
// 设置月亮位置
glTranslatef(0.6,0.0,0.0);
// 绘制月亮
glutSolidSphere(0.1,10,8);
glPopMatrix();//矩阵清除
// 绘制水星
glPushMatrix();//继续创建矩阵副本
glColor3f(0.0,1.0,1.0);
// 绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 2.5, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置水星公转速度
glRotatef(rot1,0.0,1.0,0.0);
// 设置水星公转半径
glTranslatef(2.5,0.0,0.0);
// 设置水星自传
glRotatef(rot3,0.0,1.0,0.0);
// 绘制水星
glutSolidSphere(0.2,32,32);
glPopMatrix();
//绘制金星
glPushMatrix();
glColor3f(0.0,1.0,0.0);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 3.4, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置金星公转速度
glRotatef(rot2,0.0,3.0,0.0);
// 设置金星公转半径
glTranslatef(3.4,0.0,0.0);
// 设置金星自传
glRotatef(rot0,0.0,1.0,0.0);
// 绘制金星
glutSolidSphere(0.3,32,32);
glPopMatrix();
//绘制火星
glPushMatrix();
glColor3f(1.0,0.0,0.0);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 6.6, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置火星公转速度
glRotatef(rot3,0.0,4.0,0.0);
// 设置火星公转半径
glTranslatef(6.6,0.0,0.0);
// 设置火星自转
glRotatef(rot7,0.0,2.0,0.0);
// 绘制火星
glutSolidSphere(0.5,32,32);
glPopMatrix();
//绘制木星
glPushMatrix();
glColor3f(2.0,0.1,1.0);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 8.5, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置木星公转速度
glRotatef(rot4,0.0,0.4,0.0);
// 设置木星公转半径
glTranslatef(8.5,0.0,0.0);
// 设置木星自传
glRotatef(rot3,0.0,0.3,0.0);
// 绘制木星
glutSolidSphere(1.0,32,32);
// 绘制木星卫星-木卫1
glColor3f(0.4,0.3,0.5);
// 抵消地球自转影响
//glRotatef(-rot1,0.0,1.0,0.0);
// 绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glRotatef(-90,1.0,0,0.0);
// 设置木卫1公转速度
glRotatef(rot10,0.0,1.0,0.0);
// 设置木卫1公转半径
glTranslatef(1.3,0.0,0.0);
// 绘制木卫1
glutSolidSphere(0.1,10,8);
// 绘制木星卫星-木卫2
glColor3f(0.5f,0.5f,0.5f);
// 抵消地球自转影响
//glRotatef(-rot1,0.0,1.0,0.0);
// 绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glRotatef(-90,1.0,0,0.0);
// 设置木卫2公转速度
glRotatef(rot11,0.0,1.0,0.0);
// 设置木卫2公转半径
glTranslatef(1.2,0.0,0.0);
// 绘制木卫2
glutSolidSphere(0.08,10,8);
glPopMatrix();
//绘制土星
glPushMatrix();
glColor3f(1.0f, 1.0f, 0.0f);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 12.5, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置土星公转速度
glRotatef(rot5,0.0,0.4,0.0);
// 设置土星公转半径
glTranslatef(12.5,0.0,0.0);
// 设置土星自传
glRotatef(rot3,0.0,0.3,0.0);
// 绘制土星
glutSolidSphere(0.85,32,32);
//绘制土星光环
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.1, 1.25, 10, 64);
glRotatef(-90,1.0,0,0.0);
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.07, 1.65, 10, 64);
glRotatef(-90,1.0,0,0.0);
glPopMatrix();
glPopMatrix();
//绘制天王星
glPushMatrix();
glColor3f(0.0f, 1.0f, 1.0f);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 15.5, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置天王星公转速度
glRotatef(rot6,0.0,0.4,0.0);
// 设置天王星公转半径
glTranslatef(15.5,0.0,0.0);
// 设置天王星自传
glRotatef(rot1,0.0,0.3,0.0);
// 绘制天王星
glutSolidSphere(0.22,32,32);
glPopMatrix();
//绘制海王星
glPushMatrix();
glColor3f(0.0f, 0.0, 8.0f);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 17.5, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置海王星公转速度
glRotatef(rot7,0.0,0.4,0.0);
// 设置海王星公转半径
glTranslatef(17.5,0.0,0.0);
// 设置海王星自传
glRotatef(rot3,0.0,0.3,0.0);
// 绘制海王星
glutSolidSphere(0.20,32,32);
glPopMatrix();
//绘制冥王星
glPushMatrix();
glColor3f(0.5f, 0.5f, 0.5f);
//绘制辅助轨道
glRotatef(90,1.0,0,0.0);
glutSolidTorus(0.02, 19.5, 10, 64);
glRotatef(-90,1.0,0,0.0);
// 设置冥王星公转速度
glRotatef(rot8,0.0,0.4,0.0);
// 设置冥王星公转半径
glTranslatef(19.5,0.0,0.0);
// 设置冥王星自传
glRotatef(rot2,0.0,0.3,0.0);
// 绘制冥王星
glutSolidSphere(0.20,32,32);
glPopMatrix();
// SwapBuffers(hDC);
rot0+=0.1;
if(rot0>=360.0)
rot0-=360.0;
rot1+=0.416;
if(rot1>=360.0)
rot1-=360.0;
rot2+=0.1631;
if(rot2>=360.0)
rot2-=360.0;
rot3+=0.053;
if(rot3>=360.0)
rot3-=360.0;
rot4+=0.0083;
if(rot4>=360.0)
rot4-=360.0;
rot5+=0.0034;
if(rot5>=360.0)
rot5-=360.0;
rot6+=0.00119;
if(rot6>=360.0)
rot6-=360.0;
rot7+=0.00069;
if(rot7>=360.0)
rot7-=360.0;
rot8+=0.0008;
if(rot8>=360.0)
rot8-=360.0;
rot9+=1.0;
if(rot9>=360.0)
rot9-=360.0;
rot10+=0.005;
if(rot10>=360.0)
rot10-=360.0;
rot11+=0.01;
if(rot11>=360.0)
rot11-=360.0;
SwapBuffers(hDC);
return TRUE; // Keep Going
}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{

static HWND btnHwnd[6] ; //子窗口句柄
int i,j=0;
PAINTSTRUCT ps;
static int cxChar, cyChar ;
switch (uMsg) // Check For Windows Messages
{
//在接收到 WM_CREATE 消息时创建一个按钮
case WM_CREATE: //在接收到 WM_CREATE 消息时创建一个按钮
for(i=0;i<7;i++)
{btnHwnd[i] = CreateWindow( TEXT("button"), TEXT(TEXT[i]),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
j, 0, //在父窗口客户区创建一个按钮
100, 30, //按钮的大小为100x30
hWnd, //父窗口句柄
(HMENU)i+1, //按钮控件ID
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE ),
NULL ) ;
j+=100;}
return 0 ;


case WM_SIZE:
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
return 0; // Jump Back

case WM_PAINT:
hDC =BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&WindowRect);
InitGL();
DrawGLScene();
EndPaint (hWnd,&ps);
return 0;

case WM_DESTROY:
ReleaseDC(hWnd,hDC);
wglDeleteContext(hRC);
PostQuitMessage(0);
return 0;

case WM_COMMAND :

break ;
}

return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
...全文
172 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
幻夢之葉 2017-05-25
  • 打赏
  • 举报
回复
windows下程序的绘制使用到DC(设备上下文),opengl也是使用该HDC进行绘制的 而且PAINT重绘消息里总是(或者说几乎)有如下形式

hDC =BeginPaint(hWnd,&ps);
//绘制代码
EndPaint (hWnd,&ps);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧