#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
HDC hDC;
HGLRC hRC;
HWND hWnd;
bool blink = false;
int blinkTimer = 0;
// 初始化OpenGL
void InitGL() {
glClearColor(0.0f, 0.7f, 1.0f, 1.0f); // 天空蓝背景
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
// 绘制海绵宝宝头部(立方体)
void DrawHead() {
// 黄色头部
glColor3f(1.0f, 1.0f, 0.0f); // 黄色
glutSolidCube(2.0f); // 使用GLUT的立方体
// 添加海绵孔洞效果
glColor3f(0.9f, 0.9f, 0.0f);
for(int i=0; i<50; i++) {
glPushMatrix();
glTranslatef((rand()%100)/100.0f-0.5f,
(rand()%100)/100.0f-0.5f,
1.01f);
glutSolidSphere(0.05f, 8, 8);
glPopMatrix();
}
}
// 绘制眼睛(带眨眼效果)
void DrawEyes() {
// 左眼
glPushMatrix();
glTranslatef(-0.5f, 0.3f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f); // 白眼白
glutSolidSphere(0.3f, 16, 16);
// 眼珠
glTranslatef(0.0f, 0.0f, 0.1f);
glColor3f(0.0f, 0.0f, 0.0f); // 黑色眼珠
if(blink) {
// 眨眼状态 - 画一条线
glBegin(GL_LINES);
glVertex2f(-0.2f, 0.0f);
glVertex2f(0.2f, 0.0f);
glEnd();
} else {
glutSolidSphere(0.1f, 16, 16);
}
glPopMatrix();
// 右眼(同上)
glPushMatrix();
glTranslatef(0.5f, 0.3f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glutSolidSphere(0.3f, 16, 16);
glTranslatef(0.0f, 0.0f, 0.1f);
glColor3f(0.0f, 0.0f, 0.0f);
if(blink) {
glBegin(GL_LINES);
glVertex2f(-0.2f, 0.0f);
glVertex2f(0.2f, 0.0f);
glEnd();
} else {
glutSolidSphere(0.1f, 16, 16);
}
glPopMatrix();
}
// 绘制鼻子
void DrawNose() {
glPushMatrix();
glTranslatef(0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.5f, 0.0f); // 橙色鼻子
glutSolidSphere(0.2f, 16, 16);
glPopMatrix();
}
// 绘制嘴巴
void DrawMouth() {
glPushMatrix();
glTranslatef(0.0f, -0.4f, 1.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for(float angle = 0; angle <= M_PI; angle += 0.1f) {
glVertex2f(cos(angle)*0.6f, sin(angle)*0.3f);
}
glEnd();
glPopMatrix();
}
// 绘制整个海绵宝宝
void DrawSpongeBob() {
glPushMatrix();
glScalef(1.5f, 1.5f, 1.0f); // 稍微压扁
DrawHead();
DrawEyes();
DrawNose();
DrawMouth();
glPopMatrix();
}
// 渲染场景
void Render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); // 摄像机位置
// 添加一些旋转动画
static float angle = 0;
angle += 0.5f;
glRotatef(angle, 0, 1, 0);
DrawSpongeBob();
SwapBuffers(hDC);
}
// 定时器回调(用于眨眼动画)
void CALLBACK TimerProc(HWND hWnd, UINT message, UINT idTimer, DWORD dwTime) {
blinkTimer++;
if(blinkTimer > 100) { // 每100个时间单位眨眼一次
blink = true;
if(blinkTimer > 105) {
blink = false;
blinkTimer = 0;
}
}
Render();
}
// 窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch(message) {
case WM_CREATE:
hDC = GetDC(hWnd);
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,0,0,0,0,0,0,0,
32,
0,0,
PFD_MAIN_PLANE,
0,0,0,0
};
SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
InitGL();
SetTimer(hWnd, 1, 16, TimerProc); // 约60fps
break;
case WM_DESTROY:
KillTimer(hWnd, 1);
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
PostQuitMessage(0);
break;
case WM_SIZE:
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)LOWORD(lParam)/(GLfloat)HIWORD(lParam), 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc = {
CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
WndProc,
0,0,
hInstance,
NULL,
LoadCursor(NULL, IDC_ARROW),
(HBRUSH)(COLOR_WINDOW+1),
NULL,
"SpongeBobWindow"
};
RegisterClass(&wc);
hWnd = CreateWindow("SpongeBobWindow", "海绵宝宝3D动画 - VC6.0 OpenGL",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}这段代码在vc6.0中运行后报错如下:\vcchucun\47\0609\1.c(26) : error C2065: 'glutSolidCube' : undeclared identifier
d:\vcchucun\47\0609\1.c(35) : error C2065: 'glutSolidSphere' : undeclared identifier
d:\vcchucun\47\0609\1.c(96) : error C2065: 'M_PI' : undeclared identifier
d:\vcchucun\47\0609\1.c(97) : warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
d:\vcchucun\47\0609\1.c(97) : warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
d:\vcchucun\47\0609\1.c(170) : error C2360: initialization of 'pfd' is skipped by 'case' label
d:\vcchucun\47\0609\1.c(151) : see declaration of 'pfd'
d:\vcchucun\47\0609\1.c(178) : error C2360: initialization of 'pfd' is skipped by 'case' label
d:\vcchucun\47\0609\1.c(151) : see declaration of 'pfd'
d:\vcchucun\47\0609\1.c(187) : error C2361: initialization of 'pfd' is skipped by 'default' label
d:\vcchucun\47\0609\1.c(151) : see declaration of 'pfd'
Error executing cl.exe.