Opengl ES 红绿蓝三角形
Opengl ES 红绿蓝三角形
环境:Mali OpenGL ES SDK v2.2.0(参考一下链接)
http://www.cnblogs.com/TS-qrt/articles/es_helloworld.html
===================================================
hello_triangel.vert
#version 300 es layout(location = 0) in vec4 a_position; layout(location = 1) in vec4 a_color; out vec4 v_color; void main() { v_color = a_color; gl_Position = a_position; }
hello_triangel.frag
#version 300 es precision mediump float; in vec4 v_color; out vec4 o_fragColor; void main() { o_fragColor = v_color; }
main.cpp
// UdSixOne.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" // 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; // vboIds GLuint vboIds[3]; #define VERTEX_POS_SIZE 3 // x, y and z #define VERTEX_COLOR_SIZE 4 // r, g, b, and a #define VERTEX_POS_INDX 0 #define VERTEX_COLOR_INDX 1 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)); vboIds[0] = 0; vboIds[1] = 0; vboIds[2] = 0; glClearColor(1.0f, 1.0f, 1.0f, 0.0f); return functionCallResult; } void renderFrame(void) { GLfloat vertexPos[3 * VERTEX_POS_SIZE] = { 0.0f, 0.5f, 0.0f, // v0 -0.5f, -0.5f, 0.0f, // v1 0.5f, -0.5f, 0.0f // v2 }; GLfloat color[4 * VERTEX_COLOR_SIZE] = { 1.0f, 0.0f, 0.0f, 1.0f, // c0 0.0f, 1.0f, 0.0f, 1.0f, // c1 0.0f, 0.0f, 1.0f, 1.0f // c2 }; GLint vtxStrides[2] = { VERTEX_POS_SIZE * sizeof (GLfloat), VERTEX_COLOR_SIZE * sizeof (GLfloat) }; // Index buffer data GLushort indices[3] = { 0, 1, 2 }; GLfloat *vtxBuf[2] = { vertexPos, color }; glViewport(0, 0, 800, 600); glClear(GL_COLOR_BUFFER_BIT); if (vboIds[0] == 0 && vboIds[1] == 0 && vboIds[2] == 0) { // Only allocate on the first draw glGenBuffers(3, vboIds); glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); glBufferData(GL_ARRAY_BUFFER, vtxStrides[0] * 3, vtxBuf[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]); glBufferData(GL_ARRAY_BUFFER, vtxStrides[1] * 3, vtxBuf[1], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[2]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof (GLushort)* 3, indices, GL_STATIC_DRAW); } glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]); glEnableVertexAttribArray(VERTEX_POS_INDX); glVertexAttribPointer(VERTEX_POS_INDX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[0], 0); glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]); glEnableVertexAttribArray(VERTEX_COLOR_INDX); glVertexAttribPointer(VERTEX_COLOR_INDX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, vtxStrides[1], 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[2]); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0); glDisableVertexAttribArray(VERTEX_POS_INDX); glDisableVertexAttribArray(VERTEX_COLOR_INDX); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } 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
浙公网安备 33010602011771号