环境
- win10
- visual studio2019
第二章
P9 案例1——glClearBufferfv 方法示例
#include "sb7.h"
class my_application : public sb7::application {
public:
void render(double currentTime) override {
//四个参数分别代表红、绿、蓝、透明度
static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, red);
}
};
调用方法的主程序(不同平台的调用方法见sb7.h文件结尾处)
#include <iostream>
#include "1_sb7.cpp"
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) {
my_application* app = new my_application;
app->run(app); //run方法中通过while循环调用我们的render方法
delete app;
return 0;
}
实现效果:
绘制一个点
#include "sb7.h"
class my_application : public sb7::application {
public:
void render(double currentTime) override {
glUseProgram(rendering_program);
glDrawArrays(GL_POINTS, 0, 1);
}
virtual void startup() override {
rendering_program = compile_shaders();
glCreateVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
}
virtual void shutdown() override {
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(rendering_program);
glDeleteVertexArrays(1, &vertex_array_object);
}
GLuint compile_shaders() {
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
//source code for vertex shader
static const GLchar* vertex_shader_source[] = {
"#version 460 core \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = vec4(0.0f, 0.0f, 0.5f, 1.0f); \n"
"} \n"
};
//source code for fragment shader
static const GLchar* fragment_shader_source[] = {
"#version 460 core \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n "
" color = vec4(0.0f, 0.0f, 1.0f, 1.0f); \n"
"} \n"
};
//create and compile vertex shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER); //glCreateShader创建一个空白的着色器对象
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL); //glShaderSource将着色器源代码传递给着色器对象
glCompileShader(vertex_shader); //对着色器对象中包含的任何我源代码进行编译
//create and compile fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);
//create program,attach shader to it, and link it
program = glCreateProgram(); //glCreateProgram 创建一个程序对象,我们可将着色器对象附加到该对象上
glAttachShader(program, vertex_shader); //glAttachShader 将一个着色器对象附加到一个程序对象中
glAttachShader(program, fragment_shader);
glLinkProgram(program); //将所有附加到程序对象的着色器对象链接在一起
//delete the shader as the program has them now
//删除着色器对象的原因是:一旦着色器链接到一个程序对象,程序将包含二进制代码,并不在需要着色器
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
glPointSize(40.0f);
return program;
}
private:
GLuint rendering_program;
GLuint vertex_array_object;
};
运行结果:
第三章 管线
https://blog.csdn.net/yuxiaohen/article/details/50390764