【问题标题】:OpenGL - The object moving everytime the menu clickedOpenGL - 每次单击菜单时移动的对象
【发布时间】:2018-05-24 14:43:19
【问题描述】:

我正在开发一个项目,在添加用于更改颜色的菜单后,窗口中的对象或形状会更改颜色,但每次选择菜单中的项目(例如颜色)时,都会以其他方式移动或超出范围。我也尝试将 GLUT_DOUBLE 更改为 GLUT_SINGLE,但仍然没有成功。

#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4 //white colour for sphere


float red =1.0,green =1.0,blue=0.0,white = 0.0;

void init(void) {   
    glClearColor (0.0, 0.0, 0.0, 0.0);   
    glShadeModel (GL_FLAT);
}

void display(void){
    // Sphere   
    glClear (GL_COLOR_BUFFER_BIT);   
    glColor3f(red,green,blue);   
    glPushMatrix();   
    glTranslatef (-3.5, -1.5, 0.0);   
    glTranslatef (1.0, 0.0, 0.0);   
    glPushMatrix();   
    glScalef (3.0, 3.0, 0.0);   
    glutSolidSphere(0.4,40,40);   
    glPopMatrix();    
    //Cone   
    glColor3f(0.0,0.0,0.0);   
    glTranslatef (0.95,-0.2, 0.0);   
    glPushMatrix();   
    glRotated(300,1.0,4.0,1.0);   
    glutSolidCone(0.6, 0.9, 30, 30);   
    glPopMatrix();   
    //Sphere(eye)   
    glColor3f(0.0,0.0,0.0);   
    glTranslatef (-0.75,0.8, 0.0);   
    glPushMatrix();   
    glScalef(0.6,0.6,0.0);   
    glutSolidSphere(0.2,40,40);   
    glPopMatrix();    
    glFlush();
}

void reshape (int w, int h){   
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);    
    glMatrixMode (GL_PROJECTION);   
    glLoadIdentity ();   
    gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);   
    glMatrixMode(GL_MODELVIEW);   
    glLoadIdentity();   
    glTranslatef (0.0, 0.0, -5.0);
}

void mouse(int option){
    switch(option){
    case RED:
        red = 1.0,green = 0.0,blue = 0.0;
        break;
    case GREEN:
        red = 0.0,green = 1.0,blue = 0.0;
        break;
    case BLUE:
        red = 0.0,green = 0.0,blue = 1.0;
        break;
    case WHITE:
        red = 1.0,green = 1.0,blue = 1.0;
        break;
    }
    glutPostRedisplay();
}

int main(int argc, char** argv){   
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   
    glutInitWindowSize (700, 500);    
    glutInitWindowPosition (100, 100);   
    glutCreateWindow ("Testing");
    glutDisplayFunc(display);    
    glutReshapeFunc(reshape);
    int sphere =  glutCreateMenu(mouse); //change colour of pacman 
    glutAddMenuEntry("Red",RED);
    glutAddMenuEntry("Green",GREEN);
    glutAddMenuEntry("Blue",BLUE);
    glutAddMenuEntry("White",WHITE);
    glutCreateMenu(mouse);
    glutAddSubMenu("Pacman",sphere);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    init ();  
    glutMainLoop(); 
    return 0;
}

【问题讨论】:

    标签: c++ visual-studio opengl glut


    【解决方案1】:

    你必须为每个glPushMatrix 做一个glPopMatrix。函数display中缺少1个glPopMatrix

    void display(void){
        // Sphere   
        glClear (GL_COLOR_BUFFER_BIT);   
        glColor3f(red,green,blue);   
        glPushMatrix();   
            glTranslatef (-3.5, -1.5, 0.0);   
            glTranslatef (1.0, 0.0, 0.0);   
            glPushMatrix();   
                glScalef (3.0, 3.0, 0.0);   
                glutSolidSphere(0.4,40,40);   
            glPopMatrix();    
            //Cone   
            glColor3f(0.0,0.0,0.0);   
            glTranslatef (0.95,-0.2, 0.0);   
            glPushMatrix();   
                glRotated(300,1.0,4.0,1.0);   
                glutSolidCone(0.6, 0.9, 30, 30);   
            glPopMatrix();   
            //Sphere(eye)   
            glColor3f(0.0,0.0,0.0);   
            glTranslatef (-0.75,0.8, 0.0);   
            glPushMatrix();   
                glScalef(0.6,0.6,0.0);   
                glutSolidSphere(0.2,40,40);   
            glPopMatrix();    
        glPopMatrix(); // <--------------------- this is missing
        glFlush();
    }
    

    由于缺少弹出,第一次渲染的翻译保留在矩阵堆栈的顶部。当使用新颜色第二次完成渲染时,将再次应用翻译。这导致位置的逐渐变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2019-02-15
      • 2014-12-26
      相关资源
      最近更新 更多