我必须编写一个程序来绘制一个可以改变颜色的正方形。该程序将绘制一个白色背景的窗口,尺寸为 256x256 像素,一个红色正方形,左上顶点坐标 (x, y) = (30, 226) 右下角坐标 (x, y) = (226 , 30 )。当按下“a”键(keycode = 97)时,方块应保持蓝色。当按下“v”键(keycode = 118)时,方块应该变回红色。当按下 ESC 键 (keycode = 27) 时,程序应该终止。
——有日志……
Build Log
Build started:
Project: square, Configuration: Debug|Win32
Command Lines
Creating temporary file "c:\Users\TEMP\Documents\Visual Studio 2008\Projects\square\square\Debug\RSP00000544445896.rsp" with contents
[
/OUT:"C:\Users\TEMP\Documents\Visual Studio 2008\Projects\square\Debug\square.exe" /MANIFEST /MANIFESTFILE:"Debug\square.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\TEMP\Documents\Visual Studio 2008\Projects\square\Debug\square.pdb" /DYNAMICBASE /NXCOMPAT /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
".\Debug\square.obj"
]
Creating command line "link.exe @"c:\Users\TEMP\Documents\Visual Studio 2008\Projects\square\square\Debug\RSP00000544445896.rsp" /NOLOGO /ERRORREPORT:PROMPT"
Output Window
Linking...
square.obj : error LNK2019: unresolved external symbol __imp____glutInitWithExit@12 referenced in function _glutInit_ATEXIT_HACK@8
square.obj : error LNK2019: unresolved external symbol __imp____glutCreateWindowWithExit@8 referenced in function _glutCreateWindow_ATEXIT_HACK@4
C:\Users\TEMP\Documents\Visual Studio 2008\Projects\square\Debug\square.exe : fatal error LNK1120: 2 unresolved externals
Results
Build log was saved at "file://c:\Users\TEMP\Documents\Visual Studio 2008\Projects\square\square\Debug\BuildLog.htm"
square - 3 error(s), 0 warning(s)
代码:
#include <GL/glut.h>
// Function callback that is called to manage the keyboard tasks
float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
void GerenciaTeclado(unsigned char key, int x, int y)
{
switch (key) {
case 'a':// change the actual color to red
r = 1.0f;
g = 0.0f;
b = 0.0f;
break;
case 'v':// change de color to blue
r = 0.0f;
g = 0.0f;
b = 1.0f;
break;
case 27:// close the screen
exit(0);
break;
}
glutPostRedisplay();
}
// Function callback that is called to draw
void Desenha(void)
{
// Clean the window
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Initializes the coordinates system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Draw a square
glBegin(GL_QUADS);
// Shows that the color is red
// R G B
glColor3f(r, g, b);
glVertex2f(-1, -1);
glVertex2f( 1, -1);
// Shows that the color is blue
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f( 1, 1);
glVertex2f(-1, 1);
glEnd();
glutSwapBuffers();
}
// Main Program
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(256,256);
glutInitWindowPosition(10,10);
glutCreateWindow("Quadrado");
glutDisplayFunc(Desenha);
glutKeyboardFunc(GerenciaTeclado);
glutMainLoop();
}