opengl es绘制四边形的三种方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/brahmsjiang/article/details/78682442
使用opengles3-book-master的ES API框架,由于opengl es图元不支持四边形。
所以用triangle,trianglestrip 和 trianglefan三种方法绘制四边形。

#include "esUtil.h"

typedef struct
{
   // Handle to a program object
   GLuint programObject;

} UserData;


///
// Initialize the program object
//
int Init ( ESContext *esContext )
{
   UserData *userData = esContext->userData;
   const char vShaderStr[] =
      "#version 300 es                            \n"
      "layout(location = 0) in vec4 a_position;   \n"
      "layout(location = 1) in vec4 a_color;      \n"
      "out vec4 v_color;                          \n"
      "void main()                                \n"
      "{                                          \n"
      "    v_color = a_color;                     \n"
      "    gl_Position = a_position;              \n"
      "}";


   const char fShaderStr[] =
      "#version 300 es            \n"
      "precision mediump float;   \n"
      "in vec4 v_color;           \n"
      "out vec4 o_fragColor;      \n"
      "void main()                \n"
      "{                          \n"
      "    o_fragColor = v_color; \n"
      "}" ;

   GLuint programObject;

   // Create the program object
   programObject = esLoadProgram ( vShaderStr, fShaderStr );

   if ( programObject == 0 )
   {
      return GL_FALSE;
   }

   // Store the program object
   userData->programObject = programObject;

   glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f );
   return GL_TRUE;
}

///
// Draw a square using the program object created in Init()
//
void Draw ( ESContext *esContext )
{
   UserData *userData = esContext->userData;
   GLfloat color[4 * 4] = 
   {
      1.0f, 0.0f, 0.0f, 1.0f,
	  0.0f, 1.0f, 0.0f, 1.0f,
	  0.0f, 0.0f, 1.0f, 1.0f,
	  1.0f, 1.0f, 0.0f, 1.0f,
   };
   //for two triangle, the sequence of vertex is not important
   //GLfloat vertexPos_six[6 * 3] =
   //{
   //   -0.5f,  -0.5f, 0.0f, // v0
   //   -0.5f, 0.5f, 0.0f, // v1
   //   0.5f, -0.5f, 0.0f,  // v2
	  //0.5f, 0.5f, 0.0f,  // v3
	  //0.5f, -0.5f, 0.0f,  // v2
	  //-0.5f, 0.5f, 0.0f, // v1
   //};
   //for trianglestrip, be caution of the sequence of vertex
   //GLfloat vertexPos_strip[4 * 3] =
   //{
	  // -0.5f,  -0.5f, 0.0f, // v0
	  // -0.5f, 0.5f, 0.0f, // v1
	  // 0.5f, -0.5f, 0.0f,  // v2
	  // 0.5f, 0.5f, 0.0f,  // v3
   //};
   //for trianglefan, be caution of the sequence of vertex
   GLfloat vertexPos_fan[4 * 3] =
   {
	   0.5f, -0.5f, 0.0f,  // v2
	   -0.5f,  -0.5f, 0.0f, // v0
	   -0.5f, 0.5f, 0.0f, // v1
	   0.5f, 0.5f, 0.0f,  // v3
   };

   glViewport ( 0, 0, esContext->width, esContext->height );

   glClear ( GL_COLOR_BUFFER_BIT );

   glUseProgram ( userData->programObject );
   glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, color);	//for color
   //glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vertexPos_six);	//for two triangle
   //glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vertexPos_strip); //for trianglestrip
   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertexPos_fan);	//for trianglefan

   glEnableVertexAttribArray ( 0 );
   glEnableVertexAttribArray( 1 );

   //glDrawArrays(GL_TRIANGLES, 0, 6);	//for two triangle
   //glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); //for trianglestrip
   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);	//for trianglefan
   glDisableVertexAttribArray ( 0 );
   glDisableVertexAttribArray( 1 );
}

void Shutdown ( ESContext *esContext )
{
   UserData *userData = esContext->userData;

   glDeleteProgram ( userData->programObject );
}

int esMain ( ESContext *esContext )
{
   esContext->userData = malloc ( sizeof ( UserData ) );

   esCreateWindow ( esContext, "draw square", 320, 320, ES_WINDOW_RGB );

   if ( !Init ( esContext ) )
   {
      return GL_FALSE;
   }

   esRegisterShutdownFunc ( esContext, Shutdown );
   esRegisterDrawFunc ( esContext, Draw );

   return GL_TRUE;
}


展开阅读全文

没有更多推荐了,返回首页