找了一段时间的资料才整出来的简单的可以跑的通的,不需要太多头文件的代码,可算调通了,为了节省大家的时间,特意贴出代码,有不正确的直接留言,
配置说明(不说配置环境的代码都是流氓小说):
包含:GLEW库,SOIL库,GL库(最好freeglut) 还有你的环境
链接:glew32.lib SOIL.lib opengl32.lib freeglut.lib
啥都不说了,直接上干货:
#include
#include
#include
#include
#include
#include
#include
// settings
const int SCR_WIDTH = 640, SCR_HEIGHT = 480;
int width, height;
GLuint base_prog;
GLuint vao;
GLuint quad_vbo;
GLuint tex;
unsigned char* image;
//----------------------------------------------------------------------------
void glAttachShaderSource(GLuint prog, GLenum type, const char * source)
{
GLuint sh;
sh = glCreateShader(type);
glShaderSource(sh, 1, &source, NULL);
glCompileShader(sh);
char buffer[4096];
glGetShaderInfoLog(sh, sizeof(buffer), NULL, buffer);
glAttachShader(prog, sh);
glDeleteShader(sh);
}
//----------------------------------------------------------------------------
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEBUG_OUTPUT);
base_prog = glCreateProgram();
static const char quad_shader_vs[] =
"#version 330 core\n"
"\n"
"layout (location = 0) in vec2 in_position;\n"
"layout (location = 1) in vec2 in_tex_coord;\n"
"\n"
"out vec2 tex_coord;\n"
"\n"
"void main(void)\n"
"{\n"
" gl_Position = vec4(in_position, 0.0, 1.0);\n"
" tex_coord = in_tex_coord;\n"
"}\n"
;
static const char quad_shader_fs[] =
"#version 330 core\n"
"\n"
"in vec2 tex_coord;\n"
"\n"
"layout (location = 0) out vec4 color;\n"
"\n"
"uniform sampler2D tex;\n"
"\n"
"void main(void)\n"
"{\n"
" color = texture(tex,tex_coord);\n"
"}\n"
;
glAttachShaderSource(base_prog, GL_VERTEX_SHADER, quad_shader_vs);
glAttachShaderSource(base_prog, GL_FRAGMENT_SHADER, quad_shader_fs);
glLinkProgram(base_prog);
static const GLfloat quad_data[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &quad_vbo);
glBindBuffer(GL_ARRAY_BUFFER, quad_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_data), quad_data, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)(8 * sizeof(float)));
glEnableVertexAttribArray(1);
char buf[1024];
glGetProgramInfoLog(base_prog, 1024, NULL, buf);
// texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//如果你想加载视频帧,或图片序列,你需要将以下的glTexImage2D()代码放到display()函数中,并且加上计时函数以便更新画面,如果从摄像机读的帧数据直接贴也是可以的
image = SOIL_load_image("test/campus.jpg", &width, &height, 0, SOIL_LOAD_RGB);
if (image)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
}
else
{
printf( "Failed to load texture");
}
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);
}
//----------------------------------------------------------------------------
void display(void)
{
glClearColor(0.0, 255, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao);
glUseProgram(base_prog);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(base_prog, "tex"), 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glutSwapBuffers();
}
//----------------------------------------------------------------------------
void OnShutdown()
{
glUseProgram(0);
glDeleteProgram(base_prog);
glDeleteTextures(1, &tex);
glDeleteVertexArrays(1, &tex);
//销毁vbo 和 vao
glDeleteBuffers(1, &quad_vbo);
glDeleteVertexArrays(1, &vao);
printf("Shutdown successfull" );
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowPosition(100, 100);
glutInitWindowSize(SCR_WIDTH, SCR_HEIGHT);
glutCreateWindow("GL_MultiThread_Vido");
printf("Version: %s\n", glGetString(GL_VERSION));
if (glewInit() != GLEW_OK)
{
printf( "Failed to initialize GLEW ... exiting" );
exit(EXIT_FAILURE);
}
Init();
glutDisplayFunc(display);
glutCloseFunc(OnShutdown);
glutMainLoop();
return 0;
}
后面还有进阶篇:OpenGL显示深度相机的RGBD视频