尝试将图纸分隔到不同的视口

时间:2013-12-03 16:57:12

标签: c++ opengl

基本上,输出应该如下所示:

enter image description here

这就是我现在所拥有的:

enter image description here

似乎我的所有绘图都在右上方的视口中,稍微倾斜一点(不直接观察到侧面)。

我正在尝试将每个立方体分开,并将它们放在各自的视口中,如第一张图片所示。必须有言语,但我现在并不担心这些。

有谁能告诉/告诉我如何将它们分开?

代码:

#include <Windows.h>
#include <GL\glut.h>

#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

bool init(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST); 
    glDepthFunc(GL_LEQUAL);
    glShadeModel(GL_SMOOTH);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    return true;
}

void drawline(float from_x, float from_y, float to_x, float to_y)
{
    // From coordinate position
    glVertex2f(from_x, from_y);

    // To coordinate position
    glVertex2f(to_x, to_y);
}

void diode(void)
{
    // Draw line

        // TOP
    glBegin(GL_POLYGON); 
        glColor3f(0.0f, 1.0f, 0.0f);     // Green
        glVertex3f(-1, 1, -1);
        glVertex3f(-1, 1, 1);
        glVertex3f(0, 1, 1);
        glVertex3f(1, 1, 0);
        glVertex3f(1, 1, -1);
    glEnd();

        // BOTTOM
    glBegin(GL_POLYGON); 
        glColor3f(1.0f, 0.5f, 0.0f);     // Orange
        glVertex3f(-1, -1, 1);
        glVertex3f(-1, -1, -1);
        glVertex3f(1, -1, -1);
        glVertex3f(1, -1, 1);
    glEnd();

        // RIGHT
    glBegin(GL_POLYGON); 
        glColor3f(1.0f, 0.0f, 0.0f);     // Red
        glVertex3f(1, -1, -1);
        glVertex3f(1, 1, -1);
        glVertex3f(1, 1, 0);
        glVertex3f(1, 0, 1);
        glVertex3f(1, -1, 1);
    glEnd();

        // LEFT
    glBegin(GL_POLYGON); 
        glColor3f(0.5f, 1.0f, 1.0f);     // Turquoise
        glVertex3f(-1, -1, 1);
        glVertex3f(-1, 1, 1);
        glVertex3f(-1, 1, -1);
        glVertex3f(-1, -1, -1);
    glEnd();

        // FRONT
    glBegin(GL_POLYGON); 
        glColor3f(0.0f, 0.0f, 1.0f);     // Blue
        glVertex3f(1, -1, 1);
        glVertex3f(1, 0, 1);
        glVertex3f(0, 1, 1);
        glVertex3f(-1, 1, 1);
        glVertex3f(-1, -1, 1);
    glEnd();

        // BACK
    glBegin(GL_POLYGON); 
        glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
        glVertex3f(-1, -1, -1);
        glVertex3f(-1, 1, -1);
        glVertex3f(1, 1, -1);
        glVertex3f(1, -1, -1);
    glEnd();

        // CORNER
    glBegin(GL_POLYGON); 
        glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
        glVertex3f(1, 0, 1);
        glVertex3f(1, 1, 0);
        glVertex3f(0, 1, 1);
    glEnd();
}

//void drawString(char *str)
//{
//  for (int i = str; i < sizeof(str); i++)
//  {
//      glRasterPos2i(2.0f, 2.0f);
//      glColor3f(1.0f, 1.0f, 1.0f);
//      glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str);
//  }
//}

void PilotView (float xtran, float ytran, float ztran,
      float xrot, float yrot, float zrot, float scale)
{
    glTranslatef (xtran, ytran, ztran);
    glRotatef (xrot, 1.0f, 0.0f, 0.0f);
    glRotatef (yrot, 0.0f, 1.0f, 0.0f);
    glRotatef (zrot, 0.0f, 0.0f, 1.0f);
    glScalef (scale, scale, scale);
}

void render(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    // Top view - top left
    glLoadIdentity();
    PilotView(-3.0f, 2.0f, -10.0f, 90.0f, 0.0f, 0.0f, 1.0f);
    diode();

    // Corner view - top right
    glLoadIdentity();
    PilotView(3.0f, 2.0f, -10.0f, 45.0f, -45.0f, 0.0f, 1.0f);
    diode();

    // Front view - bottom left
    glLoadIdentity();
    PilotView(-3.0f, -2.0f, -10.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    diode();

    // Right view - bottom right
    glLoadIdentity();
    PilotView(3.0f, -2.0f, -10.0f, 0.0f, -90.0f, 0.0f, 1.0f);
    diode();

    glutSwapBuffers();
}


void reshape(GLsizei width, GLsizei height)
{
    if (height == 0) height = 1;
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glViewport(0, height/2, width/2, height/2);
    glMatrixMode(GL_PROJECTION);   
    glLoadIdentity();
    PilotView(0.0f, 0.0f, 0.0f, 0.0f, -90.0f, 0.0f, 1.0f);
    diode();

    glViewport(width/2, 0, width/2, height/2);
    glMatrixMode(GL_PROJECTION);   
    glLoadIdentity();

    glViewport(0, height/2, width/2, height/2);
    glMatrixMode(GL_PROJECTION);   
    glLoadIdentity();

    glViewport(width/2, height/2, width/2, height/2);
    glMatrixMode(GL_PROJECTION);   
    glLoadIdentity();

    gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My CIS451 OpenGL program 3");

    glutDisplayFunc(render);
    glutReshapeFunc(reshape);

    init();

    glutMainLoop();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

你为#1新手错误而堕落:在重塑处理程序中执行视口和投影设置。 OpenGL是一个状态机,因此只有最后一个设置投影和视口才有效果。另一个国家的变化只是消失了。

具有重塑功能的教程是IMHO OpenGL新手十大麻烦制造商之一。

如何正确完成?在绘制处理程序中绘制相应部分之前,可以设置视口和投影。

想一想:OpenGL如何将重塑中发生的事情与渲染中发生的事情联系起来?

你想要的是摆脱重塑功能(对你没有用)并将渲染更改为:

void render(void)
{
    int const width  = glutGet(GLUT_WINDOW_WIDTH);
    int const height = glutGet(GLUT_WINDOW_HEIGHT);
    if (height == 0) height = 1;
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, aspect, 0.1f, 100.0f);

    // Top view - top left
    glViewport(0, height/2, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(-3.0f, 2.0f, -10.0f, 90.0f, 0.0f, 0.0f, 1.0f);
    diode();

    // Corner view - top right
    glViewport(width/2, 0, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(3.0f, 2.0f, -10.0f, 45.0f, -45.0f, 0.0f, 1.0f);
    diode();

    // Front view - bottom left
    glViewport(0, height/2, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(-3.0f, -2.0f, -10.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    diode();

    // Right view - bottom right
    glViewport(width/2, height/2, width/2, height/2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    PilotView(3.0f, -2.0f, -10.0f, 0.0f, -90.0f, 0.0f, 1.0f);
    diode();

    glutSwapBuffers();
}