第一个三角形OpenGL 着色器
//
//
// --- LoadShaders.h ---
//
//
#ifndef __LOAD_SHADERS_H__
#define __LOAD_SHADERS_H__
#include<GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//----------------------------------------------------------------------------
//
// LoadShaders() takes an array of ShaderFile structures, each of which
// contains the type of the shader, and a pointer a C-style character
// string (i.e., a NULL-terminated array of characters) containing the
// entire shader source.
//
// The array of structures is terminated by a final Shader with the
// "type" field set to GL_NONE.
//
// LoadShaders() returns the shader program value (as returned by
// glCreateProgram()) on success, or zero on failure.
//
typedef struct {
GLenum type;
const char* filename;
GLuint shader;
} ShaderInfo;
GLuint LoadShaders(ShaderInfo*);
//----------------------------------------------------------------------------
#ifdef __cplusplus
};
#endif // __cplusplus
#endif // __LOAD_SHADERS_H__
在这里插入代码片
//
//
// --- LoadShaders.cxx ---
//
//
#include <cstdlib>
#include <iostream>
#include "loadshader.h"
/*#ifdef __cplusplus
extern "C" {
#endif // __cplusplus*/
//----------------------------------------------------------------------------
static const GLchar*ReadShader(const char* filename)
{
#ifdef WIN32
FILE* infile;
fopen_s(&infile, filename, "rb");
#else
FILE* infile = fopen(filename, "rb");
#endif // WIN32
if (!infile) {
#ifdef _DEBUG
std::cerr << "Unable to open file '" << filename << "'" << std::endl;
#endif /* DEBUG */
return NULL;
}
fseek(infile, 0, SEEK_END);
int len = ftell(infile);
fseek(infile, 0, SEEK_SET);
GLchar* source = new GLchar[len + 1];
fread(source, 1, len, infile);
fclose(infile);
source[len] = 0;
return const_cast<const GLchar*>(source);
}
GLuint LoadShaders(ShaderInfo* shaders)
{
if (shaders == NULL) { return 0; }
GLuint program = glCreateProgram();
ShaderInfo* entry = shaders;
while (entry->type != GL_NONE) {
GLuint shader = glCreateShader(entry->type);
entry->shader = shader;
const GLchar* source = ReadShader(entry->filename);
if (source == NULL) {
for (entry = shaders; entry->type != GL_NONE; ++entry) {
glDeleteShader(entry->shader);
entry->shader = 0;
}
return 0;
}
glShaderSource(shader, 1, &source, NULL);
delete[] source;
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
#ifdef _DEBUG
GLsizei len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len + 1];
glGetShaderInfoLog(shader, len, &len, log);
std::cerr << "Shader compilation failed: " << log << std::endl;
delete[] log;
#endif /* DEBUG */
return 0;
}
glAttachShader(program, shader);
++entry;
}
glLinkProgram(program);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
#ifdef _DEBUG
GLsizei len;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len + 1];
glGetProgramInfoLog(program, len, &len, log);
std::cerr << "Shader linking failed: " << log << std::endl;
delete[] log;
#endif /* DEBUG */
for (entry = shaders; entry->type != GL_NONE; ++entry) {
glDeleteShader(entry->shader);
entry->shader = 0;
}
return 0;
}
return program;
}
//----------------------------------------------------------------------------
/*#ifdef __cplusplus
}
#endif // __cplusplus*/
第一个三角形OpenGL 着色器
//
//
// --- LoadShaders.h ---
//
//
#ifndef __LOAD_SHADERS_H__
#define __LOAD_SHADERS_H__
#include<GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//----------------------------------------------------------------------------
//
// LoadShaders() takes an array of ShaderFile structures, each of which
// contains the type of the shader, and a pointer a C-style character
// string (i.e., a NULL-terminated array of characters) containing the
// entire shader source.
//
// The array of structures is terminated by a final Shader with the
// "type" field set to GL_NONE.
//
// LoadShaders() returns the shader program value (as returned by
// glCreateProgram()) on success, or zero on failure.
//
typedef struct {
GLenum type;
const char* filename;
GLuint shader;
} ShaderInfo;
GLuint LoadShaders(ShaderInfo*);
//----------------------------------------------------------------------------
#ifdef __cplusplus
};
#endif // __cplusplus
#endif // __LOAD_SHADERS_H__
**下面是main的cpp**
#include "loadshader.h"
#pragma comment (lib, "glew32.lib")
#include<iostream>
#include<stdlib.h>
using namespace std;
enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 3;
void init()
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices][2] = {
{ -0.90, -0.90 },
{ 0.85, -0.90 },
{ -0.90, 0.85 },
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangle.vert" },
{ GL_FRAGMENT_SHADER, "triangle.frag" },
{ GL_NONE, NULL }
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
#define BUFFER_OFFSET(offset) ((void *)(offset))
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(512, 512);
/*glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);*/
glutCreateWindow("三角");
glewExperimental = GL_TRUE;
if (glewInit())
{
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(1);
}
//GLenum error = glGetError();
init();
glutDisplayFunc(display);
glutMainLoop();
}
**.vert**
#version 430 core
layout(location=0) in vec4 vPosition;
void main()
{
gl_Position=vPosition;
}
**.frag**
#version 430 core
out vec4 fColor;
void main()
{
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}