Skip to content

Commit

Permalink
Start tons of small opengl examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Mar 18, 2016
1 parent 9214d7e commit 7346c24
Show file tree
Hide file tree
Showing 36 changed files with 1,619 additions and 211 deletions.
1 change: 0 additions & 1 deletion Makefile_many
Expand Up @@ -53,7 +53,6 @@ OUTS := $(addprefix $(OUT_DIR), $(OUTS_NODIR))
all: mkdir $(OUTS)
@#TODO ignore errors if not present
@if [ -e "$(RUN_INPUT)" ] && [ ! -e "$(OUT_DIR)$(RUN_INPUT)" ]; then ln -s ../$(RUN_INPUT) "$(OUT_DIR)$(RUN_INPUT)"; fi
@-$(MAKE) all-post
ifneq ($(strip $(run)),)
@echo
cd $(OUT_DIR) && ./"$(run)"
Expand Down
3 changes: 1 addition & 2 deletions Makefile_one
Expand Up @@ -47,12 +47,11 @@ TMPS := $(addprefix $(TMP_DIR),$(TMPS_NODIR))
OUT_BASENAME := $(OUT_BASENAME_NOEXT)$(OUT_EXT)
OUT := $(OUT_DIR)$(OUT_BASENAME)

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

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

$(OUT): $(TMPS)
$(MYCXX) $(PROFILE_FLAGS) $^ -o "$@" $(LIBS)
Expand Down
2 changes: 1 addition & 1 deletion opengl/Makefile_params
@@ -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
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
36 changes: 31 additions & 5 deletions opengl/README.md
@@ -1,10 +1,36 @@
# OpenGL

1. [Introduction](introduction.md)
1. [glinfo](glinfo.c)
1. [GLUT](glut.md)
1. [Lightning](lightning.md)
1. Examples
1. [GLUT hello world](hello_world.c)
1. [glinfo](glinfo.c)
1. [Triangle](triangl.c)
1. [glOrtho](ortho.c)
1. [glShadeModel](shade_model.c)
1. [glViewport](viewport.c)
1. [Depth](depth.c)
1. Transformations
1. [glTranslate](glTranslate.c)
1. Lightning
1. [Ambient](ambient.c)
1. Diffuse
1. [glNormal](normal.c)
1. [Diffuse infinity](diffuse_infinity.c)
1. [Diffuse shadow](diffuse_shadow.c)
1. [Diffuse attenuation](diffuse_attenuation.c)
1. [glDrawPixels (TODO)](draw_pixels.c)
1. [Culling](culling.c)
1. GLUT
1. [glutBitmapCharacter](bitmap_character.c)
1. [GLX](glx.c)
1. Performance
1. [Many triangles](many_triangles.c)
1. [main](main.cpp)
1. bouncing_balls
1. [one](bouncing_balls_one.cpp)
1. [many](bouncing_balls_many.cpp)
1. [Texture FPS](texture_fps.cpp)
1. Mini projects
1. bouncing_balls
1. [one](bouncing_balls_one.cpp)
1. [many](bouncing_balls_many.cpp)
1. [Texture FPS](texture_fps.cpp)
1. [Bibliography](bibliography.md)
32 changes: 32 additions & 0 deletions opengl/TODO.md
Expand Up @@ -2,3 +2,35 @@

- Are the OpenGL types fixed size? How to properly printf them?
- How to select the rendering device? <http://stackoverflow.com/questions/6036292/select-a-graphic-device-in-windows-opengl> `prime-select`?
- Do everything without GLUT for Linux to learn what is going on. Windowing and events are likely not worth the trouble...
- <http://stackoverflow.com/questions/879035/initializing-opengl-without-glut>
- <http://stackoverflow.com/questions/5608767/learning-opengl-without-glut>
- <https://www.opengl.org/discussion_boards/showthread.php/177999-GCC-OpenGL-without-glut>
- 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.
- <http://gamedev.stackexchange.com/questions/72044/why-do-we-use-4x4-matrices-to-transform-things-in-3d>
- <http://stackoverflow.com/questions/13647108/matrix-stacks-in-opengl-deprecated>
- <http://stackoverflow.com/questions/3755998/what-is-the-point-of-the-matrix-stack-in-opengl>
- <http://gamedev.stackexchange.com/questions/83725/new-to-opengl-having-trouble-understanding-matrix-transformation>
- <https://www.reddit.com/r/learnprogramming/comments/1sipqz/why_does_opengl_use_4x4_matrices_to_describe_3d/>

1) Why use matrices at all? Answer: to pre-calculate multiple combined operations.
2) Why 4D for 3D objects? Let's see 3D for 2D and then we're done.

TODO: world, view and projection transforms?
- Make image spin with mouse drag:
- http://stackoverflow.com/questions/19884182/moving-a-drawing-around-in-opengl-with-mouse
- http://stackoverflow.com/questions/16342442/drag-and-drop-of-images-using-opengl-c
- http://stackoverflow.com/questions/20288861/mouse-drag-object-in-opengl-glut
- http://stackoverflow.com/questions/5734794/mouse-movement-opengl
- http://stackoverflow.com/questions/1426415/opengl-moving-camera-with-mouse
- http://gamedev.stackexchange.com/questions/53333/how-to-implement-a-basic-arcball-camera-in-opengl-with-glm
- VAO, VBO http://stackoverflow.com/questions/11821336/what-are-vertex-array-objects
- Immediate mode vs retained mode:
- immediate mode is deprecated. So like, glBegin, glEnd and glVertex which we use in every test program are deprecated. Lol.
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
- http://stackoverflow.com/questions/6733934/what-does-immediate-mode-mean-in-opengl Hello world requested but not provided, too long, lol. Opportunity!
- http://gamedev.stackexchange.com/questions/21733/why-does-opengl-3-only-allow-vbos
- https://en.wikipedia.org/wiki/Stencil_buffer
- shading
- vertex shader
- fragment shader
59 changes: 59 additions & 0 deletions opengl/ambient.c
@@ -0,0 +1,59 @@
/*
The triangle only becomes gray because ambient light is halved:
http://forum.unity3d.com/threads/why-is-ambient-light-at-half-strength.33955/
*/

#include <stdlib.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

static void init(void) {
GLfloat light0_ambient[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light0_position[] = {0.0, -1.0, 0.0, 0.0};
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glColorMaterial(GL_FRONT, GL_AMBIENT);
glEnable(GL_COLOR_MATERIAL);
}

static void draw_triangle() {
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
}

static void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
draw_triangle();
glEnd();
glFlush();
}

static void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}
20 changes: 18 additions & 2 deletions opengl/bibliography.md
@@ -1,12 +1,28 @@
# Bibliography

Standards:

- <https://www.opengl.org/registry/#oldspecs> Khronos standards

From there you can reach for example:

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

- Demos:
Tutorials with code:

- <https://github.com/JoeyDeVries/LearnOpenGL> <http://learnopengl.com/>
- <https://github.com/opengl-tutorials/ogl>
- <https://github.com/capnramses/antons_opengl_tutorials_book> Some hardcore examples. Uses GLFW.
- <https://github.com/Overv/Open.GL/tree/master/content/code>
- <http://www.glprogramming.com/red/>
- <https://github.com/tomdalling/opengl-series> <http://www.tomdalling.com/blog/category/modern-opengl/> Modern, no immediate rendering.
- <http://www.videotutorialsrock.com/opengl_tutorial/normalize/video.php>

Java:

- <https://github.com/OskarVeerhoek/YouTube-tutorials>

Demos:

- <https://github.com/prideout/recipes>
- <https://github.com/prideout/recipes>
72 changes: 72 additions & 0 deletions opengl/bitmap_character.c
@@ -0,0 +1,72 @@
/*
# glutBitmapCharacter
http://stackoverflow.com/questions/8847899/opengl-how-to-draw-text-using-only-opengl-methods
*/

#include <stdlib.h>
#include <string.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

static void init() {
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}

static void drawString (void *font, char *s, GLfloat x, GLfloat y, GLfloat z){
unsigned int i;
/* Must come before glRasterPos. */
glColor3f(0.0f, 0.0f, 1.0f);
glRasterPos3f(x, y, z);
for (i = 0; i < strlen(s); i++)
glutBitmapCharacter(font, s[i]);
}

static void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();

/* This is drawn as close as possible to the camera. */
drawString(GLUT_BITMAP_HELVETICA_18, "bottom", -1.0, -1.0, 3.5);

/* Depth is considered. Things that come behind are hidden. */
drawString(GLUT_BITMAP_HELVETICA_18, "behind", 0.0, 0.0, -1.0);
drawString(GLUT_BITMAP_HELVETICA_18, "front", 0.0, 0.5, 1.0);

/* The font size is fixed no matter how far back we are.*/
drawString(GLUT_BITMAP_HELVETICA_18, "far", 200.0, 200.0, -990.0);

glFlush();
}

static void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}
10 changes: 4 additions & 6 deletions opengl/bouncing_balls_many.cpp
Expand Up @@ -2,7 +2,10 @@
Many spheres bouncing inside a cube with gravity,
energy dissipation on impact, and impact between the spheres.
TODO: DRY up with bouncing_balls_one.cpp
TODO: DRY
- up with bouncing_balls_one.cpp
- balls flicker a lot. Why?
*/

#include <cstdio>
Expand Down Expand Up @@ -171,9 +174,6 @@ void calc_new_scene(void){
}

void draw_cube(){

glPushMatrix();

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3fv(DARK_GRAY);

Expand Down Expand Up @@ -214,8 +214,6 @@ void draw_cube(){
glLineWidth(2.0);
glutWireCube(2.0);

glPopMatrix();

}

void draw_spheres() {
Expand Down
16 changes: 2 additions & 14 deletions opengl/bouncing_balls_one.cpp
Expand Up @@ -63,7 +63,7 @@ void calc_new_scene(void) {
old_t = t;

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

//cout << "\n---------------------------\n";
//cout << "center\n" << spheres[i].center.str() << "\n";
Expand All @@ -83,14 +83,10 @@ void calc_new_scene(void) {
spheres[i].speed = spheres[i].speed + GRAVITY*dt;
}
}

glutPostRedisplay();
}

void draw_cube(){

glPushMatrix();

glColor3fv(DARK_GRAY);

// back
Expand Down Expand Up @@ -129,33 +125,25 @@ void draw_cube(){
glColor3fv(WHITE);
glLineWidth(2.0);
glutWireCube(2.0);

glPopMatrix();

}

void draw_spheres() {
for (int i=0; i<total_spheres; i++){
spheres[i].draw();
}

}

void draw_scene(void) {

glLoadIdentity();
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);

draw_cube();
draw_spheres();

}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_scene();
glutSwapBuffers();

}

void reshape(int w, int h) {
Expand All @@ -180,7 +168,7 @@ int main(int argc, char** argv) {
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glClearColor(clear_color_r,clear_color_g,clear_color_b,1.0);
glClearColor(clear_color_r, clear_color_g, clear_color_b, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glEnable(GL_POLYGON_OFFSET_LINE);
Expand Down
2 changes: 1 addition & 1 deletion opengl/configure
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
sudo aptitude update
sudo aptitude install -y freeglut3-dev libopencv-dev
sudo aptitude install -y freeglut3-dev libglfw3-dev libopencv-dev

0 comments on commit 7346c24

Please sign in to comment.