opengl光照之环境光

效果:

opengl光照之环境光_第1张图片

shader实现

顶点shader

attribute vec3 pos;//顶点坐标
attribute vec2 texcoord;//纹理坐标
attribute vec3 normal;//法线坐标

uniform mat4 M;//模型矩阵
uniform mat4 P;//投影矩阵
uniform mat4 V;//摄像机观察矩阵

void main()
{
	gl_Position=P*V*M*vec4(pos,1.0);//模型中的顶点经过变换后的坐标
}
片段shader

uniform vec4 U_AmbientLightColor;//环境光颜色
uniform vec4 U_AmbientMaterial;////环境光材质

void main()
{
	vec4 ambientColor=U_AmbientLightColor+U_AmbientMaterial;//将颜色和材质相乘,得到最终光照颜色
	gl_FragColor=ambientColor;//每像素颜色
}

渲染流程

#include 
#include "glew.h"
#include 
#include 
#include "utils.h"
#include "GPUProgram.h"
#include "ObjModel.h"
#include "FBO.h"
#include "FullScreenQuad.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glew32.lib")

LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}


INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
	WNDCLASSEX wndClass;
	wndClass.cbClsExtra = 0;
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.cbWndExtra = 0;
	wndClass.hbrBackground = NULL;
	wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
	wndClass.hIcon = NULL;
	wndClass.hIconSm = NULL;
	wndClass.hInstance = hInstance;
	wndClass.lpfnWndProc=GLWindowProc;
	wndClass.lpszClassName = L"OpenGL";
	wndClass.lpszMenuName = NULL;
	wndClass.style = CS_VREDRAW | CS_HREDRAW;
	ATOM atom = RegisterClassEx(&wndClass);

	HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
	HDC dc = GetDC(hwnd);
	PIXELFORMATDESCRIPTOR pfd;
	memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
	pfd.iLayerType = PFD_MAIN_PLANE;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cDepthBits = 24;
	pfd.cStencilBits = 8;

	int pixelFormatID = ChoosePixelFormat(dc, &pfd);

	SetPixelFormat(dc,pixelFormatID,&pfd);

	HGLRC rc = wglCreateContext(dc);
	wglMakeCurrent(dc, rc);

	glewInit();
	//加载并编译shader
	GPUProgram gpuProgram;
	gpuProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/ambient.vs");
	gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/ambient.fs");
	gpuProgram.Link();

	//获取shader中对应的变量名的引用id,并将变量名,引用id作为键值对 放入map
	gpuProgram.DetectAttribute("pos");
	gpuProgram.DetectAttribute("texcoord");
	gpuProgram.DetectAttribute("normal");
	gpuProgram.DetectUniform("M");
	gpuProgram.DetectUniform("V");
	gpuProgram.DetectUniform("P");
	gpuProgram.DetectUniform("U_AmbientLightColor");
	gpuProgram.DetectUniform("U_AmbientMaterial");
	//初始化并加载解析3d模型
	ObjModel model;
	model.Init("Debug/res/model/Sphere.obj");

	float identity[] = {
		1.0f,0,0,0,
		0,1.0f,0,0,
		0,0,1.0f,0,
		0,0,0,1.0f
	};
	float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f};//环境光颜色
	float ambientMaterial[] = {0.4f,0.4f,0.4f,1.0f};//环境光材质
	//model矩阵
	glm::mat4 modelMatrix = glm::translate(0.0f,0.0f,-5.0f);
	//投影矩阵  
	glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);

	glClearColor(41.0f/255.0f,  71.0f/255.0f, 121.0f / 255.0f, 1.0f);
	ShowWindow(hwnd, SW_SHOW);
	UpdateWindow(hwnd);

	MSG msg;
	while (true)
	{
		if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
		{
			if (msg.message==WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		glEnable(GL_DEPTH_TEST);
		
		//设置要使用的  program
		glUseProgram(gpuProgram.mProgram);
		
		//给gpu中变量设置值
		glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1,GL_FALSE, glm::value_ptr(modelMatrix));
		glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
		glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
		glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1,ambientLightColor);
		glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);

		model.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
		model.Draw();
		glUseProgram(0);
		glFinish();
		SwapBuffers(dc);
	}
	return 0;
}



你可能感兴趣的:(opengl_shader专题)