用OpenGL_ES 绘制了一个圆
#include "esUtil.h"
#include <math.h>
#define PI 3.1415926
typedef struct
{
GLuint programObject; // Handle to a program object
} UserData;
///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader ( GLenum type, const char *shaderSrc )
{
GLuint shader; // GLuint is typedefed as unsigned int
GLint compiled; // GLint is typedefed as int
// Create the shader object
shader = glCreateShader ( type );
glShaderSource ( shader, 1, &shaderSrc, NULL );// Load the shader source
glCompileShader ( shader );// Compile the shader
glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled ); // Check the compile status
return shader;
}
///
// Initialize the shader and program object
//
int Init ( ESContext *esContext )
{
UserData *userData = esContext->userData;
char vShaderStr[] = //顶点着色器
"#version 300 es \n" //声明着色器版本
"layout(location = 0) in vec4 vPosition; \n" //输入一个属性数组
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n" //将属性数组拷贝到gl_Position的特殊输出变量中
"} \n";
char fShaderStr[] = //片段着色器
"#version 300 es \n" //
"precision mediump float; \n" //声明着色器中浮点变量的默认精度
"out vec4 fragColor; \n" //声明一个输出变量
"void main() \n"
"{ \n"
" fragColor = vec4 ( 0.1, 0.2, 0.3, 1.0 ); \n" //给这个输出变量赋值
"} \n";
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
// Load the vertex/fragment shaders
vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr ); //
fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr ); //
// Create the program object
programObject = glCreateProgram ( );
if ( programObject == 0 )
{
return 0;
}
glAttachShader ( programObject, vertexShader );
glAttachShader ( programObject, fragmentShader );
// Link the program
glLinkProgram ( programObject );
// Check the link status
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
// Store the program object
userData->programObject = programObject;
glClearColor ( 100.0f, 100.0f, 100.0f, 1.0f );
return TRUE;
}
///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{
UserData *userData = esContext->userData;
glViewport ( 0, 0, esContext->width, esContext->height );// Set the viewport
glClear ( GL_COLOR_BUFFER_BIT );// Clear the color buffer
glUseProgram ( userData->programObject );// Use the program object
GLfloat x1 = 0.0f,y1=0.0f,x2=1.0f,y2=0.0f;
GLfloat vVertices[] = {
x1, y1, 0.0f,
x2, y2,0.0f,
};
GLfloat num = 64.0f;
for(int i = 0; i <= num; i++)
{
x1 = x2;y1=y2;
x2 = cos((GLfloat)(2*PI)/num*i);
y2 = sin((GLfloat)(2*PI)/num*i);
GLfloat vVertices[] = {
x1, y1, 0.0f,
x2, y2,0.0f,
};
glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );// Load the vertex data
glEnableVertexAttribArray ( 0 );
glDrawArrays ( GL_LINE_STRIP, 0, 2 );
glLineWidth(5);
}
}
void Shutdown ( ESContext *esContext )
{
UserData *userData = esContext->userData;
glDeleteProgram ( userData->programObject );
}
int esMain ( ESContext *esContext )
{
esContext->userData = malloc ( sizeof ( UserData ) );
esCreateWindow ( esContext, "Hello Triangle", 900, 900, ES_WINDOW_RGB );
if ( !Init ( esContext ) )
{
return GL_FALSE;
}
esRegisterShutdownFunc ( esContext, Shutdown );//recall Shutdown function
esRegisterDrawFunc ( esContext, Draw ); //recall Draw function
return GL_TRUE;
}