这是我的win32控制台程序,如何将其转换为MFC应用程序?
// Earth.cpp : 
//#include "stdafx.h"
#include <string.h>
#include "glut.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>#define BMP_Header_Length 54GLuint Earth;
GLUquadricObj *qobj;
GLdouble spin = 0.0;
GLfloat xoffset;
static GLfloat day = 0;
static GLfloat  pi = 3.141592653;//////////////////////轨道根数/////////////////////
GLfloat a=1.2;//轨道半径
GLfloat e=0.1;//轨道偏心率
GLfloat W=-45;//升交点赤经
GLfloat w=45;//近地点角距
GLfloat i=45;//轨道倾角
GLfloat t=0;//过近地点时刻
//////////////////////////////////////////////////int power_of_two(int n)
{
if( n <= 0 )
return 0;
return (n & (n-1)) == 0;
}GLuint load_texture(const char* file_name){ GLint width, height, totoal_bytes;
GLubyte* pixels = 0;
GLint last_texture_ID;
GLuint texture_ID = 0; FILE* pFile = fopen(file_name, "rb");
if( pFile == 0 )
return 0; fseek(pFile, 0x0012, SEEK_SET);
fread(&width, 4, 1, pFile);
fread(&height, 4, 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET); {
GLint line_byte = width * 3;
while( line_byte % 4 != 0 )
++line_byte;
totoal_bytes = line_byte * height;
} pixels = (GLubyte*)malloc(totoal_bytes);
if ( pixels == 0 ) {
fclose(pFile);
return 0;
} if ( fread(pixels, totoal_bytes, 1, pFile) <= 0 ) {
free(pixels);
fclose(pFile);
return 0;
} {
GLint max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if ( !power_of_two(width)
|| !power_of_two(height)
|| width > max
|| height > max) {
const GLint new_width = 256;
const GLint new_height = 256; GLint new_line_bytes, new_total_bytes;
GLubyte* new_pixels = 0; new_line_bytes =  new_width * 3;
while( new_line_bytes % 4 != 0 )
++new_line_bytes;
new_total_bytes = new_line_bytes * new_height; new_pixels = (GLubyte*)malloc(new_total_bytes);
if( new_pixels == 0 )
{
free(pixels);
fclose(pFile);
return 0;
} gluScaleImage(GL_RGB,
width, height, GL_UNSIGNED_BYTE, pixels,
new_width, new_height, GL_UNSIGNED_BYTE, new_pixels); free(pixels);
pixels = new_pixels;
width = new_width;
height = new_height; }
} glGenTextures(1, &texture_ID);
if( texture_ID == 0 )
{
free(pixels);
fclose(pFile);
return 0;
} glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture_ID);
glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels);
glBindTexture(GL_TEXTURE_2D, last_texture_ID); free(pixels);
return texture_ID;
}void drawSatellite()
{
GLfloat lmodel_ambient[]={ 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[]={ 1.0, 1.0, 1.0, 0.0 };
GLfloat yellow_light[]={ 1.0, 0.5, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, yellow_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glRotatef(day, 0.0, 1.0, 0.0);//公转
glTranslatef(a, 0.0, 0.0);
glRotatef(day, 0.0, 1.0, 0.0);//自转
glutSolidSphere(0.05, 20, 20);
glPopMatrix();
glDisable(GL_LIGHT0);

void drawOS(GLfloat a)
{
double j; glEnable(GL_LINE_LOOP);
glColor3f(1.0,1.0,0.0);
glLineWidth(0.0005);/////////////////////////////轨道旋转/////////////////////////
    
glPushMatrix();
glRotatef(W,0.0,1.0,0.0);//加入升交点赤经
glPushMatrix();
glRotatef(i,1.0,0.0,0.0);//加入轨道倾角
glPushMatrix();
glRotatef(w,0.0,1.0,0.0);//加入近地点角距
glPushMatrix();
glTranslatef(a*e,0,0);//将轨道由中心平移至焦点
    glScalef(1,sqrt(1-e*e),1);//将圆形轨道缩放为椭圆
glBegin(GL_LINE_LOOP);
   for(j=0;j<2*pi;j+=0.1)
   {
    glVertex3f(a*cos(j),0.0,a*sin(j));
   };
   glEnd();
   
/////////////////////////绘制卫星/////////////////////////////

drawSatellite();//////////////////////////////////////////////////////////////    glPopMatrix();
glPopMatrix();
    glPopMatrix();
glPopMatrix();} 
void *font=GLUT_BITMAP_TIMES_ROMAN_24;
void bitmap_output(int x,int y,char*string)
{int len,i;
glRasterPos2f(x,y);
len =(int)strlen(string);
for (i=1;i<len;i++)
{glutBitmapCharacter(font,string[i]);
}
}void display()
{
GLfloat radius=1;//地球半径
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, Earth);
glPushMatrix();
glRotatef(xoffset, 0.0, 1.0, 0.0);
glPushMatrix();
glRotated(-90,1.0,0.0,0.0);
gluSphere(qobj, radius, 50, 50);
glPopMatrix();
    ////////绘制赤道///////////
glPushMatrix();
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
    double p;
   for(p=0;p<2*pi;p+=0.05)
   {
    glVertex3f(radius*cos(p),0.0,radius*sin(p));
   };
   glEnd();
    glPopMatrix();
////////结束绘制///////////
////////绘制子午线/////////
glPushMatrix();
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
double j;
   for(j=0;j<pi;j+=0.05)
   {
    glVertex3f(0.0,radius*cos(j),radius*sin(j));
   };
   glEnd();
   /////////结束绘制///////////
glPopMatrix();
glPopMatrix(); glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
///////////////////////////////////////////////////////////////////////////////////////
////////////绘制轨道和卫星//////////////////////

drawOS(a); glColor3f(0.0,1.0,0.0);
bitmap_output(-50,-50,"Hello");
////////////////////////////////////////////////////////////////////////////////////////////
glutSwapBuffers(); glFlush();
}void init(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_FILL);//指定二次曲面期望的绘图风格
gluQuadricNormals(qobj, GLU_SMOOTH);//指定二次曲面所期望的法线类型
gluQuadricTexture(qobj, GL_TRUE);//指定是否期望为二次曲面产生纹理 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T); glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0f); 
}static void idle(void)
{
xoffset += 0.1;
day+=1; if (xoffset >= 360) {
xoffset = 0.0;
}
glutPostRedisplay();
}void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w, 2.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h, 2.0*(GLfloat)w/(GLfloat)h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
///////////////////////////
////////////////////////////
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutCreateWindow("Earth");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
init();
Earth = load_texture("map.bmp");
///////////////////////////////
//glClear(GL_COLOR_BUFFER_BIT);
//glPushMatrix();
//glPopMatrix();
//glMatrixMode(GL_PROJECTION);
////////////////////////////
glutMainLoop();

return 0;
}

解决方案 »

  1.   

    新建个 MFC的工程把这个程序加入工程呗,直接转是转不了的
      

  2.   

    就是不会改嘛……
    不知道这些代码在MFC里应该放在哪儿?
      

  3.   

    可以把main函数的实现放在ondraw里面,其他写成函数的形式
      

  4.   

    应该是把Display函数放在ondraw中吧?
    不过我试过了,还是有问题。
      

  5.   

    把main()函数放在MainFrame里试一试
      

  6.   

    把main放在initialdialog() 去除main里面的循环画glutMainLoop()
    把glutMainLoop()放在ondraw()
    试试