当我在OpenGL中按键时无法绘制六边形棱镜

时间:2019-10-10 08:44:59

标签: c++ opengl opengl-compat

当我按下一个键时,我试图绘制一个六边形棱镜,但是我有一个大问题:默认情况下,它在屏幕上绘制为随机形状。这是我的代码。当我按p时,它显示为一个金字塔,当我按c时,我可以看到一个立方体,但是默认情况下它是随机形状的,我不明白为什么...这是我的代码和示例照片:

problem

#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h> //incluziuni librarii
#define  RADDEG  57.29577951f //constanta 

float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 }, //vector coordonate 
YUP[3] = { 0,1,0 }, YUN[3] = { 0,-1, 0 },
ZUP[3] = { 0,0,1 }, ZUN[3] = { 0, 0,-1 },
ORG[3] = { 0,0,0 };

GLfloat viewangle = 0, tippangle = 0, traj[120][3]; //variabila pentru unghi camera

GLfloat d[3] = { 0.1, 0.1, 0.1 }; //vector directie

GLfloat  xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
bool draw_pyramid = false; //variabila desenat figuri 
bool draw_box = false;
bool draw_prism = false;

//  Use arrow keys to rotate entire scene !!!

void Special_Keys(int key, int x, int y) //functie ptr taste sus jos stanga dreapta
{
    switch (key) {

    case GLUT_KEY_LEFT:  viewangle -= 5;  break;
    case GLUT_KEY_RIGHT:  viewangle += 5;  break;
    case GLUT_KEY_UP:  tippangle -= 5;  break;
    case GLUT_KEY_DOWN:  tippangle += 5;  break;

    default: printf("Special key %c == %d", key, key);
    }

    glutPostRedisplay();
}
void Pyramid(void) //draw the pyramid shape
{
    glBegin(GL_TRIANGLE_FAN);//triangles have a common vertex, which is the central vertex
    glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);   //V0(red)
    glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);   //V1(green)
    glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f);   //V2(blue)
    glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);   //V3(green)
    glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);   //V4(blue)
    glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);   //V1(green)
    glEnd();
}

void Draw_Box(void) //functie desenat cub
{
    glBegin(GL_QUADS);//// Draw A Quad

    glColor3f(0.0, 0.7, 0.1);     // Front - green
    glVertex3f(-1.0, 1.0, 1.0);
    glVertex3f(1.0, 1.0, 1.0);
    glVertex3f(1.0, -1.0, 1.0);
    glVertex3f(-1.0, -1.0, 1.0);

    glColor3f(0.9, 1.0, 0.0);    // Back  - yellow
    glVertex3f(-1.0, 1.0, -1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(1.0, -1.0, -1.0);
    glVertex3f(-1.0, -1.0, -1.0);

    glColor3f(0.2, 0.2, 1.0);     // Top - blue 
    glVertex3f(-1.0, 1.0, 1.0);
    glVertex3f(1.0, 1.0, 1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(-1.0, 1.0, -1.0);

    glColor3f(0.7, 0.0, 0.1);    // Bottom - red
    glVertex3f(-1.0, -1.0, 1.0);
    glVertex3f(1.0, -1.0, 1.0);
    glVertex3f(1.0, -1.0, -1.0);
    glVertex3f(-1.0, -1.0, -1.0);

    glEnd();
}

void hexagonalPrism()
{
    glBegin(GL_QUADS);
    glVertex3f(0.5, 0, 0.5);
    glVertex3f(0.5, 0, -0.5);
    glVertex3f(-0.5, 0, -0.5);
    glVertex3f(-0.5, 0, 0.5);

    glVertex3f(0.5, 0, -0.5);
    glVertex3f(0.5, 1, -0.5);
    glVertex3f(-0.5, 1, -0.5);
    glVertex3f(-0.5, 0, -0.5);

    glVertex3f(0.5, 1, -0.5);
    glVertex3f(-0.5, 1, -0.5);
    glVertex3f(-0.5, 0, 0.5);
    glVertex3f(0.5, 0, 0.5);
    glEnd();
    glBegin(GL_TRIANGLES);
    glVertex3f(0.5, 0, 0.5);
    glVertex3f(0.5, 1, -0.5);
    glVertex3f(0.5, 0, -0.5);

    glVertex3f(-0.5, 0, 0.5);
    glVertex3f(-0.5, 1, -0.5);
    glVertex3f(-0.5, 0, -0.5);
    glEnd();
}

void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
    switch (key) {

    case 'd': d[0] += 0.1;  break; //camera right
    case 'a': d[0] -= 0.1;  break; //camera left
    case 'w': d[1] += 0.1;  break; //camera up 
    case 's': d[1] -= 0.1;  break; //camera down 
    case 'm': d[2] += 0.1;  break; //magnify
    case 'n': d[2] -= 0.1;  break; //minify
    case 'p': draw_pyramid = true;  draw_box = false; break; //draw pyramid when key is pressed
    case 'c': draw_box = true;  draw_pyramid = false; break; //draw cube when key is pressed
    case 't': draw_box = false;  draw_pyramid = false; draw_prism = true; break; //draw prism when key is pressed

    case 'x': xAngle += 5;  break; //modify x axis angle
    case 'y': yAngle += 5;  break; //modify y axis angle
    case 'z': zAngle += 5;  break;  //modify z axis angle

    default: printf("   Keyboard %c == %d", key, key); //see what key it's pressed
    }

    glutPostRedisplay();
}

void Triad(void)
{
    glColor3f(1.0, 1.0, 1.0); //set the dark grey color 

    glBegin(GL_LINES);
    glVertex3fv(ORG); glVertex3fv(XUP);
    glVertex3fv(ORG); glVertex3fv(YUP);
    glVertex3fv(ORG); glVertex3fv(ZUP);
    glEnd();

    glRasterPos3f(1.1, 0.0, 0.0);
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'X'); //draw the x axis

    glRasterPos3f(0.0, 1.1, 0.0);
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Y'); //draw the y axis

    glRasterPos3f(0.0, 0.0, 1.1);
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Z'); //draw the z axis 
}

void redraw(void)
{
    int v;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    glLoadIdentity();

    glTranslatef(0, 0, -3);
    glRotatef(tippangle, 1, 0, 0);  // Up and down arrow keys 'tip' view.
    glRotatef(viewangle, 0, 1, 0);  // Right/left arrow keys 'turn' view.

    glDisable(GL_LIGHTING);

    Triad();

    glPushMatrix();
    glTranslatef(d[0], d[1], d[2]);    // Move box down X axis.
    glScalef(0.2, 0.2, 0.2);
    glRotatef(zAngle, 0, 0, 1);
    glRotatef(yAngle, 0, 1, 0);
    glRotatef(xAngle, 1, 0, 0);
    if ( draw_pyramid )
    Pyramid();

    if (hexagonalPrism)
        hexagonalPrism();

    if ( draw_box )
        Draw_Box();
    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(900, 600);
    glutInitWindowPosition(300, 300);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);

    glutCreateWindow("Big HW1");
    glutDisplayFunc(redraw);
    glutKeyboardFunc(Keyboard);
    glutSpecialFunc(Special_Keys);

    glClearColor(0.1, 0.0, 0.1, 1.0);

    glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix, matrix that represents your camera's lens (aperture, far-field, near-field, etc).
    gluPerspective(60, 1.5, 1, 10); //set up a perspective projection matrix
    glMatrixMode(GL_MODELVIEW); //specify which matrix is the current matrix,matrix that represents your camera (position, pointing, and up vector).
    glutMainLoop();

    return 1;
}

2 个答案:

答案 0 :(得分:1)

  

[...]现在看起来像是三角棱镜

当然,六边形有6个边,但是在功能hexagonalPrism中,您只为边画3个四边形,为顶部和底部画2个三角形。

Hexagon的角点定义6个点:

x:  0.866, 0.0, -0.866, -0.866,  0.0,  0.866
y:  0.5,   1.0,  0.5,   -0.5,   -1.0, -0.5

使用该点为侧面绘制6个四边形,为顶部和底部绘制一个a多边形。例如:

void hexagonalPrism()
{
    float x[] = { 0.866f, 0.0f, -0.866f, -0.866f,  0.0f,  0.866f };
    float y[] = { 0.5f,   1.0f,  0.5f,   -0.5f,   -1.0f, -0.5f };

    glBegin(GL_QUADS);
    for (int i1 = 0; i1 < 6; ++i1)
    {
        glColor4f(
          i1 < 2 || i1 > 4 ? 1.0f : 0.0f,
          i1 > 0 && i1 < 5 ? 1.0f : 0.0f,
          i1 > 2 ? 1.0f : 0.0f,
          1.0f
        );

        int i2 = (i1 + 1) % 6;
        glVertex3f(x[i1], 0.0f, y[i1]);
        glVertex3f(x[i2], 0.0f, y[i2]);
        glVertex3f(x[i2], 1.0f, y[i2]);
        glVertex3f(x[i1], 1.0f, y[i1]);
    }
    glEnd();

    glColor4f( 1, 1, 1, 1 );

    glBegin(GL_POLYGON);
    for (int i = 0; i < 6; ++i)
        glVertex3f(x[i], 0.0f, y[i]);
    glEnd();
    glBegin(GL_POLYGON);
    for (int i = 0; i < 6; ++i)
        glVertex3f(x[i], 1.0f, y[i]);
    glEnd();
}

答案 1 :(得分:0)

也许问题出在这里

edit