我正在尝试开放并提出一些问题: 我实际上有这个代码:
#include <freeglut.h>
#include <glut.h>
#include <stdio.h>
using namespace std;
char presse;
int anglex=-45, angley=45, x, y, xold, yold;
/* Prototype des fonctions (function prototypes) */
void affichage();
void clavier(unsigned char touche, int x, int y);
void reshape(int x, int y);
void idle();
void mouse(int bouton, int etat, int x, int y);
void mousemotion(int x, int y);
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 1, 2, 1, 1 };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
int main(int argc, char **argv)
{
/* initialisation de glut et creation
de la fenetre (glut initialization and window creation) */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(200, 200);
glutInitWindowSize(1000, 1000);
glutCreateWindow("cube");
/* Initialisation d'OpenGL (OpenGL initialisation) */
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
glEnable(GL_DEPTH_TEST); // Active le test de profondeur
(activate depth testing)
glEnable(GL_LIGHTING); // Active l'éclairage
(activate lighting)
glEnable(GL_LIGHT0); // Allume la lumière n°1
(turn on light no. 1)
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
/* enregistrement des fonctions de rappel (register callbacks) */
glutDisplayFunc(affichage);
glutKeyboardFunc(clavier);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(mousemotion);
/* Entree dans la boucle principale glut (entry of the main glut loop) */
glutMainLoop();
return 0;
}
double a = 0;
int LightPos[4] = { -1, 4, -1, 1 };
int MatSpec[4] = { 1, 1, 1, 1 };
void affichage()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, 1000, 1000);
a = +1;
glLoadIdentity();
glRotatef(angley, 1.0, 0.0, 0.0);
glRotatef(anglex, 0.0, 1.0, 0.0);
for (int k = 0; k < 5; k++){
if (k == 0){
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glEnd();
}
else {
if (k > 0){
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0);
glVertex3f(0.5, 0, 0);
glVertex3f(0.5, 0, 0.5);
glEnd();
}
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0);
glVertex3f(k, 0, 0);
glVertex3f(k, 0, 0.5);
glEnd();
}
}
for (int k = 0; k < 5; k++){
if (k == 0){
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(0, 5, 0);
glEnd();
}
else {
if (k >0){
GLfloat n = k / 2;
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0);
glVertex3f(0, 0.5, 0);
glVertex3f(0.5, 0.5, 0);
glEnd();
}
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0);
glVertex3f(0, k, 0);
glVertex3f(0.5, k, 0);
glEnd();
}
}
for (int k = 0; k < 5; k++){
if (k == 0){
glBegin(GL_LINES);
glColor3f(1.0, 0.6, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glEnd();
}
else {
if (k >0){
GLfloat n = k / 2;
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0, 0, 0.5);
glVertex3f(0, 0.5, 0.5);
glEnd();
}
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 10);
glVertex3f(0, 0, k);
glVertex3f(0, 0.5, k);
glEnd();
}
}
// Par terre scene
(ground scene)
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex3d(0,-0.1,0);
glVertex3d(5, -0.1, 0);
glVertex3d(5, -0.1, 5);
glVertex3d(0, -0.1, 5);
glEnd();
glFlush();
glutSwapBuffers();
}
void clavier(unsigned char touche, int x, int y)
{
switch (touche)
{
case 'o': /* eteindre la lumiere (turn off the light) */
glDisable(GL_LIGHT0);
glutPostRedisplay();
//std::cout << "lumiere eteinte";
break;
case 'a': /* eteindre la lumiere (turn off the light) */
glEnable(GL_LIGHT0);
glutPostRedisplay();
break;
case 'p': /* affichage du carre plein (normal fill mode, interior of the polygon is filled) */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case 'f': /* affichage en mode fil de fer (draw polygon edges as lines) */
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glutPostRedisplay();
break;
case 's': /* Affichage en mode sommets seuls (draw polygon vertices as points) */
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
glutPostRedisplay();
break;
case 'd':
glEnable(GL_DEPTH_TEST);
glutPostRedisplay();
break;
case 'D':
glDisable(GL_DEPTH_TEST);
glutPostRedisplay();
break;
case 'q': /*la touche 'q' permet de quitter le programme (the key 'q' exits the program) */
exit(0);
}
}
void reshape(int x, int y)
{
if (x<y)
glViewport(0, (y - x) / 2, x, x);
else
glViewport((x - y) / 2, 0, y, y);
}
void mouse(int button, int state, int x, int y)
{
/* si on appuie sur le bouton gauche (if you press the left button) */
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
presse = 1; /* le booleen presse passe a 1 (vrai) (the boolean presse is set to 1 (true)) */
xold = x; /* on sauvegarde la position de la souris (save the position of the mouse) */
yold = y;
}
/* si on relache le bouton gauche (if you release the left button) */
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
presse = 0; /* le booleen presse passe a 0 (faux) (the boolean presse is set to 0 (false)) */
}
void mousemotion(int x, int y)
{
if (presse) /* si le bouton gauche est presse (if the left button is pressed) */
{
/* on modifie les angles de rotation de l'objet
en fonction de la position actuelle de la souris et de la derniere
position sauvegardee (modify the rotation angles of the object
depending on the current position of the mouse and the last
saved position) */
anglex = anglex + (x - xold);
angley = angley + (y - yold);
glutPostRedisplay();
}
xold = x;
yold = y;
}
但我无法管理场景,只需将x,y和z轴绘制到它的完整范围内。渲染的对象是&#34; cut&#34;在屏幕上。我怎样才能先完全绘制它们然后向后移动相机?我很高兴已经把镜头移动了一些等等......但是它仍然很复杂...
欢迎任何建议。
答案 0 :(得分:1)
您没有设置任何特定的投影矩阵,因此您将获得单位矩阵的默认值。这意味着查看体积沿所有三个轴的范围从-1到1。因此,如果物体到达该体积之外,物体就会被切断。