Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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
C++ Opengl相机和渲染问题_C++_Opengl - Fatal编程技术网

C++ Opengl相机和渲染问题

C++ Opengl相机和渲染问题,c++,opengl,C++,Opengl,我正在尝试OpenGL,有一些问题: 我实际上有这样的代码: #include <freeglut.h> #include <glut.h> #include <stdio.h> using namespace std; char presse; int anglex=-45, angley=45, x, y, xold, yold; /* Prototype des fonctions (func

我正在尝试OpenGL,有一些问题: 我实际上有这样的代码:

    #include <freeglut.h>
    #include <glut.h> 
    #include <stdio.h>
    using namespace std;

    char presse;
    int anglex=-45, angley=45, x, y, xold, yold;

    /* Prototype des fonctions (function prototypes) */
    void affichage();
    void clavier(unsigned char touche, int x, int y);
    void reshape(int x, int y);
    void idle();
    void mouse(int bouton, int etat, int x, int y);
    void mousemotion(int x, int y);


    const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
    const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat light_position[] = { 1, 2, 1, 1 };

    const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
    const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat high_shininess[] = { 100.0f };

    int main(int argc, char **argv)
    {
    /* initialisation de glut et creation
    de la fenetre (glut initialization and window creation) */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(200, 200);
    glutInitWindowSize(1000, 1000);
    glutCreateWindow("cube");

    /* Initialisation d'OpenGL (OpenGL initialisation) */
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glColor3f(1.0, 1.0, 1.0);
    glPointSize(2.0);

    glEnable(GL_DEPTH_TEST);    // Active le test de profondeur
(activate depth testing)
    glEnable(GL_LIGHTING);  // Active l'éclairage
(activate lighting)
    glEnable(GL_LIGHT0);    // Allume la lumière n°1
(turn on light no. 1)
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);

    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    /* enregistrement des fonctions de rappel (register callbacks) */
    glutDisplayFunc(affichage);
    glutKeyboardFunc(clavier);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMotionFunc(mousemotion);

    /* Entree dans la boucle principale glut (entry of the main glut loop) */
    glutMainLoop();
    return 0;
    }


    double a = 0;
    int LightPos[4] = { -1, 4, -1, 1 };
    int MatSpec[4] = { 1, 1, 1, 1 };

    void affichage()
    {   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_MODELVIEW);
    glViewport(0, 0, 1000, 1000);

    a = +1;


    glLoadIdentity();

    glRotatef(angley, 1.0, 0.0, 0.0);
    glRotatef(anglex, 0.0, 1.0, 0.0);

    for (int k = 0; k < 5; k++){
        if (k == 0){
            glBegin(GL_LINES);
            glColor3f(1.0, 1.0, 1.0);
            glVertex3f(0, 0, 0);
            glVertex3f(1, 0, 0);
            glEnd();
        }
        else {
            if (k > 0){
                        glBegin(GL_LINES);
                            glColor3f(1.0, 0.0, 0);
                            glVertex3f(0.5, 0, 0);
                            glVertex3f(0.5, 0, 0.5);
                        glEnd();

                    }
            glBegin(GL_LINES);
                glColor3f(1.0, 0.0, 0);
                glVertex3f(k, 0, 0);
                glVertex3f(k, 0, 0.5);
            glEnd();


        }

        }


    for (int k = 0; k < 5; k++){
        if (k == 0){
            glBegin(GL_LINES);
                glColor3f(1.0, 0.0, 1.0);
                glVertex3f(0, 0, 0);
                glVertex3f(0, 5, 0);
            glEnd();
        }
        else {
            if (k >0){
                GLfloat n = k / 2;
                glBegin(GL_LINES);
                    glColor3f(0.0, 1.0, 0);
                    glVertex3f(0, 0.5, 0);
                    glVertex3f(0.5, 0.5, 0);
                glEnd();
            }
            glBegin(GL_LINES);
                glColor3f(0.0, 1.0, 0);
                glVertex3f(0, k, 0);
                glVertex3f(0.5, k, 0);
            glEnd();


        }
    }


    for (int k = 0; k < 5; k++){
        if (k == 0){
            glBegin(GL_LINES);
            glColor3f(1.0, 0.6, 1.0);
            glVertex3f(0, 0, 0);
            glVertex3f(0, 0, 5);
            glEnd();
        }
        else {
            if (k >0){
                GLfloat n = k / 2;
                glBegin(GL_LINES);
                glColor3f(0.0, 0.0, 1.0);
                glVertex3f(0, 0, 0.5);
                glVertex3f(0, 0.5, 0.5);
                glEnd();
            }
            glBegin(GL_LINES);
            glColor3f(0.0, 0.0, 10);
            glVertex3f(0, 0, k);
            glVertex3f(0, 0.5, k);
            glEnd();


        }
    }



    // Par terre scene
(ground scene)
    glBegin(GL_QUADS);
    glColor3f(1.0, 1.0, 1.0);
    glVertex3d(0,-0.1,0);
    glVertex3d(5, -0.1, 0);
    glVertex3d(5, -0.1, 5);
    glVertex3d(0, -0.1, 5);

        glEnd();



        glFlush();

       glutSwapBuffers();
       }

    void clavier(unsigned char touche, int x, int y)
    {
    switch (touche)
    {


    case 'o': /* eteindre la lumiere (turn off the light) */
        glDisable(GL_LIGHT0);
        glutPostRedisplay();
        //std::cout << "lumiere eteinte";

        break;
    case 'a': /* eteindre la lumiere (turn off the light) */
        glEnable(GL_LIGHT0);
        glutPostRedisplay();
        break;
    case 'p': /* affichage du carre plein (normal fill mode, interior of the polygon is filled) */
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glutPostRedisplay();
        break;
    case 'f': /* affichage en mode fil de fer (draw polygon edges as lines) */
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glutPostRedisplay();
        break;
    case 's': /* Affichage en mode sommets seuls (draw polygon vertices as points) */
        glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
        glutPostRedisplay();
        break;
    case 'd':
        glEnable(GL_DEPTH_TEST);
        glutPostRedisplay();
        break;
    case 'D':
        glDisable(GL_DEPTH_TEST);
        glutPostRedisplay();
        break;
    case 'q': /*la touche 'q' permet de quitter le programme (the key 'q' exits the program) */
        exit(0);
     }
     }

     void reshape(int x, int y)
    {
    if (x<y)
        glViewport(0, (y - x) / 2, x, x);
    else
        glViewport((x - y) / 2, 0, y, y);
    }

    void mouse(int button, int state, int x, int y)
    {
    /* si on appuie sur le bouton gauche (if you press the left button) */
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        presse = 1; /* le booleen presse passe a 1 (vrai) (the boolean presse is set to 1 (true)) */
        xold = x; /* on sauvegarde la position de la souris (save the position of the mouse) */
        yold = y;
    }
    /* si on relache le bouton gauche (if you release the left button) */
    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
        presse = 0; /* le booleen presse passe a 0 (faux) (the boolean presse is set to 0 (false)) */
    }

    void mousemotion(int x, int y)
    {
    if (presse) /* si le bouton gauche est presse (if the left button is pressed) */
    {
        /* on modifie les angles de rotation de l'objet
        en fonction de la position actuelle de la souris et de la derniere
        position sauvegardee (modify the rotation angles of the object
depending on the current position of the mouse and the last
saved position) */
        anglex = anglex + (x - xold);
        angley = angley + (y - yold);
        glutPostRedisplay(); 
    }

    xold = x; 
    yold = y;
    }
#包括
#包括
#包括
使用名称空间std;
炭压力;
int anglex=-45,angley=45,x,y,xold,yold;
/*原型功能(功能原型)*/
无效附加();
无效键盘(无符号字符接触,整数x,整数y);
空洞重塑(int x,int y);
无效空闲();
无效鼠标(int bouton、int etat、int x、int y);
虚无情绪(int x,int y);
const GLfloat light_ambient[]={0.0f,0.0f,0.0f,1.0f};
const GLfloat light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat light_specular[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat light_position[]={1,2,1,1};
常数GLfloat mat_ambient[]={0.7f,0.7f,0.7f,1.0f};
常数GLfloat mat_diffuse[]={0.8f,0.8f,0.8f,1.0f};
常量GLfloat mat_镜面反射[]={1.0f,1.0f,1.0f,1.0f};
常量GLfloat高亮度[]={100.0f};
int main(int argc,字符**argv)
{
/*初始化glut et创建
de la fenetre(glut初始化和窗口创建)*/
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_深度);
位置(200200);
glutInitWindowSize(10001000);
创建窗口(“立方体”);
/*OpenGL初始化(OpenGL初始化)*/
glClearColor(0.0,0.0,0.0,0.0);
GL3F(1.0,1.0,1.0);
glPointSize(2.0);
glEnable(GL_深度测试);//主动式深度测试
(激活深度测试)
glEnable(GL_LIGHTING);//主动照明
(激活照明)
glEnable(Glu LIGHT0);//第1号诱惑
(打开1号灯)
glEnable(GL_正常化);
glEnable(GL_颜色_材料);
glLightfv(GL_LIGHT0、GL_AMBIENT、light_AMBIENT);
glLightfv(GL_LIGHT0,GL_漫反射,light_漫反射);
glLightfv(GL_LIGHT0,GL_镜面反射,light_镜面反射);
glLightfv(GLU灯0、GLU位置、灯位置);
GLMATERIALV(GL_前部、GL_环境、mat_环境);
GLMATERIALV(GL_前部、GL_漫反射、mat_漫反射);
GLMATERIALV(GL_前部、GL_镜面反射、mat_镜面反射);
GLMATERIALV(GLU正面、GLU反光度、高反光度);
/*rappel基金注册(注册回调)*/
glutDisplayFunc(附件);
键盘功能(键盘);
GLUTREFORUNC(重塑);
glutMouseFunc(小鼠);
glutMotionFunc(mousemotion);
/*主glut(主glut回路入口)*/
glutMainLoop();
返回0;
}
双a=0;
int LightPos[4]={-1,4,-1,1};
int-MatSpec[4]={1,1,1,1};
无效附加()
{   
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glShadeModel(GL_平滑);
glMatrixMode(GLU模型视图);
glViewport(0,0,1000,1000);
a=+1;
glLoadIdentity();
glRotatef(角度,1.0,0.0,0.0);
glRotatef(anglex,0.0,1.0,0.0);
对于(int k=0;k<5;k++){
如果(k==0){
glBegin(GL_行);
GL3F(1.0,1.0,1.0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
格伦德();
}
否则{
如果(k>0){
glBegin(GL_行);
GL3F(1.0,0.0,0);
glVertex3f(0.5,0,0);
glVertex3f(0.5,0,0.5);
格伦德();
}
glBegin(GL_行);
GL3F(1.0,0.0,0);
glVertex3f(k,0,0);
glVertex3f(k,0,0.5);
格伦德();
}
}
对于(int k=0;k<5;k++){
如果(k==0){
glBegin(GL_行);
GL3F(1.0,0.0,1.0);
glVertex3f(0,0,0);
glVertex3f(0,5,0);
格伦德();
}
否则{
如果(k>0){
gln=k/2;
glBegin(GL_行);
GL3F(0.0,1.0,0);
glVertex3f(0,0.5,0);
glVertex3f(0.5,0.5,0);
格伦德();
}
glBegin(GL_行);
GL3F(0.0,1.0,0);
glVertex3f(0,k,0);
glVertex3f(0.5,k,0);
格伦德();
}
}
对于(int k=0;k<5;k++){
如果(k==0){
glBegin(GL_行);
GL3F(1.0,0.6,1.0);
glVertex3f(0,0,0);
glVertex3f(0,0,5);
格伦德();
}
否则{
如果(k>0){
gln=k/2;
glBegin(GL_行);
GL3F(0.0,0.0,1.0);
glVertex3f(0,0,0.5);
glVertex3f(0,0.5,0.5);
格伦德();
}
glBegin(GL_行);
GL3F(0.0,0.0,10);
glVertex3f(0,0,k);
glVertex3f(0,0.5,k);
格伦德();
}
}
现场场景
(地面场景)
glBegin(GL_QUADS);
GL3F(1.0,1.0,1.0);
glVertex3d(0,-0.1,0);
glVertex3d(5,-0.1,0);
glVertex3d(5,-0.1,5);
glVertex3d(0,-0.1,5);
格伦德();
glFlush();
glutSwapBuffers();
}
无效键盘(无符号字符接触,整数x,整数y)
{
开关(触摸)
{
案例“o”:/*eteindre la lumiere(关灯)*/
glDisable(GL_LIGHT0);
再发现();

//std::cout您没有设置任何特定的投影矩阵,因此您将获得默认的标识矩阵。这意味着查看体积将沿所有三个轴从-1到1。因此,如果对象到达该体积之外,则会被剪切。

我尝试实现glViewport(0,0,(GLsizei)1000,(GLsizei)1000);glMatrixMode(GL_投影);glLoadIdentity();glFrustum(-3.0,3.0,-5.0,5.0,1.0,1.0);glMatrixMode(GL_MODELVIEW);但它在任何方面都没有帮助