textfile.h的内容:
char *textFileRead(char *fn);
textfile.cpp的内容:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *textFileRead(char *fn)
{
FILE *fp;
char *content = NULL;
int count = 0;
if (fn != NULL)
{
fp = fopen(fn, "rt");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
count =ftell(fp);
rewind(fp);
if (count > 0)
{
content = (char *)malloc(sizeof(char)*(count + 1));
count = fread(content, sizeof(char), count, fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
minimal.vert内容:
void main()
{
// gl_Position=gl_ProjectionMatrix*gl_ModelViewMatrix*gl_Vertex;
// gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;
gl_Position=ftransform();//经过显卡优化
}
#include<stdio.h>
#include<stdlib.h>
#include<GL/glew.h>
#include<GL/glut.h>
#include "textfile.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#pragma comment(lib,"glew32.lib")
GLuint v,f, p;//顶点着色器,片源着色器,着色器程序
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 printProgramInfoLog(GLuint obj)
{
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength);
if (infologLength > 0)
{
infoLog = (char*)malloc(infologLength);
glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n", infoLog);
free(infoLog);
}
}
float a = 0;
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, -1.0,
0.0, 1.0, 0.0);
glRotatef(a, 0, 1, 1);
glColor3f(1.0, 0.0, 0.0);
glutSolidTeapot(1);
a += 0.1;
//glFlush();//刷新缓存
glutSwapBuffers();
}
void changeSize(int w,int h)
{
if (h == 0)
h = 1;
float ratio = 1.0*w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keys(unsigned char key, int x, int y)
{
if (key == 27)//Esc
exit (0);
}
void setShaders()
{
//创建着色器
f = glCreateShader(GL_FRAGMENT_SHADER);
v = glCreateShader(GL_VERTEX_SHADER);
char *fs,*vs;
fs = textFileRead("minimal.frag");
vs = textFileRead("minimal.vert");
const char *ff = fs;
const char *vv = vs;
glShaderSource(f, 1, &ff, NULL);
glShaderSource(v, 1, &vv, NULL);
free(fs);
free(vs);
glCompileShader(f);
glCompileShader(v);
printShaderInfoLog(v);
p = glCreateProgram();
glAttachShader(p, f);
glAttachShader(p, v);
glLinkProgram(p);
printProgramInfoLog(p);
glUseProgram(p);
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("顶点着色器");
glutDisplayFunc(RenderScene);
glutIdleFunc(RenderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
GLenum err=glewInit();
if (GLEW_OK != err)
{
printf("Error:%s\n", glewGetErrorString(err));
exit(1);
}
setShaders();
glutMainLoop();
return 0;
}