Opengl ES 学习笔记 3:纹理贴图

#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <assert.h>

#include "importgl.h"

#include "app.h"
#include "shapes.h"
#include "cams.h"
#include <android/log.h>
#define PRECISION 16

inline GLfixed FixedFromInt(int value){ return value<<PRECISION; }

 


// Capped conversion from float to fixed.
static long Float2Fixed(float value)
{
    if (value < -32768) value = -32768;
    if (value > 32767) value = 32767;
    return (long)(value * 65536);
}

#define FIXED(value) Float2Fixed(value)

 


// Called from the app framework.
void appInit()
{
 glEnable(GL_BLEND);
 glEnable(GL_TEXTURE_2D);
 glClearColorx(0,0,0,0);
 glShadeModel(GL_SMOOTH);
//  glMatrixMode(GL_PROJECTION);
//  glLoadIdentity();
//
//  glOrthox(FixedFromInt(-50),FixedFromInt(50),
//   FixedFromInt(-50),FixedFromInt(50),
//   FixedFromInt(-50),FixedFromInt(50));

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}


// Called from the app framework.
void appDeinit()
{
 int a;
}

 


// Called from the app framework.
/* The tick is current time in milliseconds, width and height
* are the image dimensions to be rendered.
*/
void testTexture(GLushort *buffer) ;
void appRender(long tick, int width, int height)
{
 static int rotation=0;
 GLenum err  = 0;
 int i = 0;
 int j = 0;
 GLshort vertexArray[9]={-25,-25,20,  25,-25,-20,  0,25,20 };
 GLubyte colorArray[12]={255,0,0,0,  0,255,0,255,  0,0,255,0 };
 GLushort *buffer = (GLushort *) malloc(128*128*2);
 glViewport(0, 0, width, height);//设置视口的大小以及位置,视口:也就是图形最终显示到屏幕的区域,前两个参数是视口的位置,后两个参数是视口的宽和长。

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glLoadIdentity();

 //glTranslatex(0,0,FixedFromInt(-10));
 //glRotatex(FixedFromInt(rotation++),0,ONE,0);
 /*
 glEnableClientState(GL_VERTEX_ARRAY);
 glVertexPointer(3,GL_SHORT,0,vertexArray);

 glEnableClientState(GL_COLOR_ARRAY);
 glColorPointer(4,GL_UNSIGNED_BYTE,0,colorArray);

 //glDrawArrays(GL_TRIANGLES,0,3);

 glDisableClientState(GL_VERTEX_ARRAY);
 glDisableClientState(GL_COLOR_ARRAY);
 */
 //glEnable(GL_DEPTH_TEST);
 //glDepthFunc(GL_LEQUAL);
 //glPushMatrix();

 testTexture(buffer);
 //glPopMatrix();

 free(buffer);

}


void testTexture(GLushort *buffer)
{
 GLfixed rect[] = {
  Float2Fixed(-1.0f), Float2Fixed(-1.0),0,
  Float2Fixed(1.0f), Float2Fixed(-1.0), 0,
  Float2Fixed(1.0f), Float2Fixed(1.0), 0,
  Float2Fixed(-1.0f), Float2Fixed(-1.0),0,
  Float2Fixed(1.0f), Float2Fixed(1.0), 0,
  Float2Fixed(-1.0f), Float2Fixed(1.0f) , 0

 };
 GLfloat rect2[] = {
  -1.0f, -1.0,0,
  1.0f, -1.0, 0,
  1.0f, 1.0, 0,
  -1.0f,-1.0,0,
  1.0f, 1.0, 0,
  -1.0f, 1.0f , 0

 };
 GLfixed rnormal[] = {
  0, Float2Fixed(1.0f), 0,
  0, Float2Fixed(1.0f), 0,
  0, Float2Fixed(1.0f), 0,
  0, Float2Fixed(1.0f), 0,
  0, Float2Fixed(1.0f), 0,
  0, Float2Fixed(1.0f), 0
 };
 GLfixed rectuv[] = {
  Float2Fixed(0.0f), Float2Fixed(0.0),
  Float2Fixed(1.0f), Float2Fixed(0.0),
  Float2Fixed(1.0f), Float2Fixed(1.0),
  Float2Fixed(0.0f), Float2Fixed(0.0),
  Float2Fixed(1.0f), Float2Fixed(1.0),
  Float2Fixed(0.0f), Float2Fixed(1.0f)
 };

 unsigned short color[4]={ 0xF800, 0x7E0, 0x1F, 0xffff };
 int i;
 int j;
 for (  i = 0; i < 128; ++i )
 {
  for (  j = 0; j < 128; ++j )
  {

   buffer[i*128+j] = color[(i/32 + j/32)%4];
  }
 }
 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,  128, 128 , 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, buffer);
 glEnableClientState( GL_VERTEX_ARRAY );
 glEnableClientState( GL_NORMAL_ARRAY );
 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
 glVertexPointer( 3, GL_FIXED, 0, rect );
 glNormalPointer( GL_FIXED, 0, rnormal);
 glTexCoordPointer( 2, GL_FIXED, 0, rectuv );
 glDrawArrays( GL_TRIANGLES, 0, 6);


 glDisableClientState( GL_VERTEX_ARRAY );
 glDisableClientState( GL_NORMAL_ARRAY );
 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值