该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我在vc里配置了opengl环境,运行一些简单的glut的程序也完全OK,这个程序我检查了很久也搞不懂为什么编译不过去,求大神解...
#include
#include
#include
#include
#define BUFFER 512
GLfloat point[8][3] = {
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f },
{ 0.0f, 1.0f, 0.0f },
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 1.0f },
{ 1.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f }
};
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
static GLfloat xm = 0.0f;
static GLfloat ym = 0.0f;
static GLfloat zm = 0.0f;
static GLfloat xp = 0.0f;
static GLfloat yp = 0.0f;
static GLfloat zp = 0.0f;
static GLfloat lightdx = 0.0f;
static GLfloat lightdy = 0.0f;
static GLfloat lightdz = 0.0f;
static GLfloat directiondx = 0.0f;
static GLfloat directiondz = 0.0f;
static GLint count = 0;
static GLint movebyo = 0;
static GLint movebyw = 0;
static GLint deskc = 0;
static GLint ballc = 0;
static GLint teapotc = 0;
static GLint lighth = 0;
static GLint lightj = 0;
GLfloat value = 75.0f;
GLuint Texture[2];
void init();
void light0();
void light1();
GLuint loadBMP_custom(const char * filepath);
void face(int p1, int p2, int p3, int p4);
void cube();
void desk();
void ball();
void teapot();
void glass();
void showconfig();
void draw(GLenum mode);
void display();
void processHits(GLint hits, GLuint buffer[]);
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void rotate(int key, int x, int y);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("CG");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(rotate);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
Texture[0] = loadBMP_custom("C:\Users\Admin\Documents\课设用\CG\desk.bmp");//自己的图片的位置//
Texture[1] = loadBMP_custom("C:\Users\Admin\Documents\课设用\CG\face.bmp");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
light0();
light1();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //指定混合函数
glEnable(GL_BLEND); //启用混合状态
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-value, value, -value, value, -value, value);
}
void light0()//环境光
{
GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat mat_shininess[] = { 50.0f };
GLfloat light0_position[] = { 1.0f, -1.0f, 1.0f, 0.0f };
GLfloat light[] = { 1.0f, 1.0f, 0.0f, 1.0f };
//GLfloat lmodel_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position); //指定光源的位置
glLightfv(GL_LIGHT0, GL_DIFFUSE, light); //设定漫反射效果
glLightfv(GL_LIGHT0, GL_SPECULAR, light); //设定高光反射效果
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); //设定全局环境光
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void light1()//聚光灯
{
GLfloat light1_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat light1_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light1_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light1_position[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat spot_direction[] = { -1.0f, -1.0f, -1.0f, 0.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5f); //常数衰减因子
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5f); //线性衰减因子
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2f);//二次衰减因子
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 90.0f);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 128.0f);//聚光指数
glEnable(GL_LIGHT1);
}
GLuint loadBMP_custom(const char * filepath)
{
unsigned char header[54];
unsigned char * data;
unsigned int bmp_width;
unsigned int bmp_height;
FILE * file = fopen(filepath, "rb");
if (file != NULL){
fread(header, 1, 54, file);
bmp_width = *(int*)&(header[0x12]);
bmp_height = *(int*)&(header[0x16]);
}
data = (unsigned char *)malloc(bmp_width*bmp_height * 3);
fread(data, 1, bmp_height*bmp_width * 3, file);
fclose(file);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, bmp_width, bmp_height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
free(data);
return textureID;
}
void face(int p1, int p2, int p3, int p4)
{
glActiveTexture(GL_TEXTURE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture[1]);
glActiveTexture(GL_TEXTURE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(point[p1]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(point[p2]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(point[p3]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(point[p4]);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void cube()
{
face(0, 3, 5, 1);
face(0, 2, 6, 3);
face(0, 1, 4, 2);
face(1, 4, 7, 5);
face(2, 6, 7, 4);
face(3, 5, 7, 6);
}
void desk()
{
glColor3f(1.0f, 1.0f, 1.0f);
if (deskc % 2 == 1){
glColor3f(1.0f, 0.0f, 0.0f);
}
glPushMatrix();
glTranslatef(0.0f, 25.0f, 0.0f);
glScalef(100.0f, 4.0f, 70.0f);
glutSolidCube(1.0); //桌面
glPopMatrix();
glPushMatrix();
glTranslatef(-50.0f, 27.0f, -35.0f);
glScalef(100.0f, 1.0f, 70.0f);
cube();
glPopMatrix();
GLfloat mat_ambient[] = { 0.329412f, 0.223529f, 0.027451f, 1.000000f };
GLfloat mat_diffuse[] = { 0.780392f, 0.568627f, 0.113725f, 1.000000f };
GLfloat mat_specular[] = { 0.992157f, 0.941176f, 0.807843f, 1.000000f };
GLfloat mat_shininess[] = { 27.897400f };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glColor3f(0.729412f, 0.564706f, 0.400000f);
if (deskc % 2 == 1){
glColor3f(1.0f, 0.0f, 0.0f);
}
glPushMatrix();
glTranslatef(-48.0f, 0.0f, -33.0f);
glScalef(8.0f, 25.0f, 8.0f);
cube(); //桌脚
glPopMatrix();
glPushMatrix();
glTranslatef(-48.0f, 0.0f, 25.0f);
glScalef(8.0f, 25.0f, 8.0f);
cube();
glPopMatrix();
glPushMatrix();
glTranslatef(40.0f, 0.0f, -33.0f);
glScalef(8.0f, 25.0f, 8.0f);
cube();
glPopMatrix();
glPushMatrix();
glTranslatef(40.0f, 0.0f, 25.0f);
glScalef(8.0f, 25.0f, 8.0f);
cube();
glPopMatrix();
}
void ball()
{
GLfloat mat1_ambient[] = { 0.105882f, 0.058824f, 0.113725f, 1.000000f };
GLfloat mat1_diffuse[] = { 0.427451f, 0.470588f, 0.541176f, 1.000000f };
GLfloat mat1_specular[] = { 0.333333f, 0.333333f, 0.521569f, 1.000000f };
GLfloat mat1_shininess[] = { 9.846150f };
GLfloat mat2_ambient[] = { 0.021500f, 0.174500f, 0.021500f, 0.550000f };
GLfloat mat2_diffuse[] = { 0.075680f, 0.614240f, 0.075680f, 0.550000f };
GLfloat mat2_specular[] = { 0.633000f, 0.727811f, 0.633000f, 0.550000f };
GLfloat mat2_shininess[] = { 76.800000f };
GLfloat mat3_ambient[] = { 0.247250f, 0.224500f, 0.064500f, 1.000000f };
GLfloat mat3_diffuse[] = { 0.346150f, 0.314300f, 0.090300f, 1.000000f };
GLfloat mat3_specular[] = { 0.797357f, 0.723991f, 0.208006f, 1.000000f };
GLfloat mat3_shininess[] = { 83.199997 };
glColor3f(0.647059f, 0.674510f, 0.541176f);//白蜡
glPushMatrix();
glTranslatef(-10.0f, 33.0f, 15.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat1_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat1_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat1_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat1_shininess);
glutSolidSphere(5.0f, 50, 30);
glPopMatrix();
glColor3f(0.403922f, 0.686275f, 0.356863f);//翡翠
glPushMatrix();
glTranslatef(-20.0f, 33.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat2_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat2_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat2_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat2_shininess);
glutSolidSphere(5.0f, 50, 30);
glPopMatrix();
glColor3f(1.0f, 0.843137f, 0.0f);//亮金
glPushMatrix();
glTranslatef(-30.0f, 33.0f, -15.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat3_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat3_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat3_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat3_shininess);
glutSolidSphere(5.0f, 50, 30);
glPopMatrix();
}
void teapot()
{
GLfloat mat1_ambient[] = { 0.105882f, 0.058824f, 0.113725f, 1.000000f };
GLfloat mat1_diffuse[] = { 0.427451f, 0.470588f, 0.541176f, 1.000000f };
GLfloat mat1_specular[] = { 0.333333f, 0.333333f, 0.521569f, 1.000000f };
GLfloat mat1_shininess[] = { 9.846150f };
glColor3f(0.447059f, 0.392157f, 0.607843f);
if (teapotc % 2 == 1){
glColor3f(1.0f, 0.0f, 0.0f);
}
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT, mat1_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat1_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat1_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat1_shininess);
glTranslatef(20.0f, 35.5f, 0.0f);
glutSolidTeapot(10.0);
glPopMatrix();
}
void glass()
{
GLUquadricObj *cylinder_obj;
cylinder_obj = gluNewQuadric();
glColor4f(0.647059f, 0.164706f, 0.164706f, 0.6f);
glPushMatrix();
glTranslatef(20.0f, 50.0f, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
//gluDisk(cylinder_obj, 0.0f, 20.0f, 20.0f, 30.0f);
gluCylinder(cylinder_obj, 20.0f, 20.0f, 22.0f, 20.0f, 30.0f);
glPopMatrix();
}
void showconfig()
{
//system("clear");
printf("\"鼠标点击物体\":物体变成红色\t\t");
printf("\n\n\"h\":控制环境光\t");
if (lighth % 2 == 0) printf("开启\t\t");
else printf("关闭\t");
printf("\"j\":控制聚光灯\t");
if (lightj % 2 == 0) printf("开启\n");
else printf("关闭\n");
printf("\n\"space\":切换投影模式\t");
if (count % 2 == 0) {
printf("启用:平行投影\n");
printf("\n物体移动:\n");
printf("\t\"w\":向里移动\t\"s\":向外移动\t");
printf("\t\"a\":向左移动\t\"d\":向右移动\t");
printf("\t\"c\":向上移动\t\"z\":向下移动\n");
}
if (count % 2 == 1) {
printf("启用:透视投影\n");
printf("\n观察点移动:\n");
printf("\t\"w\":向里移动\t\"s\":向外移动\t");
printf("\t\"a\":向左移动\t\"d\":向右移动\t");
printf("\t\"c\":向上移动\t\"z\":向下移动\n\n");
printf("\"y\":切换光源是否随观察点运动\t");
if (movebyw % 2 == 0) printf("启用:光源不随观察点运动\n");
if (movebyw % 2 == 1) printf("启用:光源随观察点运动\n");
}
printf("\n\"t\":切换光源是否随物体运动\t");
if (movebyo % 2 == 0) printf("启用:光源不随物体运动\n");
if (movebyo % 2 == 1) printf("启用:光源随物体运动\n");
printf("\n光源独立运动:\n");
printf("\t\"5\":向里移动\t\"0\":向外移动\t");
printf("\t\"8\":向上移动\t\"2\":向下移动\t");
printf("\t\"4\":向左移动\t\"6\":向右移动\n");
printf("\n物体旋转:\n");
printf("\t\"↑\":向里旋转\t\"↓\":向外旋转\t");
printf("\t\"←\":向左旋转\t\"→\":向右旋转\n");
printf("\n聚光灯旋转:\n");
printf("\t\"g\":向里旋转\t\"b\":向外旋转\t");
printf("\t\"v\":向左旋转\t\"n\":向右旋转\n");
}