您的位置:首页 > 移动开发 > Android开发

android上用opengl画线

2009-09-17 17:45 387 查看
android上用opengl画线

/*
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0 **
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
static void gluLookAt(float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ, float upX, float upY,
float upZ)
{
// See the OpenGL GLUT documentation for gluLookAt for a description
// of the algorithm. We implement it in a straightforward way:
float fx = centerX - eyeX;
float fy = centerY - eyeY;
float fz = centerZ - eyeZ;
// Normalize f
float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz);
fx *= rlf;
fy *= rlf;
fz *= rlf;
// Normalize up
float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ);
upX *= rlup;
upY *= rlup;
upZ *= rlup;
// compute s = f x up (x means "cross product")
float sx = fy * upZ - fz * upY;
float sy = fz * upX - fx * upZ;
float sz = fx * upY - fy * upX;
// compute u = s x f
float ux = sy * fz - sz * fy;
float uy = sz * fx - sx * fz;
float uz = sx * fy - sy * fx;
float m[16] ;
m[0] = sx;
m[1] = ux;
m[2] = -fx;
m[3] = 0.0f;
m[4] = sy;
m[5] = uy;
m[6] = -fy;
m[7] = 0.0f;
m[8] = sz;
m[9] = uz;
m[10] = -fz;
m[11] = 0.0f;
m[12] = 0.0f;
m[13] = 0.0f;
m[14] = 0.0f;
m[15] = 1.0f;
glMultMatrixf(m);
glTranslatef(-eyeX, -eyeY, -eyeZ);
}
static void checkGLErrors()
{
GLenum error = glGetError();
if (error != GL_NO_ERROR)
fprintf(stderr, "GL Error: 0x%04x/n", (int)error);
}
#define PI 3.1415926535897932f
static void gluPerspective(GLfloat fovy, GLfloat aspect,
GLfloat zNear, GLfloat zFar)
{
GLfloat xmin, xmax, ymin, ymax;
ymax = zNear * (GLfloat)tan(fovy * PI / 360);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustumx((GLfixed)(xmin * 65536), (GLfixed)(xmax * 65536),
(GLfixed)(ymin * 65536), (GLfixed)(ymax * 65536),
(GLfixed)(zNear * 65536), (GLfixed)(zFar * 65536));
}
void init(int width, int height)
{
//	glEnable(GL_NORMALIZE);
//	glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
//	glShadeModel(GL_FLAT);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
//glEnable(GL_LIGHT2);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glOrthof(0, (float)width, 0, (float)height, 0, 100);
}
static void prepareFrame(int width, int height)
{
glViewport(0, 0, width, height);
glClearColorx((GLfixed)(0.1f * 65536),
(GLfixed)(0.2f * 65536),
(GLfixed)(0.3f * 65536), 0x10000);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//	gluPerspective(45, (float)width / height, 0.5f, 150);
/*glFrustumf(0.0f, 1.0f,
0.0f, 1.0f,
1.0f, 30.0f);
*/
glOrthof(0.0, width, 0.0, height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLfloat vertices[][2] = {
{ 100.25f,  100.25f  },
{ 100.75f,  100.25f  },
{ 200.75f,  200.75f  },
{ 100.25f,  200.75f  }
};
GLubyte colors[][4] = {
{ 255, 0, 0, 80 },
{ 255, 0, 0, 80 },
{ 255, 0, 0, 80 },
{ 255, 0, 0, 80 }
};
int main(int argc, char** argv)
{
EGLint s_configAttribs[] = {
EGL_RED_SIZE,       5,
EGL_GREEN_SIZE,     6,
EGL_BLUE_SIZE,      5,
EGL_NONE
};
EGLint numConfigs = -1;
EGLint majorVersion;
EGLint minorVersion;
EGLConfig config;
EGLContext context;
EGLSurface surface;
EGLint w, h;
EGLDisplay dpy;
dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(dpy, &majorVersion, &minorVersion);
eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
surface = eglCreateWindowSurface(dpy, config,
android_createDisplaySurface(), NULL);
context = eglCreateContext(dpy, config, NULL, NULL);
eglMakeCurrent(dpy, surface, surface, context);
eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
GLint dim = w<h ? w : h;
printf("w=%d, h=%d/n", w, h);
init(w, h);
int i=0, j=0;
//while(1){
prepareFrame(w, h);
/*r(i=0; i<4; i++){
vertices[i][0] += 1.0f;
vertices[i][1] += 1.0f;
}*/
glPushMatrix();
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glDrawArrays(GL_LINES, 0, 4);
glPopMatrix();
checkGLErrors();
eglSwapBuffers(dpy, surface);
checkGLErrors();
//}
return 0;
}


放到frameworks/base/opengl/test下编译

adb push 到emulator中运行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: