OpenGL笔记十三之Uniform向量数据传输、使用glUniform3f和glUniform3fv

OpenGL笔记十三之Uniform向量数据传输、使用glUniform3f和glUniform3fv

—— 2024-07-14 晚上

bilibili赵新政老师的教程看后笔记

code review!

1.glUniform3f

1.1.运行

在这里插入图片描述

1.2.vs

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 color;

void main()
{
   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
   color = aColor;
}

1.3.fs

#version 330 core
out vec4 FragColor;

uniform vec3 uColor;

void main()
{
   FragColor = vec4(uColor, 1.0f);
}

1.4.shader.h关键部分

void setVector3(const std::string& name, float x, float y, float z);

1.5.shader.cpp关键部分

void Shader::setVector3(const std::string& name, float x, float y, float z){
    // 1 通过名称拿到Uniform变量的位置Location
    GLint location = GL_CALL(glGetUniformLocation(mProgram, name.c_str()));

    // 2 通过Location更新Uniform变量的值
    GL_CALL(glUniform3f(location, x, y, z));
}

1.6.main.cpp关键部分

void render() {
	//执行opengl画布清理操作
	GL_CALL(glClear(GL_COLOR_BUFFER_BIT));

	//1 绑定当前的program
	shader->begin();

	shader->setVector3("uColor",0.3, 0.4, 0.5);
	
	//2 绑定当前的vao
	GL_CALL(glBindVertexArray(vao));
	//3 发出绘制指令
	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
	glBindVertexArray(0);
	shader->end();
}

int main() {
	if (!app->init(800, 600)) {
		return -1;
	}

	app->setResizeCallback(OnResize);
	app->setKeyBoardCallback(OnKey);

	//设置opengl视口以及清理颜色
	GL_CALL(glViewport(0, 0, 800, 600));
	GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));

	prepareShader();
	prepareVAO();
	while (app->update()) {
		render();
	}

	app->destroy();

	return 0;
}

2.glUniform3fv

2.1.运行

在这里插入图片描述

2.2.vs同上

2.3.fs同上

2.4.shader.h关键部分

#pragma once

#include "core.h"
#include<string>

class Shader {
public:
	Shader(const char* vertexPath, const char* fragmentPath);
	~Shader();
	
	void begin();//开始使用当前Shader

	void end();//结束使用当前Shader

	void setFloat(const std::string& name, float value);

	void setVector3(const std::string& name, float x, float y, float z);
	// 重载
	void setVector3(const std::string& name, const float* values);

private:
	//shader program
	//type:COMPILE LINK
	void checkShaderErrors(GLuint target,std::string type);

private:
	GLuint mProgram{ 0 };
};

2.5.shader.cpp关键部分

void Shader::setVector3(const std::string& name, const float* values){
    // 1 通过名称拿到Uniform变量的位置Location
    GLint location = GL_CALL(glGetUniformLocation(mProgram, name.c_str()));

    // 2 通过Location更新Uniform变量的值
    // 第二个参数:当前要更新的uniform变量如果三数组,数组里面包括多少个向量vec3
    GL_CALL(glUniform3fv(location, 1, values));
}

2.6.main.cpp关键部分

void render() {
	//执行opengl画布清理操作
	GL_CALL(glClear(GL_COLOR_BUFFER_BIT));

	//1 绑定当前的program
	shader->begin();

	float color[] = {0.9, 0.3, 0.25};
	shader->setVector3("uColor",color);
	
	//2 绑定当前的vao
	GL_CALL(glBindVertexArray(vao));
	//3 发出绘制指令
	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
	glBindVertexArray(0);
	shader->end();
}

相关推荐

  1. openGL :矩阵向量

    2024-07-15 10:08:03       50 阅读
  2. 使用OpenGL加载显示Q3O类型的3D模型文件

    2024-07-15 10:08:03       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 10:08:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 10:08:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 10:08:03       57 阅读
  4. Python语言-面向对象

    2024-07-15 10:08:03       68 阅读

热门阅读

  1. 等保测评全流程详解

    2024-07-15 10:08:03       20 阅读
  2. Writing Bazel rules: data and runfiles

    2024-07-15 10:08:03       20 阅读
  3. AcWing 667. 游戏时间

    2024-07-15 10:08:03       24 阅读
  4. 面向对象的开发方法

    2024-07-15 10:08:03       26 阅读
  5. 板级调试小助手(3)基于PYNQ的OLED视频显示

    2024-07-15 10:08:03       24 阅读
  6. 小白学习微信小程序的音频合成和语音识别技术

    2024-07-15 10:08:03       27 阅读
  7. 关系型数据库和非关系型数据库

    2024-07-15 10:08:03       26 阅读
  8. 国密证书(gmssl)在Kylin Server V10下安装

    2024-07-15 10:08:03       19 阅读
  9. GE DS200CVMAG1AEB控制器 处理器 模块

    2024-07-15 10:08:03       22 阅读
  10. 【Go系列】 Sync并发控制

    2024-07-15 10:08:03       25 阅读