gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:08:53 25 4
gpt4 key购买 nike

我正在关注 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com