c_cpp 这个程序样例将生成一个以蓝灰色为背景的橘黄色三角形,

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 这个程序样例将生成一个以蓝灰色为背景的橘黄色三角形,相关的知识,希望对你有一定的参考价值。

#version 330 core

layout (location = 0) in vec3 position;

void main()
{
	gl_Position = vec4(position.x, position.y, position.z, 1.0);
}
#version 330 core

out vec4 color;

void main()
{
	color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
#include <iostream>
#include <gl/glew.h>
#include <gl/freeglut.h>
#include "LoadShaders.h"

#ifdef GLEW_STATIC
#   pragma comment(lib,"glew32s.lib")
#else
#   pragma comment(lib,"glew32.lib")
#endif

//窗口大小
const GLuint WIDTH = 800, HEIGHT = 600;
//顶点数组对象(Vertex Array Object, VAO)
static GLuint VAO = 0;

/**
 * @brief 初始化程序
 */
void init()
{   
    ShaderInfo shaders[] = {
        { GL_VERTEX_SHADER, "triangles.vert" },
        { GL_FRAGMENT_SHADER, "triangles.frag" },
        { GL_NONE, NULL }
    };

    //加载着色器
    GLuint shaderProgram = loadShaders(shaders);
    if (shaderProgram != 0)//加载成功
    {
        //我们这里不再单独使用单独的着色器,所以,直接删除
        for (ShaderInfo *entry = shaders; entry->type != GL_NONE; ++entry) {
            glDeleteShader(entry->shader);
            entry->shader = 0;
        }
    }

    GLfloat vertices[] = {
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f, 0.5f, 0.0f
    };

    GLuint VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    //1.绑定VAO
    glBindVertexArray(VAO);
    //2.把顶点数组复制到缓冲中提供给OpenGL使用
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    // 3.设置顶点属性指针
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);// Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
    //4.解绑VAO
    glBindVertexArray(0);// Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
    //5.准备绘制
    glUseProgram(shaderProgram);
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}


/**
 * @brief 绘制刷新
 */
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);
    glFlush();
}


/**
 * @brief 使用opengl绘制一个三角形
 */
int main(int argc,char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitContextVersion(4, 2);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK)
    {
        std::cerr << "Unable to initialize GlEW ... exiting" << std::endl;
        exit(EXIT_FAILURE);
    }
    init();

    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
/**
 * @file LoadShaders.h
 * @brief opengl中用于加载着色器程序
 */
#ifndef __LOAD_SHADERS_H__
#define __LOAD_SHADERS_H__

#include <gl/GL.h>

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus


/**
 * @brief 着色器基本信息
 */
typedef struct {
    GLenum       type;      ///< 着色器类型,例如GL_VERTEX_SHADER或者GL_FRAGMENT_SHADER等。如果最后一个,应为GL_NONE
    const char*  filename;  ///< 该着色器对应的文件名称,例如"triangles.vert"或者"triangles.frag"等,如果是最后一个,应为NULL
    GLuint       shader;    ///< 该着色器加载后对应的ID
} ShaderInfo;

/**
 * @brief 加载并编译、链接着色器程序
 * @detail 接受一个着色器列表,每个元素对应一个着色器,分别包含着色器类型、对应GLSL的源码文件名称(文本文件)
 * @param[in,out] 需要加载的着色器的基本信息
 * @return 如果成功,返回着色器程序的ID(如glCreateProgram()返回的相同);如果失败,返回0
 */
GLuint loadShaders(ShaderInfo*);


#ifdef __cplusplus
};
#endif // __cplusplus

#endif // __LOAD_SHADERS_H__
#include <cstdlib>
#include <iostream>
#include <GL/glew.h>
#include "LoadShaders.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
 * @brief 读取一个着色器的文件
 * 
 * @param filename 着色器源码文件名称
 * @return 如果成功,返回源码字符串(注意,不要忘记在外部用delete []释放),否则,直接返回NULL
 */
static const GLchar* readShader(const char* filename)
{
#   ifdef WIN32
        FILE* infile;
        fopen_s(&infile, filename, "rb");
#   else
        FILE* infile = fopen(filename, "rb");
#   endif // WIN32
    if (!infile) {
#       ifdef _DEBUG
            std::cerr << "Unable to open file '" << filename << "'" << std::endl;
#       endif /* DEBUG */
        return NULL;
    }

    fseek(infile, 0, SEEK_END);
    int len = ftell(infile);
    fseek(infile, 0, SEEK_SET);

    GLchar* source = new GLchar[len + 1];

    fread(source, 1, len, infile);
    fclose(infile);

    source[len] = 0;

    return const_cast<const GLchar*>(source);
}


GLuint loadShaders(ShaderInfo* shaders)
{
    if (shaders == NULL) { return 0; }

    GLuint program = glCreateProgram();
    ShaderInfo* entry = shaders;
    while (entry->type != GL_NONE) {
        GLuint shader = glCreateShader(entry->type);
        entry->shader = shader;
        const GLchar* source = readShader(entry->filename);
        if (source == NULL) {
            for (entry = shaders; entry->type != GL_NONE; ++entry) {
                glDeleteShader(entry->shader);
                entry->shader = 0;
            }
            return 0;
        }

        glShaderSource(shader, 1, &source, NULL);
        delete[] source;

        glCompileShader(shader);

        GLint compiled;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
        if (!compiled) {
#           ifdef _DEBUG
                GLsizei len;
                glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
                GLchar* log = new GLchar[len + 1];
                glGetShaderInfoLog(shader, len, &len, log);
                std::cerr << "Shader compilation failed: " << log << std::endl;
                delete[] log;
#           endif /* DEBUG */

            return 0;
        }

        glAttachShader(program, shader);

        ++entry;
    }

    glLinkProgram(program);

    GLint linked;
    glGetProgramiv(program, GL_LINK_STATUS, &linked);

    if (!linked) {
#       ifdef _DEBUG
            GLsizei len;
            glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len);

            GLchar* log = new GLchar[len + 1];
            glGetProgramInfoLog(program, len, &len, log);
            std::cerr << "Shader linking failed: " << log << std::endl;
            delete[] log;
#       endif /* DEBUG */
            
        for (entry = shaders; entry->type != GL_NONE; ++entry) {
            glDeleteShader(entry->shader);
            entry->shader = 0;
        }
        return 0;
    }

    return program;
}

#ifdef __cplusplus
}
#endif // __cplusplus

以上是关于c_cpp 这个程序样例将生成一个以蓝灰色为背景的橘黄色三角形,的主要内容,如果未能解决你的问题,请参考以下文章

为给定的背景颜色自动选择适当的灰色

html2帆布灰色背景去除

iphone上点击div会出现半透明灰色背景

C# winfrom程序可以实现按钮的鼠标悬停变色,离开恢复,按下时变成黑色并一直保持吗

用例将 json 加载为一行而不是多行,特定于数据集

UITableView 的“反弹区域”中的浅灰色背景