// // Example code for OpenGL programming // #include #include #include #include int nFPS = 60; float fRotateAngle = 0.f; float randomVar = 1; float heyThisGetsSentToAShader = 0; GLuint vs; GLuint program; const GLchar* vertex_shader_code = "\ uniform float thisCameFromC;\ vec4 p;\ void main(void){\ p = gl_Vertex;\ p.x = p.x + sin(thisCameFromC)*sin(p.y);\ p.y = p.y + cos(thisCameFromC)*sin(p.x);\ p.z = sin(5.0*p.x+thisCameFromC )*0.25;\ gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * p;\ }"; const GLchar* fragment_shader_code = "\ void main(void) {\ gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\ }"; void printShaderInfoLog(GLuint obj); GLint uniformLocation; void init(void) { // init your data, setup OpenGL environment here glClearColor(0.3,0.3,0.3,1.0); // clear color is gray glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // uncomment this function if you only want to draw wireframe model //Shader time! GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, (const GLchar **) &vertex_shader_code, NULL); glCompileShader(vs); printShaderInfoLog(vs); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, (const GLchar **) &fragment_shader_code, NULL); glCompileShader(fs); printShaderInfoLog(fs); program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); uniformLocation = glGetUniformLocation(program,"thisCameFromC"); glUseProgram(program); } /* Tastefully borrowed from http://zach.in.tu-clausthal.de/teaching/cg2_07/literatur/glsl_tutorial/index.html for your convenience */ void printShaderInfoLog(GLuint obj) { int infologLength = 0; int charsWritten = 0; char *infoLog; glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength); if (infologLength > 0) { infoLog = (char *)malloc(infologLength); glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog); printf("%s\n",infoLog); free(infoLog); } } void display(void) { // put your OpenGL display commands here glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glUniform1f(uniformLocation, heyThisGetsSentToAShader ); // reset OpenGL transformation matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // reset transformation matrix to identity // setup look at transformation so that // eye is at : (0,0,3) // look at center is at : (0,0,0) // up direction is +y axis gluLookAt(0.f,0.f,6.f,0.f,0.f,0.f,0.f,1.f,0.f); //glScalef(2,1,0); //glTranslatef(2,-2,1); // glPushMatrix(); glRotatef(fRotateAngle,0.f,0.f,1.f); glRotatef(90.0, 0.0, 0.0, 1.0); glTranslatef(2.0, 2.0, 0.0); glScalef(1.0, 1.5, 1.0); glRotatef(fRotateAngle*3,0.f,0.f,1.f); // Test drawing a solid teapot glColor3f(1.0,0.0,0.0); // set current color to Red glutSolidTeapot(1.f); // call glut utility to draw a solid teapot glPopMatrix(); glPushMatrix(); //glScalef(1,1,randomVar); //glScalef(2,2,2); glScalef(randomVar,randomVar,randomVar); glColor3f(1.0,1.0,0.0); // set current color to Red glutSolidTeapot(1.f); // call glut utility to draw a solid teapot glPopMatrix(); glutSwapBuffers(); // swap front/back framebuffer to avoid flickering } void reshape (int w, int h) { // reset viewport ( drawing screen ) size glViewport(0, 0, w, h); float fAspect = ((float)w)/h; // reset OpenGL projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70.f,fAspect,0.001f,30.f); } void keyboard(unsigned char key, int x, int y) { printf(" Key has been pressed: %c\n", key); if (key == 'w') { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // uncomment this function if you only want to draw wireframe model } else if (key == 'f') { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // uncomment this function if you only want to draw wireframe model } // put your keyboard control here if (key == 27) { // ESC hit, so quit printf("demonstration finished.\n"); exit(0); } } void mouse(int button, int state, int x, int y) { // process your mouse control here if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) printf("push left mouse button.\n"); } void motion(int x, int y) { //printf("mouse has been moved at %d,%d\n", x, y); randomVar = (float)x/30.f; } void timer(int v) { fRotateAngle += 1.f; // change rotation angles glutTimerFunc(1000/nFPS,timer,v); // restart timer again heyThisGetsSentToAShader += 0.01f; //glutTimerFunc(0,timer,v); // restart timer again glutPostRedisplay(); // trigger display function by sending redraw into message queue } int main(int argc, char* argv[]) { glutInit(&argc, (char**)argv); // set up for double-buffering & RGB color buffer & depth test glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ((const char*)"Hello Triangle"); // initalize glew GLenum res = glewInit(); if (res != GLEW_OK) { fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); return 1; } init(); // setting up user data & OpenGL environment // set up the call-back functions glutDisplayFunc(display); // called when drawing glutReshapeFunc(reshape); // called when change window size glutKeyboardFunc(keyboard); // called when received keyboard interaction glutMouseFunc(mouse); // called when received mouse interaction glutMotionFunc(motion); // called when received mouse interaction glutTimerFunc(100,timer,nFPS); // a periodic timer. Usually used for updating animation glutMainLoop(); // start the main message-callback loop return 0; }