Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/glew.h>
- #define GLUT_DISABLE_ATEXIT_HACK
- #include <GL/glut.h>
- #pragma comment(lib, "glew32.lib")
- #define USE_MATH_DEFINES
- #include <math.h>
- #define WINDSIZEX 500
- #define WINDSIZEY 500
- static char* vsSource = "#version 120 \n\
- attribute vec4 aPosition; \n\
- attribute vec4 aColor; \n\
- varying vec4 vColor; \n\
- uniform mat4 uModel; \n\
- uniform mat4 uView; \n\
- uniform mat4 uProj; \n\
- uniform float uY_change;\n\
- void main(void) { \n\
- vec4 pos = aPosition;\n\
- pos.y = pos.y + uY_change;\n\
- gl_Position = uProj * uView * uModel * pos; \n\
- vColor = aColor; \n\
- }";
- static char* fsSource = "#version 120 \n\
- varying vec4 vColor; \n\
- void main(void) { \n\
- gl_FragColor = vColor; \n\
- }";
- GLfloat theta = 0;
- GLfloat y_change = 0;
- bool up = true;
- GLfloat resize1 = 0.1f;
- GLuint vs = 0;
- GLuint fs = 0;
- GLuint prog = 0;
- GLfloat* vertices1;
- GLushort* indices1;
- GLfloat* vertices2;
- GLushort* indices2;
- int count;
- GLfloat scale = 1.0f;
- GLuint i_number = 0;
- GLuint v_number = 0;
- GLfloat vertices[] = {
- -0.5, -0.5, -0.5, 1.0, // 0
- -0.5, -0.5, +0.5, 1.0, // 1
- -0.5, +0.5, -0.5, 1.0, // 2
- -0.5, +0.5, +0.5, 1.0, // 3
- +0.5, -0.5, -0.5, 1.0, // 4
- +0.5, -0.5, +0.5, 1.0, // 5
- +0.5, +0.5, -0.5, 1.0, // 6
- +0.5, +0.5, +0.5, 1.0, // 7
- };
- GLfloat colors[] = {
- 0.5, 0.5, 0.5, 1.0, // black
- 0.5, 0.5, 1.0, 1.0, // blue
- 0.5, 1.0, 0.5, 1.0, // green
- 0.5, 1.0, 1.0, 1.0, // cyan
- 1.0, 0.5, 0.5, 1.0, // red
- 1.0, 0.5, 1.0, 1.0, // magenta
- 1.0, 1.0, 0.5, 1.0, // yellow
- 1.0, 1.0, 1.0, 1.0, // white
- };
- GLushort indices[] = { // 36 points, 12 triangles
- 0, 4, 6,
- 6, 2, 0,
- 4, 5, 7,
- 7, 6, 4,
- 1, 3, 7,
- 7, 5, 1,
- 0, 2, 3,
- 3, 1, 0,
- 2, 6, 7,
- 7, 3, 2,
- 0, 1, 5,
- 5, 4, 0,
- };
- GLuint vbo[1];
- GLuint vbo1;
- char buf[1024];
- GLfloat motion_start[3]; // start vector, (x, y, z)
- GLfloat motion_end[3]; // end vector, (x, y, z)
- GLfloat matModel[16] = {
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 0.0, 1.0,
- };
- GLfloat matView[16] = {
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 0.0, 1.0,
- };
- GLfloat matProj[16] = {
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 0.0, 1.0,
- };
- GLfloat matA[16];
- GLfloat matB[16];
- void matCopy(GLfloat a[16], GLfloat b[16]) { // a = b;
- a[0] = b[0]; a[4] = b[4]; a[8] = b[8]; a[12] = b[12];
- a[1] = b[1]; a[5] = b[5]; a[9] = b[9]; a[13] = b[13];
- a[2] = b[2]; a[6] = b[6]; a[10] = b[10]; a[14] = b[14];
- a[3] = b[3]; a[7] = b[7]; a[11] = b[11]; a[15] = b[15];
- }
- void matIden(GLfloat a[16]) { // a = I
- a[0] = 1.0; a[4] = 0.0; a[8] = 0.0; a[12] = 0.0;
- a[1] = 0.0; a[5] = 1.0; a[9] = 0.0; a[13] = 0.0;
- a[2] = 0.0; a[6] = 0.0; a[10] = 1.0; a[14] = 0.0;
- a[3] = 0.0; a[7] = 0.0; a[11] = 0.0; a[15] = 1.0;
- }
- void matMult(GLfloat c[16], GLfloat a[16], GLfloat b[16]) { // C = A * B
- c[0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3];
- c[1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3];
- c[2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3];
- c[3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3];
- //
- c[4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7];
- c[5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7];
- c[6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7];
- c[7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7];
- //
- c[8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11];
- c[9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11];
- c[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11];
- c[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11];
- //
- c[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15];
- c[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15];
- c[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
- c[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];
- }
- void vecMult(GLfloat v[4], GLfloat m[16], GLfloat u[4]) { // v = M * u
- v[0] = m[0] * u[0] + m[4] * u[1] + m[8] * u[2] + m[12] * u[3];
- v[1] = m[1] * u[0] + m[5] * u[1] + m[9] * u[2] + m[13] * u[3];
- v[2] = m[2] * u[0] + m[6] * u[1] + m[10] * u[2] + m[14] * u[3];
- v[3] = m[3] * u[0] + m[7] * u[1] + m[11] * u[2] + m[15] * u[3];
- }
- void myinit(void) {
- GLint status;
- GLuint loc;
- // vs: vertex shader
- vs = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vs, 1, &vsSource, NULL);
- glCompileShader(vs); // compile to get .OBJ
- glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
- printf("vs compile status = %s\n", (status == GL_TRUE) ? "true" : "false");
- glGetShaderInfoLog(vs, sizeof(buf), NULL, buf);
- printf("vs log = [%s]\n", buf);
- // fs: fragment shader
- fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fs, 1, &fsSource, NULL);
- glCompileShader(fs); // compile to get .OBJ
- glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
- printf("fs compile status = %s\n", (status == GL_TRUE) ? "true" : "false");
- glGetShaderInfoLog(fs, sizeof(buf), NULL, buf);
- printf("fs log = [%s]\n", buf);
- // prog: program
- prog = glCreateProgram();
- glAttachShader(prog, vs);
- glAttachShader(prog, fs);
- glLinkProgram(prog); // link to get .EXE
- glGetProgramiv(prog, GL_LINK_STATUS, &status);
- printf("program link status = %s\n", (status == GL_TRUE) ? "true" : "false");
- glGetProgramInfoLog(prog, sizeof(buf), NULL, buf);
- printf("link log = [%s]\n", buf);
- glValidateProgram(prog);
- glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
- printf("program validate status = %s\n", (status == GL_TRUE) ? "true" : "false");
- glGetProgramInfoLog(prog, sizeof(buf), NULL, buf);
- printf("validate log = [%s]\n", buf);
- glUseProgram(prog); // execute it !
- // VBO setting
- glGenBuffers(1, vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- glBufferData(GL_ARRAY_BUFFER, 2 * 8 * 4 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
- glBufferSubData(GL_ARRAY_BUFFER, 0, 8 * 4 * sizeof(GLfloat), vertices);
- glBufferSubData(GL_ARRAY_BUFFER, 8 * 4 * sizeof(GLfloat), 8 * 4 * sizeof(GLfloat), colors);
- // provide the vertex attributes
- loc = glGetAttribLocation(prog, "aPosition");
- glEnableVertexAttribArray(loc);
- glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(0));
- // provide the color attributes
- loc = glGetAttribLocation(prog, "aColor");
- glEnableVertexAttribArray(loc);
- glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(8 * 4 * sizeof(GLfloat)));
- // depth buffer enabled
- glEnable(GL_DEPTH_TEST);
- glGenBuffers(1, &vbo1);
- glBindBuffer(GL_ARRAY_BUFFER, vbo1);
- glBufferData(GL_ARRAY_BUFFER, (v_number+4)*4*sizeof(GLfloat), vertices1, GL_STATIC_DRAW);
- }
- void mykeyboard(unsigned char key, int x, int y) {
- switch (key) {
- case 27: // ESCAPE
- exit(0);
- break;
- }
- }
- void myidle(void) {
- // nothing
- }
- void myreshape(int width, int height) {
- int shift;
- if (width >= height) {
- shift = (width - height) / 2;
- glViewport(shift, 0, height, height);
- } else {
- shift = (height - width) / 2;
- glViewport(0, shift, width, width);
- }
- }
- void mydisplay(void) {
- GLuint loc;
- // clear
- glClearColor(0.3f, 0.3f, 0.3f, 1.0f); // gray
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // provide the matrices
- loc = glGetUniformLocation(prog, "uModel");
- glUniformMatrix4fv(loc, 1, GL_FALSE, matModel);
- loc = glGetUniformLocation(prog, "uView");
- glUniformMatrix4fv(loc, 1, GL_FALSE, matView);
- loc = glGetUniformLocation(prog, "uProj");
- glUniformMatrix4fv(loc, 1, GL_FALSE, matProj);
- loc = glGetUniformLocation(prog, "uY_change");
- glUniform1f(loc, y_change);
- // draw a triangle
- glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indices);
- // flush all
- glFlush();
- glutSwapBuffers();
- }
- void setViewMat(void) {
- GLfloat eye[3], at[3], up[3];
- GLfloat p[3], n[3], v[3], u[3];
- GLfloat l;
- GLfloat vec[4], uec[4];
- int i;
- eye[0] = sin(theta);
- eye[1] = 0;
- eye[2] = cos(theta);
- at[0] = at[1] = at[2] = 0;
- up[0] = up[2] = 0;
- up[1] = 1.0f;
- // p = eye
- p[0] = eye[0];
- p[1] = eye[1];
- p[2] = eye[2];
- // n = (at - eye)
- n[0] = at[0] - eye[0];
- n[1] = at[1] - eye[1];
- n[2] = at[2] - eye[2];
- l = sqrtf(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
- n[0] /= l; n[1] /= l; n[2] /= l;
- // u = up * n
- u[0] = up[1] * n[2] - up[2] * n[1];
- u[1] = up[2] * n[0] - up[0] * n[2];
- u[2] = up[0] * n[1] - up[1] * n[0];
- l = sqrtf(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]);
- u[0] /= l; u[1] /= l; u[2] /= l;
- // v = n * u;
- v[0] = n[1] * u[2] - n[2] * u[1];
- v[1] = n[2] * u[0] - n[0] * u[2];
- v[2] = n[0] * u[1] - n[1] * u[0];
- l = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
- v[0] /= l; v[1] /= l; v[2] /= l;
- //
- // view matrix
- matA[0] = u[0]; matA[4] = u[1]; matA[8] = u[2]; matA[12] = 0;
- matA[1] = v[0]; matA[5] = v[1]; matA[9] = v[2]; matA[13] = 0;
- matA[2] = n[0]; matA[6] = n[1]; matA[10] = n[2]; matA[14] = 0;
- matA[3] = 0.0; matA[7] = 0.0; matA[11] = 0.0; matA[15] = 1.0;
- matB[0] = 1; matB[4] = 0; matB[8] = 0; matB[12] = -p[0];
- matB[1] = 0; matB[5] = 1; matB[9] = 0; matB[13] = -p[1];
- matB[2] = 0; matB[6] = 0; matB[10] = 1; matB[14] = -p[2];
- matB[3] = 0.0; matB[7] = 0.0; matB[11] = 0.0; matB[15] = 1.0;
- matMult(matView, matA, matB);
- for (i = 0; i < 8; i++) {
- uec[0] = vertices[4 * i + 0];
- uec[1] = vertices[4 * i + 1];
- uec[2] = vertices[4 * i + 2];
- uec[3] = vertices[4 * i + 3];
- vecMult(vec, matView, uec);
- }
- fflush(stdout);
- }
- void setProjMat(void) {
- GLfloat xmin, xmax;
- GLfloat ymin, ymax;
- GLfloat zmin, zmax;
- GLfloat vec[4], uec[4], wec[4];
- int i;
- // input
- xmin = -2;
- xmax = 2;
- ymin = -2;
- ymax = 2;
- zmin = -2;
- zmax = 2;
- // view matrix
- matProj[0] = 2.0F / (xmax - xmin); matProj[4] = 0.0; matProj[8] = 0.0;
- matProj[12] = -(xmax + xmin) / (xmax - xmin);
- matProj[1] = 0.0; matProj[5] = 2.0F / (ymax - ymin); matProj[9] = 0.0;
- matProj[13] = -(ymax + ymin) / (ymax - ymin);
- matProj[2] = 0.0; matProj[6] = 0.0; matProj[10] = 2.0F / (zmax - zmin);
- matProj[14] = -(zmax + zmin) / (zmax - zmin);
- matProj[3] = 0.0; matProj[7] = 0.0; matProj[11] = 0.0; matProj[15] = 1.0;
- for (i = 0; i < 8; i++) {
- uec[0] = vertices[4 * i + 0];
- uec[1] = vertices[4 * i + 1];
- uec[2] = vertices[4 * i + 2];
- uec[3] = vertices[4 * i + 3];
- vecMult(wec, matView, uec);
- vecMult(vec, matProj, wec);
- }
- }
- void myRotate(int val)
- {
- theta += val * 0.0001F;
- y_change += up ? 0.05F : -0.05F;
- if(y_change >= 0.5 && up)
- up = false;
- else if(!up && y_change <= -0.5)
- up = true;
- setViewMat();
- glutPostRedisplay();
- glutTimerFunc(16, myRotate, val);
- }
- int main(int argc, char* argv[]) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(WINDSIZEX, WINDSIZEY);
- glutInitWindowPosition(0, 0);
- glutCreateWindow("simple");
- glutDisplayFunc(mydisplay);
- glutKeyboardFunc(mykeyboard);
- glutReshapeFunc(myreshape);
- glutIdleFunc(myidle);
- glutTimerFunc(16, myRotate, 100);
- glewInit();
- myinit();
- setViewMat();
- setProjMat();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement