Skip to content

Commit 7346c24

Browse files
committed
Start tons of small opengl examples
1 parent 9214d7e commit 7346c24

36 files changed

+1619
-211
lines changed

Makefile_many

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ OUTS := $(addprefix $(OUT_DIR), $(OUTS_NODIR))
5353
all: mkdir $(OUTS)
5454
@#TODO ignore errors if not present
5555
@if [ -e "$(RUN_INPUT)" ] && [ ! -e "$(OUT_DIR)$(RUN_INPUT)" ]; then ln -s ../$(RUN_INPUT) "$(OUT_DIR)$(RUN_INPUT)"; fi
56-
@-$(MAKE) all-post
5756
ifneq ($(strip $(run)),)
5857
@echo
5958
cd $(OUT_DIR) && ./"$(run)"

Makefile_one

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ TMPS := $(addprefix $(TMP_DIR),$(TMPS_NODIR))
4747
OUT_BASENAME := $(OUT_BASENAME_NOEXT)$(OUT_EXT)
4848
OUT := $(OUT_DIR)$(OUT_BASENAME)
4949

50-
.PHONY: all all-post assembler clean debug set_debug_flags help mkdir profile set_profile_flags test
50+
.PHONY: all assembler clean debug set_debug_flags help mkdir profile set_profile_flags test
5151

5252
all: mkdir $(OUT)
5353
@#TODO ignore errors if not present
5454
@if [ -e "$(RUN_INPUT)" ] && [ ! -e "$(OUT_DIR)$(RUN_INPUT)" ]; then ln -s ../$(RUN_INPUT) "$(OUT_DIR)$(RUN_INPUT)"; fi
55-
@-$(MAKE) all-post
5655

5756
$(OUT): $(TMPS)
5857
$(MYCXX) $(PROFILE_FLAGS) $^ -o "$@" $(LIBS)

opengl/Makefile_params

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
LIBS := -lm -pthread -lGL -lGLU -lglut -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
1+
LIBS := -lm -pthread -lGL -lGLU -lglut -lglfw -lX11 -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

opengl/README.md

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
# OpenGL
22

33
1. [Introduction](introduction.md)
4-
1. [glinfo](glinfo.c)
4+
1. [GLUT](glut.md)
5+
1. [Lightning](lightning.md)
6+
1. Examples
7+
1. [GLUT hello world](hello_world.c)
8+
1. [glinfo](glinfo.c)
9+
1. [Triangle](triangl.c)
10+
1. [glOrtho](ortho.c)
11+
1. [glShadeModel](shade_model.c)
12+
1. [glViewport](viewport.c)
13+
1. [Depth](depth.c)
14+
1. Transformations
15+
1. [glTranslate](glTranslate.c)
16+
1. Lightning
17+
1. [Ambient](ambient.c)
18+
1. Diffuse
19+
1. [glNormal](normal.c)
20+
1. [Diffuse infinity](diffuse_infinity.c)
21+
1. [Diffuse shadow](diffuse_shadow.c)
22+
1. [Diffuse attenuation](diffuse_attenuation.c)
23+
1. [glDrawPixels (TODO)](draw_pixels.c)
24+
1. [Culling](culling.c)
25+
1. GLUT
26+
1. [glutBitmapCharacter](bitmap_character.c)
27+
1. [GLX](glx.c)
28+
1. Performance
29+
1. [Many triangles](many_triangles.c)
530
1. [main](main.cpp)
6-
1. bouncing_balls
7-
1. [one](bouncing_balls_one.cpp)
8-
1. [many](bouncing_balls_many.cpp)
9-
1. [Texture FPS](texture_fps.cpp)
31+
1. Mini projects
32+
1. bouncing_balls
33+
1. [one](bouncing_balls_one.cpp)
34+
1. [many](bouncing_balls_many.cpp)
35+
1. [Texture FPS](texture_fps.cpp)
1036
1. [Bibliography](bibliography.md)

opengl/TODO.md

+32
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,35 @@
22

33
- Are the OpenGL types fixed size? How to properly printf them?
44
- How to select the rendering device? <http://stackoverflow.com/questions/6036292/select-a-graphic-device-in-windows-opengl> `prime-select`?
5+
- Do everything without GLUT for Linux to learn what is going on. Windowing and events are likely not worth the trouble...
6+
- <http://stackoverflow.com/questions/879035/initializing-opengl-without-glut>
7+
- <http://stackoverflow.com/questions/5608767/learning-opengl-without-glut>
8+
- <https://www.opengl.org/discussion_boards/showthread.php/177999-GCC-OpenGL-without-glut>
9+
- Understand the matrix stack, `glLoadMatrixg`. Major application: define new points relative to the previous view. E.g.: wheels of a car relative to the car.
10+
- <http://gamedev.stackexchange.com/questions/72044/why-do-we-use-4x4-matrices-to-transform-things-in-3d>
11+
- <http://stackoverflow.com/questions/13647108/matrix-stacks-in-opengl-deprecated>
12+
- <http://stackoverflow.com/questions/3755998/what-is-the-point-of-the-matrix-stack-in-opengl>
13+
- <http://gamedev.stackexchange.com/questions/83725/new-to-opengl-having-trouble-understanding-matrix-transformation>
14+
- <https://www.reddit.com/r/learnprogramming/comments/1sipqz/why_does_opengl_use_4x4_matrices_to_describe_3d/>
15+
16+
1) Why use matrices at all? Answer: to pre-calculate multiple combined operations.
17+
2) Why 4D for 3D objects? Let's see 3D for 2D and then we're done.
18+
19+
TODO: world, view and projection transforms?
20+
- Make image spin with mouse drag:
21+
- http://stackoverflow.com/questions/19884182/moving-a-drawing-around-in-opengl-with-mouse
22+
- http://stackoverflow.com/questions/16342442/drag-and-drop-of-images-using-opengl-c
23+
- http://stackoverflow.com/questions/20288861/mouse-drag-object-in-opengl-glut
24+
- http://stackoverflow.com/questions/5734794/mouse-movement-opengl
25+
- http://stackoverflow.com/questions/1426415/opengl-moving-camera-with-mouse
26+
- http://gamedev.stackexchange.com/questions/53333/how-to-implement-a-basic-arcball-camera-in-opengl-with-glm
27+
- VAO, VBO http://stackoverflow.com/questions/11821336/what-are-vertex-array-objects
28+
- Immediate mode vs retained mode:
29+
- immediate mode is deprecated. So like, glBegin, glEnd and glVertex which we use in every test program are deprecated. Lol.
30+
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
31+
- http://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl Hello world requested but not provided, too long, lol. Opportunity!
32+
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
33+
- https://en.wikipedia.org/wiki/Stencil_buffer
34+
- shading
35+
- vertex shader
36+
- fragment shader

opengl/ambient.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
The triangle only becomes gray because ambient light is halved:
3+
http://forum.unity3d.com/threads/why-is-ambient-light-at-half-strength.33955/
4+
*/
5+
6+
#include <stdlib.h>
7+
8+
#include <GL/gl.h>
9+
#include <GL/glu.h>
10+
#include <GL/glut.h>
11+
12+
static void init(void) {
13+
GLfloat light0_ambient[] = {1.0, 1.0, 1.0, 1.0};
14+
GLfloat light0_position[] = {0.0, -1.0, 0.0, 0.0};
15+
glClearColor(0.0, 0.0, 0.0, 0.0);
16+
glShadeModel(GL_SMOOTH);
17+
glEnable(GL_LIGHTING);
18+
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
19+
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
20+
glColorMaterial(GL_FRONT, GL_AMBIENT);
21+
glEnable(GL_COLOR_MATERIAL);
22+
}
23+
24+
static void draw_triangle() {
25+
glColor3f(1.0, 0.0, 0.0);
26+
glVertex3f(0.0f, 1.0f, 0.0f);
27+
glVertex3f(-1.0f, -1.0f, 0.0f);
28+
glVertex3f(1.0f, -1.0f, 0.0f);
29+
}
30+
31+
static void display(void) {
32+
glClear(GL_COLOR_BUFFER_BIT);
33+
glLoadIdentity();
34+
glBegin(GL_TRIANGLES);
35+
draw_triangle();
36+
glEnd();
37+
glFlush();
38+
}
39+
40+
static void reshape(int w, int h) {
41+
glViewport(0, 0, w, h);
42+
glMatrixMode(GL_PROJECTION);
43+
glLoadIdentity();
44+
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0);
45+
glMatrixMode(GL_MODELVIEW);
46+
}
47+
48+
int main(int argc, char** argv) {
49+
glutInit(&argc, argv);
50+
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
51+
glutInitWindowSize(500, 500);
52+
glutInitWindowPosition(100, 100);
53+
glutCreateWindow(argv[0]);
54+
init();
55+
glutDisplayFunc(display);
56+
glutReshapeFunc(reshape);
57+
glutMainLoop();
58+
return EXIT_SUCCESS;
59+
}

opengl/bibliography.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
# Bibliography
22

3+
Standards:
4+
35
- <https://www.opengl.org/registry/#oldspecs> Khronos standards
46

57
From there you can reach for example:
68

79
- <https://www.opengl.org/registry/doc/glspec21.20061201.pdf> 2.1 spec
810
- <https://www.opengl.org/sdk/docs/man2/> 2.1 online reference
911

10-
- Demos:
12+
Tutorials with code:
13+
14+
- <https://github.com/JoeyDeVries/LearnOpenGL> <http://learnopengl.com/>
15+
- <https://github.com/opengl-tutorials/ogl>
16+
- <https://github.com/capnramses/antons_opengl_tutorials_book> Some hardcore examples. Uses GLFW.
17+
- <https://github.com/Overv/Open.GL/tree/master/content/code>
18+
- <http://www.glprogramming.com/red/>
19+
- <https://github.com/tomdalling/opengl-series> <http://www.tomdalling.com/blog/category/modern-opengl/> Modern, no immediate rendering.
20+
- <http://www.videotutorialsrock.com/opengl_tutorial/normalize/video.php>
21+
22+
Java:
23+
24+
- <https://github.com/OskarVeerhoek/YouTube-tutorials>
25+
26+
Demos:
1127

12-
- <https://github.com/prideout/recipes>
28+
- <https://github.com/prideout/recipes>

opengl/bitmap_character.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
# glutBitmapCharacter
3+
4+
http://stackoverflow.com/questions/8847899/opengl-how-to-draw-text-using-only-opengl-methods
5+
*/
6+
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
#include <GL/gl.h>
11+
#include <GL/glu.h>
12+
#include <GL/glut.h>
13+
14+
static void init() {
15+
glEnable(GL_DEPTH_TEST);
16+
glClearColor(0.0, 0.0, 0.0, 0.0);
17+
glShadeModel(GL_FLAT);
18+
}
19+
20+
static void drawString (void *font, char *s, GLfloat x, GLfloat y, GLfloat z){
21+
unsigned int i;
22+
/* Must come before glRasterPos. */
23+
glColor3f(0.0f, 0.0f, 1.0f);
24+
glRasterPos3f(x, y, z);
25+
for (i = 0; i < strlen(s); i++)
26+
glutBitmapCharacter(font, s[i]);
27+
}
28+
29+
static void display(void) {
30+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
31+
glLoadIdentity();
32+
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
33+
glBegin(GL_TRIANGLES);
34+
glColor3f(1.0f, 0.0f, 0.0f);
35+
glVertex3f( 0.0f, 1.0f, 0.0f);
36+
glVertex3f(-1.0f, -1.0f, 0.0f);
37+
glVertex3f( 1.0f, -1.0f, 0.0f);
38+
glEnd();
39+
40+
/* This is drawn as close as possible to the camera. */
41+
drawString(GLUT_BITMAP_HELVETICA_18, "bottom", -1.0, -1.0, 3.5);
42+
43+
/* Depth is considered. Things that come behind are hidden. */
44+
drawString(GLUT_BITMAP_HELVETICA_18, "behind", 0.0, 0.0, -1.0);
45+
drawString(GLUT_BITMAP_HELVETICA_18, "front", 0.0, 0.5, 1.0);
46+
47+
/* The font size is fixed no matter how far back we are.*/
48+
drawString(GLUT_BITMAP_HELVETICA_18, "far", 200.0, 200.0, -990.0);
49+
50+
glFlush();
51+
}
52+
53+
static void reshape(int w, int h) {
54+
glViewport(0, 0, w, h);
55+
glMatrixMode(GL_PROJECTION);
56+
glLoadIdentity();
57+
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 1000.0);
58+
glMatrixMode(GL_MODELVIEW);
59+
}
60+
61+
int main(int argc, char** argv) {
62+
glutInit(&argc, argv);
63+
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
64+
glutInitWindowSize(500, 500);
65+
glutInitWindowPosition(100, 100);
66+
glutCreateWindow(argv[0]);
67+
init();
68+
glutDisplayFunc(display);
69+
glutReshapeFunc(reshape);
70+
glutMainLoop();
71+
return EXIT_SUCCESS;
72+
}

opengl/bouncing_balls_many.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
Many spheres bouncing inside a cube with gravity,
33
energy dissipation on impact, and impact between the spheres.
44
5-
TODO: DRY up with bouncing_balls_one.cpp
5+
TODO: DRY
6+
7+
- up with bouncing_balls_one.cpp
8+
- balls flicker a lot. Why?
69
*/
710

811
#include <cstdio>
@@ -171,9 +174,6 @@ void calc_new_scene(void){
171174
}
172175

173176
void draw_cube(){
174-
175-
glPushMatrix();
176-
177177
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
178178
glColor3fv(DARK_GRAY);
179179

@@ -214,8 +214,6 @@ void draw_cube(){
214214
glLineWidth(2.0);
215215
glutWireCube(2.0);
216216

217-
glPopMatrix();
218-
219217
}
220218

221219
void draw_spheres() {

opengl/bouncing_balls_one.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void calc_new_scene(void) {
6363
old_t = t;
6464

6565
// Calculate new scene based on dt (ms).
66-
for (int i = 0; i<total_spheres; i++){
66+
for (int i = 0; i < total_spheres; ++i){
6767

6868
//cout << "\n---------------------------\n";
6969
//cout << "center\n" << spheres[i].center.str() << "\n";
@@ -83,14 +83,10 @@ void calc_new_scene(void) {
8383
spheres[i].speed = spheres[i].speed + GRAVITY*dt;
8484
}
8585
}
86-
8786
glutPostRedisplay();
8887
}
8988

9089
void draw_cube(){
91-
92-
glPushMatrix();
93-
9490
glColor3fv(DARK_GRAY);
9591

9692
// back
@@ -129,33 +125,25 @@ void draw_cube(){
129125
glColor3fv(WHITE);
130126
glLineWidth(2.0);
131127
glutWireCube(2.0);
132-
133-
glPopMatrix();
134-
135128
}
136129

137130
void draw_spheres() {
138131
for (int i=0; i<total_spheres; i++){
139132
spheres[i].draw();
140133
}
141-
142134
}
143135

144136
void draw_scene(void) {
145-
146137
glLoadIdentity();
147138
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);
148-
149139
draw_cube();
150140
draw_spheres();
151-
152141
}
153142

154143
void display(void) {
155144
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156145
draw_scene();
157146
glutSwapBuffers();
158-
159147
}
160148

161149
void reshape(int w, int h) {
@@ -180,7 +168,7 @@ int main(int argc, char** argv) {
180168
glutInitWindowSize(500, 500);
181169
glutInitWindowPosition(100, 100);
182170
glutCreateWindow(argv[0]);
183-
glClearColor(clear_color_r,clear_color_g,clear_color_b,1.0);
171+
glClearColor(clear_color_r, clear_color_g, clear_color_b, 1.0);
184172
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
185173
glEnable(GL_POLYGON_OFFSET_FILL);
186174
glEnable(GL_POLYGON_OFFSET_LINE);

opengl/configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22
sudo aptitude update
3-
sudo aptitude install -y freeglut3-dev libopencv-dev
3+
sudo aptitude install -y freeglut3-dev libglfw3-dev libopencv-dev

0 commit comments

Comments
 (0)