AKever

导航

OpenGL ES 环境搭建 helloworld

OpenGL ES 环境搭建 helloworld

win8.1\ vs2013\ mali es3.0.rar
PS:mali es3.0.rar 里面包括 模拟器安装程序 && sdk 安装程序(例子)

hello_triangel: 绘制一个简单的3角形

hello_triangel.frag

// hello_triangel.frag

#version 300 es                             
precision mediump float;                    
out vec4 fragColor;                         
void main()                                 
{                                           
   fragColor = vec4 ( 0.3, 0.2, 1.0, 1.0 ); 
}             

hello_triangel.vert

// hello_triangel.vert

#version 300 es                        
layout(location = 0) in vec4 vPosition;
void main()                            
{                                      
   gl_Position = vPosition;            
}    

主程序:

// UdHello_Triangle.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "EGLRuntime.h"
#include "Platform.h"
#include "SphereModel.h"
#include "Shader.h"
#include "Timer.h"

#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>

#include <string>

using namespace MaliSDK;
using std::string;

/* Asset directory. */
string resourceDirectory = "assets/";

/* Window. */
/* Height of window. */
const int windowHeight = 600;
/* Width of window. */
const int windowWidth = 800;

/* Instance of a timer used as input for path generation for leader. It is also used to keep the leader's velocity constant across different GPUs. */
Timer timer;

/* Constant telling number of buffer objects that should be generated. */
const GLuint numberOfBufferObjectIds = 4;
/* Array of buffer object names. */
GLuint bufferObjectIds[numberOfBufferObjectIds] = { 0 };

/* Program used for transforming vertices into world space. */
/* Fragment shader name. */
GLuint fragmentShaderId = 0;
string fragmentShaderPath = resourceDirectory + "hello_triangel.frag";
/* Vertex shader name. */
GLuint vertexShaderId = 0;
string vertexShaderPath = resourceDirectory + "hello_triangel.vert";
/* Program name. */
GLuint renderingProgramId = 0;


/* Program name. */
GLuint movementProgramId = 0;

bool initializeData(void)
{
    return true;
}

bool setupPrograms(void)
{
    bool functionCallResult = true;

    /* Create program object. */
    renderingProgramId = GL_CHECK(glCreateProgram());

    /* Initialize rendering program. */
    Shader::processShader(&vertexShaderId, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
    Shader::processShader(&fragmentShaderId, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);

    /* Attach vertex and fragment shaders to rendering program. */
    GL_CHECK(glAttachShader(renderingProgramId, vertexShaderId));
    GL_CHECK(glAttachShader(renderingProgramId, fragmentShaderId));

    /* Link and use rendering program object. */
    GL_CHECK(glLinkProgram(renderingProgramId));
    GL_CHECK(glUseProgram(renderingProgramId));

    /* Get uniform, attribute and uniform block locations from current program. */
    //positionLocation = GL_CHECK(glGetAttribLocation(renderingProgramId, "attributePosition"));
    //cubeVertexColorLocation = GL_CHECK(glGetAttribLocation(renderingProgramId, "attributeColor"));
    return functionCallResult;
}

void renderFrame(void)
{
    /* Clear contents of back buffer. */
    GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));

    /* Draw cubes on a screen. */
    GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f
    };

    // Load the vertex data
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
    glEnableVertexAttribArray(0);

    glDrawArrays(GL_TRIANGLES, 0, 3);
}

int main(int argc, char **argv)
{
    /* Intialise the Platform object for platform specific functions. */
    Platform* platform = Platform::getInstance();

    if (platform != NULL)
    {
        /* Initialize windowing system. */
        platform->createWindow(windowWidth, windowHeight);

        /* Initialize EGL. */
        EGLRuntime::initializeEGL(EGLRuntime::OPENGLES3);
        EGL_CHECK(eglMakeCurrent(EGLRuntime::display, EGLRuntime::surface, EGLRuntime::surface, EGLRuntime::context));

        /* Initialize data used for rendering. */
        if (initializeData())
        {
            /* Create programs. */
            if (setupPrograms())
            {
                /* Start counting time. */
                //timer.reset();

                /* Rendering loop to draw the scene starts here. */
                bool shouldContinueTheLoop = true;

                while (shouldContinueTheLoop)
                {
                    /* If something happened to the window, leave the loop. */
                    if (platform->checkWindow() != Platform::WINDOW_IDLE)
                    {
                        shouldContinueTheLoop = false;
                    }

                    /* Render a single frame */
                    renderFrame();

                    eglSwapBuffers(EGLRuntime::display, EGLRuntime::surface);
                }
            }
            else
            {
                LOGE("Could not create programs successfully.");
            }

            /* Delete buffers. */
            GL_CHECK(glDeleteBuffers(numberOfBufferObjectIds, bufferObjectIds));
        }
        else
        {
            LOGE("Could not initialize data used for rendering.");
        }
        /* Shut down EGL. */
        EGLRuntime::terminateEGL();

        /* Shut down windowing system. */
        platform->destroyWindow();

        /* Shut down the Platform object. */
        delete platform;
    }
    else
    {
        LOGE("Could not create platform.");
    }

    return 0;
} 

 在stdafx.h 文件中添加

#define GLES_VERSION 3

 

 

-- THE END !!

 

posted on 2015-09-14 00:07  AKever  阅读(420)  评论(0)    收藏  举报