Opengl ES Glew库 ----- By YDD的铁皮锅

Posted yddsblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Opengl ES Glew库 ----- By YDD的铁皮锅相关的知识,希望对你有一定的参考价值。

前一篇随笔我写了Opengl的窗口创建,这一篇随笔我要写OpenGL glew库的使用。首先需要引入头文件h,库文件Lib和动态链接库DLL,百度搜索OpenGL glew库找到这个纯英文网站,尽量不要去下别人提供的文件一是版本跟不上,也有可能会缺些东西,还是去官方提供的好。点击glew在开头找到LIBRARY然后GLEW再是Download下的Glew Web Site,点进去之后下面会有一排下载链接从上到下版本越低,建议用最新的。一是可能你在网上找别人的代码Ctrl C+V结果有些函数不能用,就有可能是你的glew版本低了。

下载下来是一个zip压缩包解压,不管其他东西找到里面的glew. h. glew. lib和glew. dll这三个文件。然后找到你编译器头文件目录把h文件拖进include文件夹里,lib文件放入Lib文件夹,dll文件拖入C:\\Windows\\System32\\文件夹下。然后#include头文件就可以使用glew库了。(记得加#pragma comment(lib, "glew32.lib"))

OpenGL的特有类型:

typedef unsigned int GLenum;
typedef unsigned int GLbitfield;
typedef unsigned int GLuint;
typedef int GLint;
typedef int GLsizei;
typedef unsigned char GLboolean;
typedef signed char GLbyte;
typedef short GLshort;
typedef unsigned char GLubyte;
typedef unsigned short GLushort;
typedef unsigned long GLulong;
typedef float GLfloat;
typedef float GLclampf;
typedef double GLdouble;
typedef double GLclampd;
typedef void GLvoid;

然后是一些介绍一些glew库的常用函数和用法。

 

 glewInit() glew库初始化(一定要初始化)

 

用于GLShader编程

glCreateProgram() 创建一个Program

glCreateShader() 创建一个着色器

glShaderSource() 用于把Shader代码和着色器关联

glCompileShader() 编译着色器源代码

glGetShader..() 获取Shader编译信息

glGetShaderInfoLog() 获取编译Log

glAttachShader() 把着色器和Program关联

glLinkProgram() 链接项目

glGetProgram..() 获取Program信息

glGetProgramInfoLog() 获取Program链接Log

glUseProgram() 使用由下标代表的Program

 

用于创建顶点,指定顶点数据

glGenVertexArrays()  

glBindVertexArray()

glGenBuffers()

glBindBuffer()

glBufferData()

glBufferSubData()

glVertexAttribPointer()

 

用于创建纹理和纹理图片

glGenTextures()

glBindTexture()

glTexParameter..()

glTexImage2D()

glUniform..()

 

接下来是我的Demo测试,大家可以放到VS上玩一玩。#include "stdafx.h"没有可以删去,加上#include <Windows.h> 和 #include <gl/glew.h>。技术图片

#include "stdafx.h"

#pragma comment(lib ,"opengl32.lib")
#pragma comment(lib , "glew32.lib")

GLuint render, glarray, program;
float arr[2] =  0.0f, 0.0f , angle = 0.0f;

GLfloat MoveMat[] = 
    1.0f*cos(angle), 0.0f, 1.0f*sin(angle), 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f,
    -1.0f*sin(angle), 0.0f, 1.0f*cos(angle), 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f
;

const GLchar *vShader = "#version 330 core\\n"
"layout (location = 0) in vec4 vPosition;"
"layout (location = 1) in vec4 vColor;"
"uniform vec2 vector;"
"uniform mat4 mat;"
"varying vec4 out_tex;"
"void main()"
""
"    gl_Position = vPosition * mat + vector;"
"    out_tex = vColor;"
"";

const GLchar *fShader = "#version 330 core\\n"
"varying vec4 out_tex;"
"void main()"
""
"gl_FragColor = out_tex;"
"";

void init() 

    const GLfloat position[][4] = 
        //under
         0.25, -0.25, 0.25, 1.0 ,
         -0.25, -0.25, 0.25, 1.0 ,
         -0.25, -0.25, 0.5, 1.0 ,

         0.25, -0.25, 0.25, 1.0 ,
         -0.25, -0.25, 0.5, 1.0 ,
         0.25, -0.25, 0.5, 1.0 ,
        //right
         0.25, -0.25, 0.5, 1.0 ,
         0.25, -0.25, 0.25, 1.0 ,
         0.25, 0.25, 0.25, 1.0 ,

         0.25, -0.25, 0.5, 1.0 ,
         0.25, 0.25, 0.25, 1.0 ,
         0.25, 0.25, 0.5, 1.0 ,
        //left
         -0.25, -0.25, 0.5, 1.0 ,
         -0.25, -0.25, 0.25, 1.0 ,
         -0.25, 0.25, 0.25, 1.0 ,

         -0.25, -0.25, 0.5, 1.0 ,
         -0.25, 0.25, 0.25, 1.0 ,
         -0.25, 0.25, 0.5, 1.0 ,
        //back
         -0.25, -0.25, 0.25, 1.0 ,
         0.25, -0.25, 0.25, 1.0 ,
         0.25, 0.25, 0.25, 1.0 ,

         -0.25, -0.25, 0.25, 1.0 ,
         0.25, 0.25, 0.25, 1.0 ,
         -0.25, 0.25, 0.25, 1.0 ,
        //front
         -0.25, -0.25, 0.5, 1.0 ,
         0.25, -0.25, 0.5, 1.0 ,
         0.25, 0.25, 0.5, 1.0 ,

         -0.25, -0.25, 0.5, 1.0 ,
         0.25, 0.25, 0.5, 1.0 ,
         -0.25, 0.25, 0.5, 1.0 ,
        //up
         0.25, 0.25, 0.5, 1.0 ,
         0.25, 0.25, 0.25, 1.0 ,
         -0.25, 0.25, 0.25, 1.0 ,

         -0.25, 0.25, 0.5, 1.0 ,
         0.25, 0.25, 0.5, 1.0 ,
         -0.25, 0.25, 0.25, 1.0 ,

    ;

    const GLfloat color[][4] = 
         1.0, 0.0, 0.0, 1.0 ,
         1.0, 0.0, 0.0, 1.0 ,
         1.0, 0.0, 0.0, 1.0 ,
         1.0, 0.0, 0.0, 1.0 ,
         1.0, 0.0, 0.0, 1.0 ,
         1.0, 0.0, 0.0, 1.0 ,

         0.0, 1.0, 0.0, 1.0 ,
         0.0, 1.0, 0.0, 1.0 ,
         0.0, 1.0, 0.0, 1.0 ,
         0.0, 1.0, 0.0, 1.0 ,
         0.0, 1.0, 0.0, 1.0 ,
         0.0, 1.0, 0.0, 1.0 ,

         0.0, 0.0, 1.0, 1.0 ,
         0.0, 0.0, 1.0, 1.0 ,
         0.0, 0.0, 1.0, 1.0 ,
         0.0, 0.0, 1.0, 1.0 ,
         0.0, 0.0, 1.0, 1.0 ,
         0.0, 0.0, 1.0, 1.0 ,

         1.0, 1.0, 0.0, 1.0 ,
         1.0, 1.0, 0.0, 1.0 ,
         1.0, 1.0, 0.0, 1.0 ,
         1.0, 1.0, 0.0, 1.0 ,
         1.0, 1.0, 0.0, 1.0 ,
         1.0, 1.0, 0.0, 1.0 ,

         1.0, 0.0, 1.0, 1.0 ,
         1.0, 0.0, 1.0, 1.0 ,
         1.0, 0.0, 1.0, 1.0 ,
         1.0, 0.0, 1.0, 1.0 ,
         1.0, 0.0, 1.0, 1.0 ,
         1.0, 0.0, 1.0, 1.0 ,

         0.0, 1.0, 1.0, 1.0 ,
         0.0, 1.0, 1.0, 1.0 ,
         0.0, 1.0, 1.0, 1.0 ,
         0.0, 1.0, 1.0, 1.0 ,
         0.0, 1.0, 1.0, 1.0 ,
         0.0, 1.0, 1.0, 1.0 ,

    ;

    glewInit();

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);

    GLuint shader = 0;
    GLint news;
    program = glCreateProgram();

    shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(shader, 1, (const GLchar **)&vShader, NULL);
    glCompileShader(shader);
    glGetShaderiv(shader, GL_COMPILE_STATUS, &news);
    glAttachShader(program, shader);

    shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(shader, 1, (const GLchar **)&fShader, NULL);
    glCompileShader(shader);
    glGetShaderiv(shader, GL_COMPILE_STATUS, &news);
    glAttachShader(program, shader);
    glLinkProgram(program);
    glUseProgram(program);

    glGenVertexArrays(1, &glarray);
    glBindVertexArray(glarray);

    glGenBuffers(1, &render);
    glBindBuffer(GL_ARRAY_BUFFER, render);
    glBufferData(GL_ARRAY_BUFFER, sizeof(position) + sizeof(color), NULL, GL_STATIC_DRAW);

    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(position), position);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(position), sizeof(color), color);

    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)sizeof(position));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glUniformMatrix4fv(glGetUniformLocation(program, "mat"), 1, GL_FALSE, MoveMat);



LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
    WPARAM wParam, LPARAM lParam)


    switch (message)
    
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        
        case VK_ESCAPE:
        
            PostQuitMessage(0);
            return 0;
        
        case VK_UP:
        
            arr[1] += 0.1f;
            break;
        
        case VK_DOWN:
        
            arr[1] -= 0.1f;
            break;
        
        case VK_LEFT:
        
            arr[0] -= 0.1f;
            break;
        
        case VK_RIGHT:
        
            arr[0] += 0.1f;
            break;
        
        default:
            break;
        
        return 0;

    case WM_CHAR:
    
        if (wParam == a) 
            angle += 0.5f;
            MoveMat[0] = 1.0f*cos(angle);
            MoveMat[2] = 1.0f*sin(angle);
            MoveMat[8] = -1.0f*sin(angle);
            MoveMat[10] = 1.0f*cos(angle);
        
        else if (wParam == d) 
            angle -= 0.5f;
            MoveMat[0] = 1.0f*cos(angle);
            MoveMat[2] = 1.0f*sin(angle);
            MoveMat[8] = -1.0f*sin(angle);
            MoveMat[10] = 1.0f*cos(angle);
        
        return 0;
    

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    



int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int iCmdShow)

    MSG msg;
    BOOL bQuit = FALSE;

    WNDCLASS wc;
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = TEXT("GLSample");
    RegisterClass(&wc);

    HWND hWnd = CreateWindowA("GLSample", "OpenGL Sample", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 600, 150, 600, 600, NULL, NULL, hInstance, NULL);
    if (hWnd == NULL) return -1;

    HDC hDC = GetDC(hWnd);
    PIXELFORMATDESCRIPTOR pfd;

    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    int iFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, iFormat, &pfd);

    HGLRC hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    init();

    while (!bQuit)
    

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        

            if (msg.message == WM_QUIT)
            
                bQuit = TRUE;
            
            else
            
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            
        
        else
        

            glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glBindVertexArray(glarray);
            glUniform2fv(glGetUniformLocation(program, "vector"), 1, arr);
            glUniformMatrix4fv(glGetUniformLocation(program, "mat"), 1, GL_FALSE, MoveMat);

            glDrawArrays(GL_TRIANGLES, 0, 36);


            SwapBuffers(hDC);

            Sleep(10);
        
    


    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);
    DestroyWindow(hWnd);


    return msg.wParam;

 按下小写字母a绕z轴顺时针旋转,字母d逆时针旋转,方向键↑向上移动,其他三个也有响应。

这一篇还是有很多东西没有写清楚,可能是自己文笔不好吧。然后这些函数的用法效果也要单独来写;Opengl着色器,OpenGL渲染过程这些东西的理解对于使用glew库也十分重要。

下一篇从OpenGL渲染过程和着色器理论从头写起,在结合Demo来实践加强理解。

以上是关于Opengl ES Glew库 ----- By YDD的铁皮锅的主要内容,如果未能解决你的问题,请参考以下文章

使用C++库GLEW在openGl中绘制一个红色三角形[关闭]

GLEW 将 OpenGL 2 EXT 加载为 OpenGL 4

OpenGL2.0及以上版本中gl,glut,glew,glfw,mesa等部件的关系

Blender源码分析之OpenGL基本例子

使用 GLFW/GLEW 进行 OpenGL 编程

Blender源码分析之OpenGL基本例子