GLSL 使用demo

/*


  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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值