GLSL 使用demo

来源:互联网 发布:淘宝4.6 编辑:程序博客网 时间:2024/04/28 13:52
/*  Simple Demo for GLSL 2.0  www.lighthouse3d.com*/#include <stdio.h>#include <stdlib.h>#include <GL/glew.h>#include <GL/glut.h>#include "textfile.h"GLuint v,f,f2,p;float lpos[4] = {1,0.5,1,0};void changeSize(int w, int h) { // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0)  h = 1; float ratio = 1.0* w / h; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity();  // Set the viewport to be the entire window    glViewport(0, 0, w, h); // Set the correct perspective. gluPerspective(45,ratio,1,1000); glMatrixMode(GL_MODELVIEW);}void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0.0,0.0,5.0,         0.0,0.0,-1.0,     0.0f,1.0f,0.0f); glLightfv(GL_LIGHT0, GL_POSITION, lpos); glutSolidTeapot(1); glutSwapBuffers();}void processNormalKeys(unsigned char key, int x, int y) { if (key == 27)   exit(0);}void setShaders() { char *vs = NULL,*fs = NULL,*fs2 = NULL; v = glCreateShader(GL_VERTEX_SHADER); f = glCreateShader(GL_FRAGMENT_SHADER); f2 = glCreateShader(GL_FRAGMENT_SHADER); vs = textFileRead("toon.vert"); fs = textFileRead("toon.frag"); fs2 = textFileRead("toon2.frag"); const char * ff = fs; const char * ff2 = fs2; const char * vv = vs; glShaderSource(v, 1, &vv,NULL); glShaderSource(f, 1, &ff,NULL); glShaderSource(f2, 1, &ff2,NULL); free(vs);free(fs); glCompileShader(v); glCompileShader(f); glCompileShader(f2); p = glCreateProgram(); glAttachShader(p,f); glAttachShader(p,f2); glAttachShader(p,v); glLinkProgram(p); glUseProgram(p);}int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("MM 2004-05"); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutReshapeFunc(changeSize); glutKeyboardFunc(processNormalKeys); glEnable(GL_DEPTH_TEST); glClearColor(1.0,1.0,1.0,1.0);// glEnable(GL_CULL_FACE); glewInit(); if (glewIsSupported("GL_VERSION_2_0"))  printf("Ready for OpenGL 2.0\n"); else {  printf("OpenGL 2.0 not supported\n");  exit(1); } setShaders(); glutMainLoop(); // just for compatibiliy purposes return 0;} 


效果图

 

demo完整地址:

http://lighthouse3d.com/wptest/wp-content/uploads/2011/03/glutglsl_2.0.zip

 

0 0
原创粉丝点击