OpenGL(4) 着色器 - 着色器文档的读入

  • 新加项Shader.h
#pragma once
#include<string>
class Shader
{
public:
	Shader();
	Shader(const char * vertexPath, const char * fragmentPath);

	//cpu中的string 和 char*
	std::string vertexString;
	std::string fragmentString;
	const char* vertexSource;
	const char* fragmentSource;
};

  • 新加项Shader.cpp
#include "Shader.h"
#include<iostream>
#include<fstream>
#include<sstream>

using namespace std;

Shader::Shader()
{
}

Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
	
	ifstream vertexFile;
	ifstream fragmentFile;
	
	stringstream vertexSStream;
	stringstream fragmentSStream;//先是file 然后再转化为String(memory中)


	//打开文件,还没有写入内存
	vertexFile.open(vertexPath);
	fragmentFile.open(fragmentPath);
	//异常查询
	vertexFile.exceptions(ifstream::failbit || ifstream::badbit);
	fragmentFile.exceptions(ifstream::failbit || ifstream::badbit);



	try//异常检测机制
	{
		if (!vertexFile.is_open())
		{
			throw exception("open file erro");//如果检测到异常则丢出一个open file erro
		}

		vertexSStream << vertexFile.rdbuf();
		fragmentSStream << fragmentFile.rdbuf(); //把内存中的file转为string

		vertexString = vertexSStream.str();
		fragmentString = fragmentSStream.str();

		vertexSource = vertexString.c_str();
		fragmentSource = fragmentString.c_str();

		//printf(vertexSource);
		printf(fragmentSource);



	}
	catch (const std::exception& ex)
	{
		printf(ex.what());
	}
}
  • 加入vertexSource和fragmentSource txt文档

这两个文档就是我们需要读入的着色器

在这里插入图片描述
里面写入着色器的内容

vertexSource:

#version 330 core               
layout (location = 7) in vec3 aPos; 
layout (location = 8) in vec3 aColor; 
out vec4 vertexColor;    
void main(){ 
 gl_Position  = vec4(aPos.x, aPos.y, aPos.z, 1.0);   
 vertexColor = vec4(aColor.x,aColor.y,aColor.z,1.0); 
} 

fragmentSource:

#version 330 core          
in vec4 vertexColor;    
uniform vec4 ourColor; 
out vec4 FragColor;     
void main(){           
 FragColor = vertexColor;
 } 

具体的流程如下:

文件:

ifstream vertexFile;
ifstream fragmentFile;

string:

stringstream vertexSStream;
stringstream fragmentSStream;//先是file 然后再转化为String(memory中)

memory :从文件到string

vertexSStream << vertexFile.rdbuf();
fragmentSStream << fragmentFile.rdbuf();

CPU:从string(memory)到string(cpu)

vertexString = vertexSStream.str();
fragmentString = fragmentSStream.str();

CPU:从string (CPU)到 char

vertexSource = vertexString.c_str();
fragmentSource = fragmentString.c_str();

如下图所示:

在这里插入图片描述

C++

rdbuf() 可以实现一个流对象指向的内容用另一个流对象来输出,比如想把 一个文件的内容输出到显示器上,我们可以两行代码完成:

  ifstream infile("test.txt");  
  cout << infile.rdbuf(); 

c_str(): 返回const char*类型(可读不可改)的指向字符数组的指针

try catch 异常处理

课程参考:傅老师OpenGL

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Requires Unity 5.6.0 or higher. RealToon v5.1.2 An AAA Anime/Toon Shader to make your characters & objects to look as close to real Anime/Cartoon. Toon/Cel shading shader. (For Games, Film/Animations & Illustrations/Arts) - It is Fully Multi-Lighting. Features: - Smooth Object Normal: *Smooth object normal for better or clean shading. - Self Shadow: *(Adjust Size/Threshold and Hardness without using any texture) - Normal Map: *(For more details) - FReflection: *(Use images/textures as reflection.) *(Can also adjust the size and position) *(This is not a matcap style reflection or cubemap.) - Outline: *(Can change the Color and Width) *(With noise/distort outline for sketch style outline and dynamic) *(You can choose to use outline or without outline "for less drawcalls"). - Gloss (Texture) (Custom Gloss): *(Use images/textures as gloss) *(Can choose to follow light position & object rotation.) - ShadowT (Texture): *(Gradient or Flat base shadow/shade.) - RealToon Refraction: *Anime/cartoon style glass, ice, liquid, etc. - Reduce Shadow: *Reduce real-time shadow of an object - material without affecting other parts of an object. - Fade Transparency with outline and transparent affects shadow. - Vertex color support for Outline, Self Shadow & Smooth Object Normal features. - Includes RealToon (Lite Version) shaders for mobile or lite type games. - Includes RealToon (Tessellation Version) shaders. - Includes RealToon (LWRP Version) (Unity 2018 with LWRP V4.0.0 to latest) - Includes effects like sobel outline. - More... Other Features - Use Directional Light and Point & Spot light or multiple lights at the same time without problem and maintaining that anime/cartoon style shadow color & brightness. - Fully color shadow and maintain light falloff. - Fog affect Outline. - Receive GI with Flat/Smooth shade & Skylight/Environment Light. - Do baked/real-time reflection. - Can use patterned textures for manga/comics like shadow. - Use it with or without shadow. - Computer (PC,Mac & Linux), Mobile & Video Game Console (Nintendo Switch) in one shader. - You can also use this in creating Anime/Cartoon film & Illustrations/Arts in unity3d. - You can also use this in making VTuber works with or without real-time shadows. - Can now be used in 2D type games. - Includes a simple tool/script Frame By Frame Rendering for animation & illustration/art use. *Renders frames to Image Sequence or PNG Sequence file. - Includes a simple custom shadow resolution script for higher quality shadow. (For Film/Animation & Illustration/Art use) - This can also be use in VRChat/VRC. - More features to come so be sure to check my social network/media.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值