gpt4 book ai didi

c++ - 当我在 OpenGL 中按下一个键时无法绘制形状

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:14 25 4
gpt4 key购买 nike

我在绘制一些形状时遇到了问题。当我执行程序时,我应该只看到坐标,然后按下一个键来绘制形状,但默认情况下它已经绘制了立方体,然后是金字塔。按'p'键时如何绘制金字塔,按'c'键时如何绘制立方体?我尝试了一些东西,但它似乎不起作用 :(

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

float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 },
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];

GLfloat d[3] = { 0.1, 0.1, 0.1 };

GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;

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

void Special_Keys(int key, int x, int y)
{
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);
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)
{
glBegin(GL_QUADS);

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 Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
switch (key) {

case 'd': d[0] += 0.1; break;
case 'a': d[0] -= 0.1; break;
case 'w': d[1] += 0.1; break;
case 's': d[1] -= 0.1; break;
case 'm': d[2] += 0.1; break;
case 'n': d[2] -= 0.1; break;
case 'p': Pyramid(); break;
case 'c': Draw_Box(); break;

case 'x': xAngle += 5; break;
case 'y': yAngle += 5; break;
case 'z': zAngle += 5; break;

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

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);
//Draw_Box(); // if i delete comment the cube it's drawn
//Pyramid(); // if i delete this the pyramid it's drawn inside the cube
glPopMatrix();

glutSwapBuffers();
}

//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4

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);
gluPerspective(60, 1.5, 1, 10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();

return 1;
}

最佳答案

有几种方法可以解决这个问题。一种是创建一个全局变量,它会告诉您是否要绘制形状:

bool draw_pyramid = false;
bool draw_box = false;

然后,在你的键盘句柄函数中,这样做:

case 'p': draw_pyramid = true;  break;
case 'c': draw_box = true; break;

最后,在您的重绘函数中,进行有条件的绘制:

if ( draw_pyramid )
Pyramid();

if ( draw_box )
Draw_Box();

关于c++ - 当我在 OpenGL 中按下一个键时无法绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58268286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com