Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GLUT OpenGL glRotatef 我一直试图用OpenGL、GLUT和C++来创建一个非常简单的小行星游戏。_C++_Opengl_Glut - Fatal编程技术网

GLUT OpenGL glRotatef 我一直试图用OpenGL、GLUT和C++来创建一个非常简单的小行星游戏。

GLUT OpenGL glRotatef 我一直试图用OpenGL、GLUT和C++来创建一个非常简单的小行星游戏。,c++,opengl,glut,C++,Opengl,Glut,我一辈子都搞不清楚为什么会发生这种情况,但按“a”或“d”键时,我希望三角形沿Z轴旋转。问题是,当按下这些键时,glRotatef()会围绕屏幕的0,0位置或左下角旋转。我尝试过许多不同的方法和教程,我将链接的代码是最好的运行示例 我确信我遗漏了一些东西,但我无法让三角形围绕自身旋转,而不是屏幕的原点 #include <iostream> #include <windows.h> #include <GL/gl.h> #include <GL/glu.

我一辈子都搞不清楚为什么会发生这种情况,但按“a”或“d”键时,我希望三角形沿Z轴旋转。问题是,当按下这些键时,glRotatef()会围绕屏幕的0,0位置或左下角旋转。我尝试过许多不同的方法和教程,我将链接的代码是最好的运行示例

我确信我遗漏了一些东西,但我无法让三角形围绕自身旋转,而不是屏幕的原点

#include <iostream>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>  
#include <GL/glut.h>

//g++ -o game.exe -Wall main.cpp glut32.lib -lopengl32 -lglu32 -static-libgcc -static-libstdc++

float posX = 0, posY = 0, posZ = 0;

int width = 100, height = 100;
int triangle_height = 5, triangle_width = 4;
float move_unit = 1;

void display() {

    std::cout << "display" << std::endl;

    //glLoadIdentity();

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(posX, posY, posZ);
    glColor3f(1,1,1); //sets the current color to white
        glBegin(GL_TRIANGLES); //tells openGL we are going to draw some triangles

            //glTranslatef(0, 0, 0);
            glVertex2i( width/2 , height/2 ); //specifies the first vertext of our triangle
            glVertex2i( width/2 + triangle_width , height/2 ); //specifies the second vertext of our triangle
            glVertex2i( width/2 , height/2 + triangle_height); //specifies the third vertext of our triangle

        glEnd(); //tells openGL that we are done drawing

    glPopMatrix();
    glutSwapBuffers();

    glFlush();

}

void keyboard(unsigned char key, int x, int y) {

    switch(key) {

        case 'w':
            std::cout << "moving up" << std::endl;
            posY += move_unit;
        break;

        case 's':
            std::cout << "moving down" << std::endl;
            posY -= move_unit;
        break;

        case 'a':
            std::cout << "rotate left" << std::endl;
            glRotatef(5.0f, 0.0f, 0.0f, 1.0f);
        break;

        case 'd':
            std::cout << "rotate right" << std::endl;
            glRotatef(5.0f, 0.0f, 0.0f, -1.0f);
        break;

    }

    glutPostRedisplay();

}

void init() {

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION); //changes the current matrix to the projection matrix
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);

}

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

    glutInit(&argc, argv); //inits the GLUT framework

    glutInitWindowSize(800, 600);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("Eric's Game");

    init();

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop(); // Infinite event loop

    return 0;

}
#包括
#包括
#包括
#包括
#包括
//g++-o game.exe-Wall main.cpp glut32.lib-lopengl32-lglu32-static libgcc-static libstdc++
浮点数posX=0,posY=0,posZ=0;
内部宽度=100,高度=100;
int三角形高度=5,三角形宽度=4;
浮动移动单位=1;
无效显示(){

std::cout因为旋转应用于原点,所以应该连接三个变换:
1.首先在原点上平移三角形
2.然后应用旋转

3.在这之后,您反转第一次平移,以便将其放回原始位置。

OpenGL以相反的顺序应用矩阵。换句话说,如果要旋转然后平移,则需要在
glTranslatef
之后调用
glRotatef
。最好的方法是存储当前的旋转表达式合法地,而不仅仅是对
glRotatef
进行一系列调用:

float rotation = 0.0;

void display() {
    // ...
    glPushMatrix();
    glTranslatef(posX, posY, posZ);
    glRotatef(rotation, 0.0f, 0.0f, 1.0f);
    // Rewrite triangle vertex coordinates to be centered at (0, 0)
    glPopMatrix();
    // ...
}

void keyboard(unsigned char key, int x, int y) {
    switch(key) {
        // ...
        case 'a':
            std::cout << "rotate left" << std::endl;
            rotation += 5.0f;
            break;

        case 'd':
            std::cout << "rotate right" << std::endl;
            rotation -= 5.0f;
            break;
    }
    // ...
}
float旋转=0.0;
无效显示(){
// ...
glPushMatrix();
glTranslatef(posX,posY,posZ);
glRotatef(旋转,0.0f,0.0f,1.0f);
//重写三角形顶点坐标,使其居中于(0,0)
glPopMatrix();
// ...
}
无效键盘(无符号字符键,整数x,整数y){
开关(钥匙){
// ...
案例“a”:

std::你的建议很好地发挥了作用,使用旋转变量就像是一个“duh”时刻,哈哈。但是,我该如何在屏幕上使对象居中呢?这一直是我的问题,我希望三角形在屏幕上居中,而不是左下角。
glVertex2i(0,0);
glVertex2i(0+三角形宽度,0);
glVertex2i(0,0+三角形高度);
初始化
posX
posY
为屏幕宽度/高度的一半,你应该很好。