Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

update opengl programming

  • Loading branch information...
commit c94154d4449c837f38ef697c1bf796a22b9f84a8 1 parent 0d8f83d
Qing Bao authored
36 OpenGL_Programming/README
... ... @@ -0,0 +1,36 @@
  1 +OpenGL变量类型对应C数据类型:
  2 +
  3 + +---------------------------------------------------------------------------+
  4 + | OpenGL数据类型 | 内部表示形式 | 对应的C数据类型 | C字面值后缀 |
  5 + +---------------------------------------------------------------------------+
  6 + |GLbyte | 8位整数 | signed char | b |
  7 + +---------------------------------------------------------------------------+
  8 + |GLshort | 16位整数 | short | s |
  9 + +---------------------------------------------------------------------------+
  10 + |GLint,GLsizei | 23位整数 | long | l |
  11 + +---------------------------------------------------------------------------+
  12 + |GLfloat,GLclampf | 32位浮点数 | float | f |
  13 + +---------------------------------------------------------------------------+
  14 + |GLdouble,GLclampd | 64位整数 | double | d |
  15 + +---------------------------------------------------------------------------+
  16 + |GLubyte,GLboolean | 8位无符号整数 | unsigned char | ub |
  17 + +---------------------------------------------------------------------------+
  18 + |GLushort | 16位无符号整数 | unsigned short | us |
  19 + +---------------------------------------------------------------------------+
  20 + |GLuint,GLenum,GLbitfield | 32位无符号整数 | unsigned long | ui |
  21 + +---------------------------------------------------------------------------+
  22 + |GLchar | 8位字符 | char | 无 |
  23 + +---------------------------------------------------------------------------+
  24 + |GLsizeiptr,GLintptr | 本地指针 | ptrdiff_t | 无 |
  25 + +---------------------------------------------------------------------------+
  26 +OpenGL函数分析
  27 +
  28 + +----------------------------------------+
  29 + | glColor3f(...) |
  30 + / / \ \
  31 + ------------------------------------------
  32 + | gl库函数 | color根命令 | 3参数 | f参数类型 |
  33 + +----------------------------------------+
  34 +
  35 +
  36 +
114 OpenGL_Programming/anime_gl.c
... ... @@ -0,0 +1,114 @@
  1 +/* include OpenGL utility toolkit */
  2 +#include <GL/glut.h>
  3 +
  4 +
  5 +/* rectangle with position */
  6 +GLfloat xx1 = 0.0f;
  7 +GLfloat yy1 = 0.0f;
  8 +GLfloat rsize = 25;
  9 +
  10 +/* move pixel */
  11 +GLfloat xstep = 1.0f;
  12 +GLfloat ystep = 1.0f;
  13 +
  14 +GLfloat windowWidth;
  15 +GLfloat windowHeight;
  16 +
  17 +
  18 +void RenderScene(void)
  19 +{
  20 + /* use current color clear window */
  21 + glClear(GL_COLOR_BUFFER_BIT);
  22 + /* set red color */
  23 + glColor3f(1.0f,0.0f,0.0f);
  24 + /* Draws a filled rectangle with the current color */
  25 + glRectf(xx1,yy1,xx1 + rsize ,yy1 - rsize);
  26 + /* Refresh the drawing command and swaping */
  27 + glutSwapBuffers();
  28 +}
  29 +
  30 +/* free time glut function libraries call */
  31 +void TimerFunction(int value){
  32 + /* arrive left and right veer */
  33 + if (xx1 > windowWidth-rsize || xx1 < -windowWidth){
  34 + xstep = -xstep;
  35 + }
  36 + /* arrive top and buttom veer */
  37 + if (yy1 > windowHeight || yy1 < -windowHeight + rsize ){
  38 + ystep = -ystep;
  39 + }
  40 + xx1 += xstep;
  41 + yy1 += ystep;
  42 +
  43 + if(xx1 > (windowWidth-rsize + xstep))
  44 + xx1 = windowWidth-rsize-1;
  45 + else if (xx1 < -(windowWidth + xstep))
  46 + xx1 = - windowWidth-1;
  47 + if (yy1 > (windowHeight+ystep))
  48 + yy1 = windowHeight-1;
  49 + else if (yy1 < -(windowHeight - rsize + ystep))
  50 + yy1 = -windowHeight + rsize - 1;
  51 +
  52 + glutPostRedisplay();
  53 + glutTimerFunc(33,TimerFunction,1);
  54 +}
  55 +
  56 + /* set romance status */
  57 +void SetupRC(void){
  58 + /* set clear color */
  59 + glClearColor(0.0f,0.0f,1.0f,1.0f);
  60 +}
  61 +
  62 +void ChangeSize(int w ,int h){
  63 + GLfloat aspectRatio;
  64 + if (h == 0){
  65 + h = 1;
  66 + }
  67 + /* set view for window size */
  68 + glViewport(0,0,w,h);
  69 + /* reset coordinate system */
  70 + glMatrixMode(GL_PROJECTION);
  71 + glLoadIdentity();
  72 + /* foundation cut area */
  73 + aspectRatio = (GLfloat)w / (float)h;
  74 + if (w <= h)
  75 + {
  76 + windowWidth = 100;
  77 + windowHeight = 100 / aspectRatio;
  78 + glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
  79 + }
  80 + else
  81 + {
  82 + windowWidth = 100 * aspectRatio;
  83 + windowHeight = 100;
  84 + glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
  85 + }
  86 + glMatrixMode(GL_MODELVIEW);
  87 + glLoadIdentity();
  88 +}
  89 +
  90 +
  91 +int main(int argc, char *argv[])
  92 +{
  93 + /* initialize glut */
  94 + glutInit(&argc, argv);
  95 + /* use what display mode
  96 + use single buffer,and use RGBA color mode
  97 + */
  98 + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  99 + /* initialize window position */
  100 + //glutInitWindowPosition(200, 200);
  101 + /* initialize window size */
  102 + glutInitWindowSize(800, 600);
  103 + /* Create window,and set title */
  104 + glutCreateWindow("rectangle opengl");
  105 + /* Displays the callback function */
  106 + glutDisplayFunc(RenderScene);
  107 + /* change window size callback function */
  108 + glutReshapeFunc(ChangeSize);
  109 + glutTimerFunc(33,TimerFunction,1);
  110 + SetupRC();
  111 + /* Start glut framework, defined screen display callback function and other function. */
  112 + glutMainLoop();
  113 + return 0;
  114 +}
39 OpenGL_Programming/first_gl.c
... ... @@ -0,0 +1,39 @@
  1 +/* include OpenGL utility toolkit */
  2 +#include <GL/glut.h>
  3 +#include <math.h>
  4 +
  5 +
  6 +void myDisplay(void)
  7 +{
  8 + /* display a triangles */
  9 +
  10 + glClear(GL_COLOR_BUFFER_BIT);
  11 + glBegin (GL_TRIANGLES);
  12 + glVertex3f (0,1,0);
  13 + glVertex3f (-1, 0, 0);
  14 + glVertex3f (1, 0, 0);
  15 + glEnd ();
  16 + glFlush();
  17 +}
  18 +
  19 +
  20 +int main(int argc, char *argv[])
  21 +{
  22 + /* initialize glut */
  23 + glutInit(&argc, argv);
  24 + /* use what display mode
  25 + use single buffer,and use RGBA color mode
  26 + */
  27 + glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  28 + /* initialize window position */
  29 + glutInitWindowPosition(200, 200);
  30 + /* initialize window size */
  31 + glutInitWindowSize(500, 500);
  32 + /* Create window,and set title */
  33 + glutCreateWindow("first opengl");
  34 + /* Displays the callback function */
  35 + glutDisplayFunc(&myDisplay);
  36 + /* Start glut framework, defined screen display callback function and other function. */
  37 + glutMainLoop();
  38 + return 0;
  39 +}
66 OpenGL_Programming/rectangle_gl.c
... ... @@ -0,0 +1,66 @@
  1 +/* include OpenGL utility toolkit */
  2 +#include <GL/glut.h>
  3 +#include <math.h>
  4 +
  5 +void RenderScene(void)
  6 +{
  7 + /* use current color clear window */
  8 + glClear(GL_COLOR_BUFFER_BIT);
  9 + /* set red color */
  10 + glColor3f(1.0f,0.0f,0.0f);
  11 + /* Draws a filled rectangle with the current color */
  12 + glRectf(-15.0f,15.0f,15.0f,-15.0f);
  13 + /* Refresh the drawing command */
  14 + glFlush();
  15 +}
  16 + /* set romance status */
  17 +void SetupRC(void){
  18 + /* set clear color */
  19 + glClearColor(0.0f,0.0f,1.0f,1.0f);
  20 +}
  21 +
  22 +void ChangeSize(GLsizei w ,GLsizei h){
  23 + GLfloat aspectRatio;
  24 + if (h == 0){
  25 + h = 1;
  26 + }
  27 + /* set view for window size */
  28 + glViewport(0,0,w,h);
  29 + /* reset coordinate system */
  30 + glMatrixMode(GL_PROJECTION);
  31 + glLoadIdentity();
  32 + /* foundation cut area */
  33 + aspectRatio = (GLfloat)w / (float)h;
  34 + if (w <= h){
  35 + /* projection mode command */
  36 + glOrtho(-100.0,100.0,-100.0 / aspectRatio,100.0 / aspectRatio,1.0,-1.0);
  37 + }else
  38 + glOrtho(-100.0 * aspectRatio,100.0 * aspectRatio, -100.0,-100.0,1.0,-1.0);
  39 + glMatrixMode(GL_MODELVIEW);
  40 + glLoadIdentity();
  41 +}
  42 +
  43 +
  44 +int main(int argc, char *argv[])
  45 +{
  46 + /* initialize glut */
  47 + glutInit(&argc, argv);
  48 + /* use what display mode
  49 + use single buffer,and use RGBA color mode
  50 + */
  51 + glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  52 + /* initialize window position */
  53 + //glutInitWindowPosition(200, 200);
  54 + /* initialize window size */
  55 + //glutInitWindowSize(500, 500);
  56 + /* Create window,and set title */
  57 + glutCreateWindow("rectangle opengl");
  58 + /* Displays the callback function */
  59 + glutDisplayFunc(RenderScene);
  60 + /* change window size callback function */
  61 + glutReshapeFunc(ChangeSize);
  62 + SetupRC();
  63 + /* Start glut framework, defined screen display callback function and other function. */
  64 + glutMainLoop();
  65 + return 0;
  66 +}

0 comments on commit c94154d

Please sign in to comment.
Something went wrong with that request. Please try again.