基本的GL函数glTranslatef似乎没有工作

时间:2010-12-31 19:17:10

标签: c++ opengl

我关注this tutorial,三角形呈现完美,但是当我按下Page Up键时,没有任何反应。

这是我的代码:

// made in Visual Studio Express 2008
// OpenGL3-1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


// if you are not using Visual Studio to compile this then remove stdafx.h

#include <stdlib.h>
#include <windows.h>
#include "glut.h"

void init(void)
{

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel (GL_SMOOTH);

}


void display(void)
{

    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /* Loading the Identity matrix means we reset the screen coordinate system to XYZ axis of lenght 1:
    The screen starts at z=0, x=-1 to x=1 and y=-1 to y=1 */
    glLoadIdentity ();

    glTranslatef(0,0.0f,-6.0f);
    // translate everything by 6 units in the z axis.

    glBegin(GL_TRIANGLES);

        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
        glVertex3f(-1.0f,-1.0f, 0.0f);
        glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
        glVertex3f( 1.0f,-1.0f, 0.0f);

    glEnd(); // Done Drawing A Triangle

    Sleep(5);
    glutSwapBuffers();



}

void reshape (int w, int h)
{
    // just the window reshape function

    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
    glMatrixMode (GL_MODELVIEW);

}

void keyboard(unsigned char key, int x, int y)
{
    // escapes from the program if the ESC key is hit

    switch (key) {

        case 27:
        exit(0);
        break;

    }

}



void keyspecial( int key, int x, int y )
{

    if( key == GLUT_KEY_PAGE_UP) // Page up
    {
        glTranslatef(90.0,0.0,0.0);
        // ...... do what ever you want to do
        glutPostRedisplay(); // redraw everything to reflect the changes

    }
    if (key == GLUT_KEY_PAGE_DOWN)
    {

        // ...... do what ever you want to do 

        glutPostRedisplay();// redraw everything to reflect the changes

    }
    if (key == GLUT_KEY_HOME)
    {

        // ...... do what ever you want to do
        glutPostRedisplay();// redraw everything to reflect the changes

    }
    if (key == GLUT_KEY_END)
    {

        // ...... do what ever you want to do
        glutPostRedisplay();// redraw everything to reflect the changes

    }


}

int main(int argc, char** argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutKeyboardFunc(keyboard); // tell glut to call this function when the user presses a key

    glutSpecialFunc(keyspecial); // tell glut to call this function when the user presses a special a key
    glutMainLoop();
    return 0;

}

注意:

教程建议使用glTranslate(x,y,z)代替glTranslatef(x,y,z)。我认为这是一个错字,因为glTranslate()不存在

2 个答案:

答案 0 :(得分:2)

您正在display重置矩阵,因此关键事件处理程序中的glTranslate*将丢失。重新思考你想要实现的目标。

答案 1 :(得分:1)

你在这个功能中所做的不是正确的事情

void reshape (int w, int h)
{ // just the window reshape function

    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
    glMatrixMode (GL_MODELVIEW);
}

与我联系:不要在重塑处理程序中设置视口大小和投影!

您始终将视口和投影与显示处理程序中的所有其他内容一起设置。这是唯一正确的地方。

接下来,您不使用OpenGL矩阵函数“放置”对象。你只是在操纵变换矩阵,它应该根据对象的位置进行设置,这些矩阵可以很好地存储为矩阵,但与OpenGL状态无关。因此,键盘处理程序应设置一些变量,然后用于在适当的时刻设置模型视图矩阵。

相关问题