LearnOpenGL U1
cmake_minimum_required(VERSION 3.16)
project(LearnOpenGL)
set(CMAKE_CXX_STANDARD 14)
add_executable(LearnOpenGL main.cpp)
set(TARGET_LIB
"/usr/lib/x86_64-linux-gnu/libGL.so"
"/usr/lib/x86_64-linux-gnu/libGLU.so"
"/usr/lib/x86_64-linux-gnu/libglut.so"
)
target_link_libraries(LearnOpenGL ${TARGET_LIB})
#include <GL/glut.h>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glFlush();
}
//int main(int argc, char *argv[]){
// glutInit(&argc, argv);
// glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
// glutInitWindowPosition(100, 100);
// glutInitWindowSize(400, 400);
// glutCreateWindow("window name");
// glutDisplayFunc(&myDisplay);
// glutMainLoop();
// return 0;
//}
int main(int argc,char *argv[])
{
// 传递命令行参数,并对GLUT函数库进行初始化
glutInit(&argc, argv);
// 设置创建窗口时的显示模式(双缓冲区、RGB颜色模式)
glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGBA);
// 定位左上角
glutInitWindowPosition(100,100);
// 设置窗口的初始大小
glutInitWindowSize(400,400);
// 创建窗口
glutCreateWindow("Bounce");
// 显示回调函数
glutDisplayFunc(&myDisplay);
// 启动GLUT框架的运行,一经调用便不再返回,直到程序终止
glutMainLoop();
return 0;
}
LearnOpenGL U2
GLSL 详解(基础篇) · Colin's Nest GLSL 详解(基础篇)https://colin1994.github.io/2017/11/11/OpenGLES-Lesson04/
Shader
顶点着色器
#version 330
layout (location = 1) in vec3 Position;
uniform float gScale;
// varying
out vec3 color;
void main()
{
gl_Position = vec4(Position.x*gScale, Position.y*gScale, Position.z , 1.0);
color = vec3(Position.x*gScale,Position.y*gScale,Position.z);
}
片元着色器
#version 330
out vec4 FragColor;
uniform float gScale;
in vec3 color;
void main()
{
FragColor = vec4(color.x, color.y , color.z , 0.1);
}
main.cpp
#include "../CommonLib/common.h"
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
using namespace std;
#include "../CommonLib/Utils.h"
#pragma comment(lib, "CommonLib.lib")
#include "../CommonLib/math3d.h"
#include <assert.h>
const char* pVS = "shader.vert";
const char* pFS = "shader.frag";
GLuint VBO;
GLuint gScaleLocation;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
static float Scale = 0.0f;
Scale += 0.001;
glUniform1f(gScaleLocation, sinf(Scale));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 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 (ShaderObj == 0)
{
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;
GLuint ShaderProgram = glCreateProgram();
if (0 == ShaderProgram)
{
ErrorOut();
exit(1);
}
AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER);
AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER);
glLinkProgram(ShaderProgram);
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if (Success == 0) {
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
exit(1);
}
gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale");
assert(gScaleLocation != 0xFFFFFFFF);
glValidateProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
if (Success == 0) {
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_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 02");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
CompileShaders();
CreateVertexBuffer();
glutMainLoop();
return 0;
}
common.h
#pragma once
#pragma comment(lib,"freeglut.lib")
#pragma comment(lib,"glew32.lib")
math3d.h
#pragma once
class Vector3f
{
public:
float x, y, z;
Vector3f(){}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};