-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbuffer.cpp
158 lines (135 loc) · 3.19 KB
/
zbuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
using namespace std;
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0f,0.0f,0.0f);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
GLfloat xRot = 0.0;
GLfloat yRot = 0.0;
void DrawCube()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glColor3f(0.0f, 1.0f, 0.0f);
glShadeModel(GL_FLAT);
glFrontFace(GL_CW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glColor3f(0.0,1.0,0.0);
glVertex3f( 50.0f, 50.0f,50.0f);
glVertex3f(-50.0f, 50.0f,50.0f);
glVertex3f(-50.0f,-50.0f,50.0f);
glVertex3f( 50.0f,-50.0f,50.0f);
glColor3f(0.0,0.0,1.0);
glVertex3f(50.0, 50.0,-50.0);
glVertex3f(50.0, 50.0, 50.0);
glVertex3f(50.0,-50.0, 50.0);
glVertex3f(50.0,-50.0,-50.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 50.0,-50.0,-50.0);
glVertex3f(-50.0,-50.0,-50.0);
glVertex3f(-50.0, 50.0,-50.0);
glVertex3f( 50.0, 50.0,-50.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-50.0, 50.0, 50.0);
glVertex3f(-50.0, 50.0,-50.0);
glVertex3f(-50.0,-50.0,-50.0);
glVertex3f(-50.0,-50.0, 50.0);
glColor3f(1.0,1.0,0.0);
glVertex3f( 50.0,50.0,-50.0);
glVertex3f(-50.0,50.0,-50.0);
glVertex3f(-50.0,50.0, 50.0);
glVertex3f( 50.0,50.0, 50.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 50.0,-50.0, 50.0);
glVertex3f(-50.0,-50.0, 50.0);
glVertex3f(-50.0,-50.0,-50.0);
glVertex3f( 50.0,-50.0,-50.0);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
else if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
else if(key == GLUT_KEY_RIGHT)
yRot -= 5.0f;
else if(key == GLUT_KEY_LEFT)
yRot += 5.0f;
else if(key > 356.0f)
xRot = 0.0f;
else if(key < -1.0f)
xRot = 355.0f;
else if(key > 356.0f)
yRot = 0.0f;
else if(key < -1.0f)
yRot = 355.0f;
else
exit(0);
glutPostRedisplay();
}
void KeyboardAction(unsigned char key, int x,int y)
{
exit(0);
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
if(h == 0)
{
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
{
glOrtho(-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
}
else
{
glOrtho(-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Z-Buffering");
glViewport(0, 0, 500, 500);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(KeyboardAction);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(DrawCube);
init();
glutMainLoop();
return 0;
}