标题:请问这个程序问题在哪里?(OpenGL)
取消只看楼主
hopepark
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2008-3-23
结帖率:100%
 问题点数:0 回复次数:0 
请问这个程序问题在哪里?(OpenGL)
我现在先在程序运行时候,点右键,出菜单来,菜单选项有两个,一个是Ortho,一个是Frustum。当点Ortho时候运行
if(w<=h)
   
            glOrtho(-2.0 , 2.0, -2.0*(float)h/(float)w, 2.0*(float)h/(float)w, 1.0, 10.0 );
        else
            glOrtho(-2.0*(float)w/(float)h, 2.0*(float)w/(float)h, -2.0, 2.0, 1.0, 10.0 );
当点Frustum时候运行
if(w<=h)
            glFrustum(-2.0 , 2.0, -2.0*(float)h/(float)w, 2.0*(float)h/(float)w, 1.0, 10.0 );
        else
            glFrustum(-2.0*(float)w/(float)h, 2.0*(float)w/(float)h, -2.0, 2.0, 1.0, 10.0 );
但是现在点了以后没有反应,但是不知道什么问题,请大家指教下啊,小弟感激不尽。
下面是全部代码:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <winUser.h>
#include <math.h>

static float  viewer[3];

int * pointer,a;


static bool mouseLeftDown;
static float point[2][2];
static float theta;
 

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(viewer[0], viewer[1], viewer[2], 0, 0, 0, 0, 1, 0);
    glRotatef(theta,0,1,0);
    GLfloat vertices[8][3] = {{-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1},{-1,-1,-1},{-1,1,-1},{1,1,-1},{1,-1,-1}};
    GLfloat colors[8][3] = {{0,0,1},{0,1,1},{1,1,1},{1,0,1},{0,0,0},{0,1,0},{1,1,0},{1,0,0}};

    glBegin(GL_QUADS);
        glColor3fv(colors[0]); glVertex3fv(vertices[0]);
        glColor3fv(colors[3]); glVertex3fv(vertices[3]);
        glColor3fv(colors[2]); glVertex3fv(vertices[2]);
        glColor3fv(colors[1]); glVertex3fv(vertices[1]);

        glColor3fv(colors[2]); glVertex3fv(vertices[2]);
        glColor3fv(colors[3]); glVertex3fv(vertices[3]);
        glColor3fv(colors[7]); glVertex3fv(vertices[7]);
        glColor3fv(colors[6]); glVertex3fv(vertices[6]);
   
        glColor3fv(colors[3]); glVertex3fv(vertices[3]);
        glColor3fv(colors[0]); glVertex3fv(vertices[0]);
        glColor3fv(colors[4]); glVertex3fv(vertices[4]);
        glColor3fv(colors[7]); glVertex3fv(vertices[7]);

        glColor3fv(colors[1]); glVertex3fv(vertices[1]);
        glColor3fv(colors[2]); glVertex3fv(vertices[2]);
        glColor3fv(colors[6]); glVertex3fv(vertices[6]);
        glColor3fv(colors[5]); glVertex3fv(vertices[5]);

        glColor3fv(colors[4]); glVertex3fv(vertices[4]);
        glColor3fv(colors[5]); glVertex3fv(vertices[5]);
        glColor3fv(colors[6]); glVertex3fv(vertices[6]);
        glColor3fv(colors[7]); glVertex3fv(vertices[7]);

        glColor3fv(colors[5]); glVertex3fv(vertices[5]);
        glColor3fv(colors[4]); glVertex3fv(vertices[4]);
        glColor3fv(colors[0]); glVertex3fv(vertices[0]);
        glColor3fv(colors[1]); glVertex3fv(vertices[1]);
        glEnd();
    glutSwapBuffers();
}


void mouseMotion(int x, int y)
{
    if(mouseLeftDown)
    {
        double viewport[4];
        glGetDoublev(GL_VIEWPORT,viewport);
        point[1][0] = x / (float)viewport[2] * 500;
        point[1][1] = (viewport[3] - y ) / (float)viewport[3]*500;
    }

    glutPostRedisplay();
}

void timer(int value)
{
    theta += 2.0;
    if(theta >= 360.0)
        theta -= 360.0;
    glutPostRedisplay();
    glutTimerFunc(1000/30,timer,1);
}

void init(void)
{

    viewer[0] = 2.5;
    viewer[1] = 2.5;
    viewer[2] = 2.5;
}


void keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {
    case 'q' | 'Q' :
        exit(0);
        break;
    case VK_ESCAPE :
        exit(0);
        break;
   
    case 'z':
    case 'Z':
        viewer[0] -= 0.1;
        break;
    case 'c':
    case 'C':
        viewer[0] += 0.1;
        break;
    case 's':
    case 'S':
        viewer[1] -= 0.1;
        break;
    case 'x':
    case 'X':
        viewer[1] += 0.1;
        break;
    case 'a':
    case 'A':
        viewer[2] -= 0.1;
        break;
    case 'd':
    case 'D':
        viewer[2] += 0.1;
        break;
    case 1:
        * pointer=2;//将这里改成exit(0);然后在程序运行时候点右键菜单,能关闭程序。

        break;
    case 2:
        * pointer=1;//将这里改成exit(0);然后在程序运行时候点右键菜单,能关闭程序。

        break;
    default:
        break;
    }
    glutPostRedisplay();
}



void SetupRC(void)
{
    glClearColor(1.0f,1.0f,1.0f,1.0f );
    glEnable(GL_DEPTH_TEST);
}

void ChangeSize(int w, int h)
{
    if(h==0)
        h=1;
    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (a=1)
    {
       if(w<=h)
   
            glOrtho(-2.0 , 2.0, -2.0*(float)h/(float)w, 2.0*(float)h/(float)w, 1.0, 10.0 );
        else
            glOrtho(-2.0*(float)w/(float)h, 2.0*(float)w/(float)h, -2.0, 2.0, 1.0, 10.0 );
    }
   
   
    else if (a=2)
    {
        if(w<=h)
            glFrustum(-2.0 , 2.0, -2.0*(float)h/(float)w, 2.0*(float)h/(float)w, 1.0, 10.0 );
        else
            glFrustum(-2.0*(float)w/(float)h, 2.0*(float)w/(float)h, -2.0, 2.0, 1.0, 10.0 );
    }
   
}

void menu(int item)
{
    keyboard((unsigned char)item,0,0);
}

void main(int argc, char* argv[])
{
    pointer = &a;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutCreateWindow("Simple");
    glutDisplayFunc(RenderScene);
   


    glutMotionFunc(mouseMotion);
    glutKeyboardFunc(keyboard);
   
    glutCreateMenu(menu);
    glutAddMenuEntry("Ortho",1);
    glutAddMenuEntry("Frustum",2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutReshapeFunc(ChangeSize);

    init();
    SetupRC();

    glutMainLoop();

}
搜索更多相关主题的帖子: OpenGL 
2009-10-16 17:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-288791-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.502070 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved