使用OpenGL的C ++ - 绘制正方形

时间:2013-04-25 11:00:01

标签: c++ visual-studio opengl

我必须制作一个应该绘制一个改变颜色的正方形的程序。 该程序将绘制一个白色背景窗口,尺寸为256x256像素,红色正方形,左上角坐标(x,y)=(30,226),右下角坐标(x,y)=(226,30) )。当按下键'a'(键码= 97)时,方块应该粘在蓝色上。当按下'v'键(keycode = 118)时,方块应该返回红色。当按下ESC键(键码= 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();
}

1 个答案:

答案 0 :(得分:1)

使用GLUT构建内容时,您需要将代码与glut库链接起来。尝试谷歌如何链接视觉工作室与其他第三方库。在您的情况下,我相信您需要添加另一个库目录,而不是在项目链接器属性中添加额外的依赖项(glut.lib)。