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

本文介绍如何使用OpenGL ES API中的三角形、三角形带和三角形扇形来绘制四边形,提供了具体的顶点着色器和片段着色器代码,并展示了不同绘制方式下顶点顺序的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值