LearnOpenGL notes (3) Shader

Some operations GLSL vector

vec2 someVec;

vec4 differentVec = someVec.xyxx;

vec3 anotherVec = differentVec.zyw;

vec4 otherVec = someVec.xxxx + anotherVec.yxzy;

Defines a shader class

  (This part is the lack of knowledge of c ++

class Shader
{
public:
	unsigned int ID;

	Shader(const GLchar* vertexPath,const GLchar* fragmentPath);

	void use();
	// uniform utility functions
	void setBool(const std::string &name, bool value) const;
	void setInt(const std::string& name, int value) const;
	void setFloat(const std::string& name, float value) const;
};

const keyword:

It indicates that this is a constant function, the function can not change the class member variables.

// 1 // read from the file
	std::string vertexCode;
	std::string fragmentCode;
	std::ifstream vShaderFile;
	std::ifstream fShaderFile;
	// ensure ifstream object can throw an exception
	vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	try {
		//open a file
		vShaderFile.open(vertexPath);
		fShaderFile.open(fragmentPath);
		std::stringstream vShaderStream, fShaderStream;
		// read buffer content file into the data stream
		vShaderStream << vShaderFile.rdbuf();
		fShaderStream << fShaderFile.rdbuf ();
		// close the file processor
		vertexCode = vShaderStream.str();
		fragmentCode = fShaderStream.str();
	}
	catch (std::ifstream::failure e) {
		std :: cout << "ERROR: SHADER :: file read failed" << std :: endl;
	}
	const char* vShaderCode = vertexCode.c_str();
	const char* fShaderCode = fragmentCode.c_str();

try & catch (throw) Exception Handling

// throw an exception, the type of value can be an expression of the basic types, also can be a class
throw expression;
//try…catch
	try{
		Statement group
	}
	catch (Exception Type) {
		Exception handling code
	}
	...
	catch (Exception Type) {
		Exception handling code
	}

The try block statements, if the process of implementation is not an exception is thrown, then the implementation of the latter on the implementation last catch block behind the statement, all statements in the catch block will not be executed;

If the process executed in the try block throws an exception, then immediately jump to the first "Exception Types" and catch block matches the type of exception thrown exception is thrown after the execution (the exception is referred to as a catch block of the "Capture "), after their execution jumps back to the last catch block continues.

When reading the file directory settings

 

Two routes parameters:

A relative path starting from the root directory: "Resource / pos_color_outpos.fs"

Absolute path: "F: \\ c ++ \\ MyOpenGL \\ MyOpenGL \\ Resource \\ pos_color_outpos.vs"

 

Absolute path is the path of the computer's address bar:

 

Moreover, because the \ is the escape character in the string, use \\ represents \

 Exercises

Use outkeywords to the outputs of the vertex position of the fragment shader, color settings and equal segments with vertex position (vertex positions to look at even values are interpolated in the triangle results). And after that, try to answer the following questions: Why is the lower left corner of the triangle is black

 

Rendering results

 

 

 

Since all the points z = 0, the color blue has been rgb 0 (no blue

As is apparent in the lower left corner coordinates are (0,0,0), the black

And from the color point of view, red → positive direction, and the x-axis positive direction.

↑ green, y-axis positive direction, z-axis can not see it.

(Opengl is because right-handed, in fact, is the forward direction of the positive z-axis)

 

  (Attached to the thumb finger is bent _ xy axis is the z-axis direction of the middle finger grasp _ .jpg)

 

 

Guess you like

Origin www.cnblogs.com/lxliiin/p/11815482.html