Evan
Evan

Reputation: 2635

Trying to separate drawings onto different viewports

Basically, the output is supposed to look like this:

enter image description here

This is what I currently have right now:

enter image description here

It seems like all my drawings are in the top-right viewport and somewhat tilted a little bit (not viewing directly to the side).

I'm trying to separate each cube and put them in their respective viewports as shown in the first picture. There has to be words, but I'm not worried about those right now.

Can anyone tell/show me how I can separate them?

Code:

#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;
}

Upvotes: 0

Views: 84

Answers (1)

datenwolf
datenwolf

Reputation: 162367

You fell for the #1 newbie mistake: Performing Viewport and Projection setup in the reshape handler. OpenGL is a state machine, so only the very last of the set projection and viewport has an effect. The other state changes simply vanish.

Tutorials featureing a reshape function are IMHO one of the Top 10 trouble makers for OpenGL newbies.

How to do it correctly? You set viewport and projection right before you draw the corresponding portion in the drawing handler.

Think about is: How is OpenGL supposed to associate what happens in reshape with what happens in render?

What you want is get rid of the reshape function (it's of no use for you) and change render to this:

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();
}

Upvotes: 1

Related Questions