c++ - 基本 GL 函数 glTranslatef 似乎不起作用

标签 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() 不存在

最佳答案

您在display 中重置了您的矩阵,因此来自键事件处理程序的glTranslate* 丢失了。重新考虑您要实现的目标。

关于c++ - 基本 GL 函数 glTranslatef 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4571898/

相关文章:

c++ - 为什么 nullptr 在 C++11 中是小写的?

c++ - 判断包是否为空

c++ - 在 C++ 中写入注册表时出错

java - 对于 3d CAD 程序,我是否应该删除对顶点数组/显示列表的使用,转而使用顶点缓冲区对象?

c++ - 窗口坐标到摄像机角度?

c++ - C win32 应用程序

c++ - C++函数调用到RISC-V系统调用

java - JOGL 中的文本困难

c - 一种语言在我们需要语言绑定(bind)的库中留下了什么 'mark'?

opengl - 透视划分解释?