Created
December 20, 2011 18:34
-
-
Save dpiponi/1502653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// On MacOSX compile with: | |
// g++ -framework OpenGL -framework GLUT -o example example.cpp | |
// | |
#include <stdlib.h> | |
#include <GLUT/glut.h> | |
GLuint program; | |
const char *vertexSource = | |
"varying vec4 pos;\n" | |
"void main() {\n" | |
" gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n" | |
" pos = gl_MultiTexCoord0;\n" | |
"}\n"; | |
const char *fragmentSource = | |
"varying vec4 pos;\n" | |
"void main() {\n" | |
" gl_FragColor = vec4(pos.x, pos.y, 0.5, 1.0);\n" | |
"}\n"; | |
void init(void) { | |
glClearColor (0.0, 0.0, 0.0, 0.0); | |
glShadeModel(GL_FLAT); | |
glEnable(GL_DEPTH_TEST); | |
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(vertexShader, 1, &vertexSource, NULL); | |
glShaderSource(fragmentShader, 1, &fragmentSource, NULL); | |
glCompileShader(vertexShader); | |
glCompileShader(fragmentShader); | |
program = glCreateProgram(); | |
glAttachShader(program, vertexShader); | |
glAttachShader(program, fragmentShader); | |
glLinkProgram(program); | |
} | |
void display(void) { | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glUseProgram(program); | |
glBegin(GL_QUADS); | |
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); | |
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0); | |
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0); glVertex3f(1.0, 1.0, 0.0); | |
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); | |
glEnd(); | |
glFlush(); | |
} | |
void reshape(int w, int h) { | |
glViewport(0, 0, (GLsizei)w, (GLsizei)h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 1.0, 30.0); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glTranslatef(0.0, 0.0, -3.6); | |
} | |
void keyboard (unsigned char key, int x, int y) { | |
switch (key) { | |
case 27: | |
exit(0); | |
break; | |
default: | |
break; | |
} | |
} | |
int main(int argc, char** argv) { | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); | |
glutInitWindowSize(1280, 1280); | |
glutInitWindowPosition(100, 100); | |
glutCreateWindow(argv[0]); | |
init(); | |
glutDisplayFunc(display); | |
glutReshapeFunc(reshape); | |
glutKeyboardFunc(keyboard); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment