-
Notifications
You must be signed in to change notification settings - Fork 18
/
example_beginend_freeglut_1.cpp
138 lines (101 loc) · 2.79 KB
/
example_beginend_freeglut_1.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
// STL
#include <iostream>
#include <string>
#include <stdexcept>
#include <chrono>
// GLM [https://glm.g-truc.net/0.9.9/api/index.html]
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// OpenGL
#include <gl/gl_glew.h>
// FreeGLUT [http://freeglut.sourceforge.net/]
#include <gl/freeglut.h>
// STB
#define STB_IMAGE_IMPLEMENTATION
//#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image.h>
//#include <stb_image_write.h>
// project includes
#include <gl/gl_debug.h>
// preprocessor definitions
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// https://replit.com/@Rabbid76/freeglut-opengl-1#main.cpp
// function prototypes
void display( void );
void cube( void );
// main
int main(int argc, char** argv)
{
std::cout << "start" << std::endl;
glutInit(&argc, argv);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800, 600);
int wnd = glutCreateWindow("FreeGLUT OpenGL window");
if ( glewInit() != GLEW_OK )
throw std::runtime_error( "error initializing glew" );
OpenGL::CContext::TDebugLevel debug_level = OpenGL::CContext::TDebugLevel::all;
OpenGL::CContext context;
context.Init( debug_level );
glutDisplayFunc(display);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(90.0, 800.0/600.0, 0.1, 100.0);
glViewport( 0, 0, 800, 600 );
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable( GL_DEPTH_TEST );
glutMainLoop();
std::cout << "terminate" << std::endl;
glutDestroyWindow(wnd);
return 0;
}
float angle = 0.0f;
void display( void )
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(0, 5.0f, 0, 0, 0, 0, 0, 0, 1);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(-2.0f, 0, 0);
glRotatef(angle, 1, 1, 0);
cube();
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f, 0, 0);
glRotatef(angle*2.0f, 1, 0, 1);
cube();
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
angle += 1.0f;
}
void cube( void )
{
static float verticies[] = {
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, -1,
1, -1, 1,
1, 1, 1,
-1, -1, 1,
-1, 1, 1,
};
static int surfaces[] = {0,1,2,3, 3,2,7,6, 6,7,5,4, 4,5,1,0, 1,5,7,2, 4,0,3,6 };
static float colors[] = { 1,0,0, 0,1,0, 0,0,1, 1,1,0, 1,0,1, 1,0.5,0 };
glBegin(GL_QUADS);
for (int side = 0; side < 6; ++side)
{
glColor3fv(colors + side*3);
for (int corner = 0; corner < 4; corner++)
{
int vi = surfaces[side*4+corner];
glVertex3fv(verticies + vi*3);
}
}
glEnd();
}