Skip to content

Instantly share code, notes, and snippets.

@madbence
Created April 27, 2014 10:21
Show Gist options
  • Save madbence/11342280 to your computer and use it in GitHub Desktop.
Save madbence/11342280 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdlib.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
const int screenWidth = 600;
const int screenHeight = 600;
void onInitialization( ) {
glViewport(0, 0, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
gluPerspective(60,1,1,1000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float ambient[]={.3,.3,.3},diffuse[]={.8,.8,.8,1};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient);
}
int res=2;
void onDisplay() {
float t = glutGet(GLUT_ELAPSED_TIME);
float s = sin(t/500)*5;
float c = cos(t/500)*5;
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(20,20,10,0,0,0,0,0,1);
float position[]={s,c,1,1};
glLightfv(GL_LIGHT0, GL_POSITION, position);
float col[]={.7,.7,.7};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col);
glBegin(GL_TRIANGLES);
for(int i=0;i<res;i++) {
for(int j=0;j<res;j++) {
float x0 = -10+i*(20./res);
float y0 = -10+j*(20./res);
float x1 = x0+20./res;
float y1 = y0+20./res;
glVertex2f(x0, y0);
glVertex2f(x1, y0);
glVertex2f(x1, y1);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glVertex2f(x0, y1);
}
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void onIdle() { glutPostRedisplay(); }
void onKeyboard(unsigned char k, int x, int y) {
res += k == 'k';
res -= k == 'j';
res = res < 1 ? 1 : res;
glutPostRedisplay();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Grafika funky feladat");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
onInitialization();
glutDisplayFunc(onDisplay);
glutIdleFunc(onIdle);
glutKeyboardFunc(onKeyboard);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment