opengl对三种光源(方向光,点光源,聚光灯)进行特写并分屏渲染

分屏特写渲染效果图


实现原理

1,创建3个fbo

2,分别将方向光,点光源,聚光灯的照射效果渲染到fbo

3,在将渲染好的三个fbo作为纹理贴到要绘制的三个四边形上。

渲染入口

#include <windows.h>
#include "glew.h"
#include <stdio.h>
#include <math.h>
#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);

	RECT rect;
	rect.left = 0;
	rect.top = 0;
	rect.right = 1280;
	rect.bottom = 720;
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
	HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right-rect.left, rect.bottom-rect.top, 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);
	GetClientRect(hwnd, &rect);
	int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top;
	glewInit();
	//init gpu program
	GPUProgram fsqProgram;
	fsqProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
	fsqProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/fullscreenquad.fs");
	fsqProgram.Link();

	fsqProgram.DetectAttribute("pos");
	fsqProgram.DetectAttribute("texcoord");
	fsqProgram.DetectUniform("U_MainTexture");

	GPUProgram gpuProgram;
	gpuProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/Light.vs");
	gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/Light.fs");
	gpuProgram.Link();

	gpuProgram.DetectAttribute("pos");
	gpuProgram.DetectAttribute("texcoord");
	gpuProgram.DetectAttribute("normal");
	gpuProgram.DetectUniform("M");
	gpuProgram.DetectUniform("V");
	gpuProgram.DetectUniform("P");
	gpuProgram.DetectUniform("NM");
	gpuProgram.DetectUniform("U_AmbientLightColor");
	gpuProgram.DetectUniform("U_AmbientMaterial");
	gpuProgram.DetectUniform("U_DiffuseLightColor");
	gpuProgram.DetectUniform("U_DiffuseMaterial");
	gpuProgram.DetectUniform("U_SpecularLightColor");
	gpuProgram.DetectUniform("U_SpecularMaterial");
	gpuProgram.DetectUniform("U_LightPos");
	gpuProgram.DetectUniform("U_EyePos");
	gpuProgram.DetectUniform("U_LightDirection");
	gpuProgram.DetectUniform("U_Cutoff");
	gpuProgram.DetectUniform("U_DiffuseIntensity");
	//init 3d model
	ObjModel cube;
	cube.Init("Debug/res/model/Cube.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.2f,0.2f,0.2f,1.0f };//环境光材质
	float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };//漫反射光颜色
	float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f };//漫反射光材质
	float diffuseIntensity = 1.0f;//漫反射光强度
	float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };//镜面光颜色
	float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };//镜面光材质
	float lightPos[] = { 0.0f,1.5f,0.0f,0.0f };//光源位置
	float spotLightDirection[] = { 0.0f,-1.0f,0.0f,128.0f };//聚光灯中心线向量
	float spotLightCutoff = 0.0f;//聚光灯中心线向量和入射光线最大向量
	float eyePos[] = { 0.0f,0.0f,0.0f };//研究的位置

	//每个渲染模型的Mode矩阵
	glm::mat4 model1=glm::translate<float>(-2.0f, 0.0f, -6.0f)*glm::rotate(-30.0f,1.0f,1.0f,1.0f);
	glm::mat4 model2 = glm::translate<float>(2.0f, 0.0f, -6.0f)*glm::rotate(-30.0f,1.0f,1.0f,1.0f);
	glm::mat4 model3 = glm::translate<float>(6.0f, 0.0f, -6.0f)*glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
	//投影矩阵
	glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
	//三个摄像机矩阵
	glm::mat4 viewMatrix1 = glm::lookAt(glm::vec3(-0.5f, 1.5f, -3.0f), glm::vec3(-2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
	glm::mat4 viewMatrix2 = glm::lookAt(glm::vec3(3.0f, 1.5f, -3.0f), glm::vec3(2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
	glm::mat4 viewMatrix3 = glm::lookAt(glm::vec3(7.0f, 1.5f, -3.0f), glm::vec3(6.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
	
	//法线矩阵
	glm::mat4 normalMatrix1 = glm::inverseTranspose(model1);
	glm::mat4 normalMatrix2 = glm::inverseTranspose(model2);
	glm::mat4 normalMatrix3 = glm::inverseTranspose(model3);

	FullScreenQuad fsq;
	fsq.Init();
	FBO fboDirectionLight;
	fboDirectionLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
	fboDirectionLight.AttachDepthBuffer("depth",viewportWidth,viewportHeight);
	fboDirectionLight.Finish();

	FBO fboPointLight;
	fboPointLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
	fboPointLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
	fboPointLight.Finish();

	FBO fboSpotLight;
	fboSpotLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
	fboSpotLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
	fboSpotLight.Finish();

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

	glClearColor(0.0f,0.0f,0.0f,1.0f);
	glEnable(GL_DEPTH_TEST);
	MSG msg;
	while (true)
	{
		if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		//方向光的绘制绑定到fbo
		fboDirectionLight.Bind();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glUseProgram(gpuProgram.mProgram);
		//给shader中变量赋值
		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);
		glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
		glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
		glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
		glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
		glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
		glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
		glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
		//指定方向光光源位置
		lightPos[0] = 0.0f;
		lightPos[1] = 1.5f;
		lightPos[2] = 0.0f;
		lightPos[3] = 0.0f;
		//漫反射光强度
		diffuseIntensity = 1.0f;
		//聚光灯中心线和入射光线最大夹角
		spotLightCutoff = 0.0f;
		glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
		glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
		glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
		glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model1));
		glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix1));
		glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix1));
		//绘制方向光光源照射的立方体
		cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
		cube.Draw();
		fboDirectionLight.Unbind();//解除fbo的绑定
		
		//点光源的绘制绑定到fbo
		fboPointLight.Bind();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		//指定点光源的位置
		lightPos[0] = 0.0f;
		lightPos[1] = 3.0f;
		lightPos[2] = -6.0f;
		lightPos[3] = 1.0f;
		diffuseIntensity = 3.0f;
		//设置shader中变量的值
		glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
		glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
		glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model2));
		glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix2));
		glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix2));
		//绘制点光源照射的立方体
		cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
		cube.Draw();
		fboPointLight.Unbind();

		//聚光灯的绘制绑定到fbo
		fboSpotLight.Bind();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		//聚光灯光源位置
		lightPos[0] = 6.3f;
		lightPos[1] = 3.0f;
		lightPos[2] = -5.8f;
		lightPos[3] = 1.0f;
		diffuseIntensity = 4.0f;
		spotLightCutoff = 15.0f;
		//设置shader中变量的值
		glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
		glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
		glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
		glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model3));
		glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix3));
		glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix3));
		//绘制被聚光灯照射的立方体
		cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
		cube.Draw();
		fboSpotLight.Unbind();
		glUseProgram(0);
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);//重要,不能省略
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		//切换shader,分别将三个fbo中的内容作为纹理,贴到待绘制的三个长方形上。
		//将方向光fbo中的内容作为纹理贴图到左上角矩形
		glUseProgram(fsqProgram.mProgram);
		glActiveTexture(GL_TEXTURE0);//**0号纹理
		glBindTexture(GL_TEXTURE_2D,fboDirectionLight.GetBuffer("color"));
		glUniform1i(fsqProgram.GetLocation("U_MainTexture"),0);//指定0号纹理
		fsq.DrawToLeftTop(fsqProgram.GetLocation("pos"),fsqProgram.GetLocation("texcoord"));


		//将点光源fbo中的内容作为纹理贴图到右上角
		glBindTexture(GL_TEXTURE_2D, fboPointLight.GetBuffer("color"));
		glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0);
		fsq.DrawToRightTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));

		//聚光灯光源fbo中的内容作为纹理贴图到左下角矩形
		glBindTexture(GL_TEXTURE_2D,fboSpotLight.GetBuffer("color"));
		glUniform1i(fsqProgram.GetLocation("U_MainTexture"),0);//使用0号纹理
		fsq.DrawToLeftBottom(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));

	
		glUseProgram(0);
		glFlush();
		SwapBuffers(dc);
	}

	return 0;
}
如何绘制屏幕某个指定方位的四边形

void FullScreenQuad::DrawToLeftTop(GLint posLoc, GLint texcoordLoc)
{
	//draw to left top qualter of screen
	float vertices[] = {
		-0.5f,0.0f,-1.0f,0.0f,0.0f,
		0.0f,0.0f,-1.0f,1.0f,0.0f,
		0.0f,0.5f,-1.0f,1.0f,1.0f,
		-0.5f,0.5f,-1.0f,0.0f,1.0f
	};
	glBindBuffer(GL_ARRAY_BUFFER, mVBO);
	//将顶点数据从内存上传至显卡
	glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, vertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glBindBuffer(GL_ARRAY_BUFFER, mVBO);//绑定vbo
	glEnableVertexAttribArray(posLoc);//启用顶点坐标属性
	//指定顶点坐标在VBO中的起始地址
	glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
	//启用顶点纹理属性
	glEnableVertexAttribArray(texcoordLoc);
	//指定纹理坐标在vbo中的起始地址
	glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
	glDrawArrays(GL_QUADS, 0, 4);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
}
重点在顶点shader(opengl默认的笛卡尔坐标系的取值范围-1到1)

attribute vec3 pos;
attribute vec2 texcoord;

varying vec2 V_Texcoord;
void main()
{
	vec4 worldPos=vec4(pos,1.0);
	V_Texcoord=texcoord;
	worldPos.x*=2.0;
	worldPos.y*=2.0;
	gl_Position=worldPos;
}




版权声明:本文为hb707934728原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hb707934728/article/details/77473679

智能推荐

Ubuntu 14.04 下,安装 Java8

下载Java http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 打开上述链接, , 下载 特定的 Java8 版本。笔者选择的是 jdk-8u172-linux-x64.tar.gz。 - 配置Java环境变量 将 jdk-8u172-linux-x64.tar.gz 解压至指定目...

OpenCV学习之路(五)图像的几何变换

在这一章将要学习图像的移动、旋转,仿射变换等 扩展缩放 我们如果想要改变图像的大小,我们就需要对图像进行扩展缩放,opencv提供给我们控制扩展缩放的函数: 参数解释: src:进行扩展缩放的原图片 dst:可以在此处设置缩放因子,也可手动设置尺寸 interpolation:在缩放时我们推荐使用cv2.INTER_AREA, 在扩展时我们推荐使用cv2.INTER_CUBIC(慢) 和 cv2....

2018.8.27

2018.8.27...

HTML 表单元素的基本样式

HTML 表单元素的基本样式 原创 ixygj197875 发布于2018-02-22 17:48:53 阅读数 2296 收藏 更新于2018-05-20 15:35:58 分类专栏: 揭秘 CSS 揭秘 CSS 收起 表单元素主要包括 label、input、textarea、select、datalist、******、progress、meter、output等,以及对表单元素进行分组的 ...

php输出语句

php输出语句 常见的输出语句 echo(): 可以一次输出多个值,多个值之间用逗号分隔。echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用。 print(): 函数print()打印一个值(它的参数),如果字符串成功显示则返回true,否则返回false。 print_r(): 可以把字符串和数字简单地打印出来,而数组则以括起来的键和值...

猜你喜欢

工厂模式

简介 常见的实例化对象模式。 用工厂方法替代new操作的一种模式。 当我们使用new操作实例化对象时,调用构造函数完成初始化。若初始化仅是进行赋值等简单的操作,写入构造函数即可。但如果初始化时需要执行一长串复杂的代码,将多个工作装入一个方法,是不妥的。 创建实例与使用实例分离。将创建实例所需的大量初始化工作从基类的构造函数中分离出去。 简单工厂模式、工厂方法模式针对的是一个产品等级结构;而抽象工厂...

B1105 Spiral Matrix (画图)

B1105 Spiral Matrix (25分) //第一次只拿了21分 矩阵的长和宽,求最大因子,从sqrt(num)开始枚举. 每次循环一次,s++,t--,d--,r++ 测试点四运行超时,是因为输入一个数字的时候,需要直接输出这个数字。//1分 测试点二运行超时,最后一个数字不必再while循环一次,直接输出即可。//3分 最后一个测试点卡了好久/(ㄒoㄒ)/~~ 螺旋矩阵...

Java基础=>String,StringBuffer与StringBuilder的区别

字符串常量池 什么是字符串常量池? JVM为了减少字符串对象的重复创建,其维护了一块特殊的内存,这段内存被称为字符串常量池(存储在方法区中)。 具体实现 当代码中出现字符串时,JVM首先会对其进行检查。 如果字符串常量池中存在相同内容的字符串对象,如果有,则不再创建,直接返回这个对象的地址返回。 如果字符串常量池中不存在相同内容的字符串对象,则创建一个新的字符串对象并放入常量池,并返回新创建的字符...

java调用其他java项目的Https接口

项目中是这样的: 用户拿出二维码展示,让机器识别二维码, 机器调用开门的后台系统接口, 然后开门的后台系统接口需要调用管理系统的接口, 管理系统需要判断能不能开门.这两个系统是互相独立的.当时使用http调用是没有问题的.当时后来要求必须用https.废话不说,直接代码: 我的项目中调用的是 HttpsUtils.Get(utlStr) 这个接口 开门系统接口如下图:   管理系统的接口...

Hadoop1.2.1全分布式模式配置

一 集群规划 主机名            IP                               安装的软件 &nbs...