main.cpp
#include <stdio.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include "math3d.h"
#include <assert.h>
const char* pVS = "shader.vert";
const char* pFS = "shader.frag";
GLuint VBO;
GLuint gScaleLocation;
GLuint GWorldLocation;
//渲染
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);//渲染回调函数
static float Scale = 0.0f;
Scale += 0.01f;
//glUniform1f(gScaleLocation, sinf(Scale));
//以列模式存储和c++行存储有些区别
Matrix4f World;
//缩放
World.mat[0][0] = sinf(Scale); World.mat[0][1] = 0.0f; World.mat[0][2] = 0.0f; World.mat[0][3] = 0.0f;
World.mat[1][0] = 0.0f; World.mat[1][1] = sinf(Scale); World.mat[1][2] = 0.0f; World.mat[1][3] = 0.0f;
World.mat[2][0] = 0.0f; World.mat[2][1] = 0.0f; World.mat[2][2] = sinf(Scale); World.mat[2][3] = 0.0f;
World.mat[3][0] = 0.0f; World.mat[3][1] = 0.0f; World.mat[3][2] = 0.0f; World.mat[3][3] = 1.0f;
/*
平移
World.mat[0][0] = 1.0f; World.mat[0][1] = 0.0f; World.mat[0][2] = 0.0f; World.mat[0][3] = sinf(Scale);
World.mat[1][0] = 0.0f; World.mat[1][1] = 1.0f; World.mat[1][2] = 0.0f; World.mat[1][3] = 0.0f;
World.mat[2][0] = 0.0f; World.mat[2][1] = 0.0f; World.mat[2][2] = 1.0f; World.mat[2][3] = 0.0f;
World.mat[3][0] = 0.0f; World.mat[3][1] = 0.0f; World.mat[3][2] = 0.0f; World.mat[3][3] = 1.0f;
*/
/绕z轴旋转
/*
cos(a) -sin(a) 0 0
sin(a) cos(a) 0 0
0 0 1 0
0 0 0 1
*/
//World.mat[0][0] = cosf(Scale); World.mat[0][1] = -sinf(Scale); World.mat[0][2] = 0.0f; World.mat[0][3] = 0.0f;
//World.mat[1][0] = sinf(Scale); World.mat[1][1] = cosf(Scale); World.mat[1][2] = 0.0f; World.mat[1][3] = 0.0f;
//World.mat[2][0] = 0.0f; World.mat[2][1] = 0.0f; World.mat[2][2] = 1.0f; World.mat[2][3] = 0.0f;
//World.mat[3][0] = 0.0f; World.mat[3][1] = 0.0f; World.mat[3][2] = 0.0f; World.mat[3][3] = 1.0f;
/绕y轴旋转
/*
cos(a) -sin(a) 0 0
0 1 0 0
sin(a) cos(a) 0 0
0 0 0 1
*/
//World.mat[0][0] = cosf(Scale); World.mat[0][1] = 0.0f; World.mat[0][2] = -sinf(Scale); World.mat[0][3] = 0.0f;
//World.mat[1][0] = 0.0f; World.mat[1][1] =1.0f ; World.mat[1][2] = 0.0f; World.mat[1][3] = 0.0f;
//World.mat[2][0] = sinf(Scale); World.mat[2][1] = 0.0f; World.mat[2][2] = cosf(Scale); World.mat[2][3] = 0.0f;
//World.mat[3][0] = 0.0f; World.mat[3][1] = 0.0f; World.mat[3][2] = 0.0f; World.mat[3][3] = 1.0f;
//绕x轴旋转
/*
1 0 0 0
0 cos(a) -sin(a) 0
0 sin(a) cos(a) 0
0 0 0 1
*/
/*World.mat[0][0] = 1.0f; World.mat[0][1] = 0.0f; World.mat[0][2] = 0.0f; World.mat[0][3] = 0.0f;
World.mat[1][0] = 0.0f; World.mat[1][1] = cosf(Scale); World.mat[1][2] = -sinf(Scale); World.mat[1][3] = 0.0f;
World.mat[2][0] = 0.0f; World.mat[2][1] = sinf(Scale); World.mat[2][2] = cosf(Scale); World.mat[2][3] = 0.0f;
World.mat[3][0] = 0.0f; World.mat[3][1] = 0.0f; World.mat[3][2] = 0.0f; World.mat[3][3] = 1.0f;
*/
glUniformMatrix4fv(GWorldLocation, 1, GL_TRUE, &World.mat[0][0]);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,VBO);//VBO渲染
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,3*4,0);//数据格式解析
glDrawArrays(GL_TRIANGLES, 0, 3);//渲染
glDisableVertexAttribArray(1);
glutSwapBuffers();
}
static void CreateVertexbuffer()
{
Vector3f vertices[3];
vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
glGenBuffers(1,&VBO);//缓冲区
glBindBuffer(GL_ARRAY_BUFFER, VBO);//绑定缓冲区
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//将顶点数据拷贝到缓存对象中
}
static void AddShader(GLuint ShaderProgram, const char* shaderText, GLenum shaderType)
{
//创建着色器对象
GLuint shaderObj = glCreateShader(shaderType);
if (0 == shaderObj)
{
ErrorOut();
exit(1);
}
const GLchar* p[1];
p[0] = shaderText;
GLint L[1];
L[0] = strlen(shaderText);
//将字符数组绑定到着色器对象
glShaderSource(shaderObj, 1, p, L);
//编译着色器对象
glCompileShader(shaderObj);
GLint success;
glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(shaderObj, 1024, NULL, InfoLog);//打印错误内容
fprintf(stderr, "Error compiling shader type %d: '%s'\n", shaderType, InfoLog);
exit(1);
}
//将着色器对象绑定程序对象
glAttachShader(ShaderProgram, shaderObj);
}
void CompileShaders()
{
string vs, fs;
if (!ReadFile(pVS, vs))
{
exit(1);
}
if (!ReadFile(pFS, fs))
{
exit(1);
}
/*cout << vs << endl;
cout << fs << endl;*/
//Creates a program object
GLuint ShaderProgram = glCreateProgram();
if (0 == ShaderProgram)
{
ErrorOut();
exit(1);
}
//创建加载shader shader与program绑定
AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER);//顶点shader
AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER);//颜色shader
// Links a program object
glLinkProgram(ShaderProgram);
//验证是否有错误代码
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
//检查连接是否成功
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if (0 == Success)
{
//获取编译错误
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program:%s\n", ErrorLog);
exit(1);
}
//
//gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale");
//assert(gScaleLocation != 0xFFFFFFFF);
GWorldLocation = glGetUniformLocation(ShaderProgram, "gWorld");
assert(GWorldLocation != 0xFFFFFFFF);
//检查项目当前的执行状态
glValidateProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
if (0 == Success)
{
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr,"Error linking shader program: %s \n",ErrorLog);
exit(1);
}
//设置项目为实际的渲染目标
glUseProgram(ShaderProgram);
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
glutIdleFunc(RenderSceneCB);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("test");
InitializeGlutCallbacks();
GLenum res = glewInit();
if (GLEW_OK != res)
{
fprintf(stderr, "Error:%s", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 1.0f, 0.0f,0.0f);
CompileShaders();
CreateVertexbuffer();
glutMainLoop();
return 0;
}
//#include<Windows.h>
//#include <stdio.h>
//#include <GL/glut.h>
//int main(int argc, char** argv)
//{
// glutInit(&argc, argv);
// //显示模式初始化
// glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
// //定义窗口大小
// glutInitWindowSize(300, 300);
// //定义窗口位置
// glutInitWindowPosition(100, 100);
// //创建窗口
// glutCreateWindow("OpenGL Version");
// const GLubyte* name = glGetString(GL_VENDOR); //返回负责当前OpenGL实现厂商的名字
// const GLubyte* biaoshifu = glGetString(GL_RENDERER); //返回一个渲染器标识符,通常是个硬件平台
// const GLubyte* OpenGLVersion = glGetString(GL_VERSION); //返回当前OpenGL实现的版本号
// const GLubyte* gluVersion = gluGetString(GLU_VERSION); //返回当前GLU工具库版本
// printf("OpenGL实现厂商的名字:%s\n", name);
// printf("渲染器标识符:%s\n", biaoshifu);
// printf("OOpenGL实现的版本号:%s\n", OpenGLVersion);
// printf("OGLU工具库版本:%s\n", gluVersion);
//
// return 0;
//}
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class Vector3f
{
public:
float x, y, z;
Vector3f(){};
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};
class Matrix4f
{
public:
float mat[4][4];
};
#define ErrorOut() errorout(__FILE__,__LINE__)
void errorout(string file, unsigned int linenum)
{
cout << file << ":" << linenum << endl;
}
bool ReadFile(const char* pFileName, string& outFile)
{
ifstream f(pFileName);
bool ret = false;
if (f.is_open())
{
string line;
while (getline(f, line))
{
outFile.append(line);
outFile.append("\n");
}
f.close();
ret = true;
}
else
{
ErrorOut();
}
return ret;
}
shader.vert
#version 330
layout (location = 1) in vec3 Position;
uniform float gScale;
uniform mat4 gWorld;
//shader之间传递变量
out vec3 color;
void main()
{
gl_Position = gWorld * vec4(Position,1.0);
//gl_Position = vec4(Position.x*0.5,Position.y*0.5,Position.z*0.5,1.0);
//gl_Position = vec4(Position.x*gScale,Position.y*gScale,Position.z*gScale,1.0);
color = vec3(Position * gScale);
}
shader.frag
#version 330
out vec4 FragColor;
uniform float gScale;
//shader之间传递变量
in vec3 color;
void main()
{
//FragColor = vec4(color.x,color.y,color.z,0.1);
FragColor = vec4(1.0,0.0,0.0,0.1);
}