PageRenderTime 193ms CodeModel.GetById 100ms app.highlight 6ms RepoModel.GetById 85ms app.codeStats 0ms

/TheElements/jni/gl.c

http://thelements.googlecode.com/
C | 103 lines | 55 code | 24 blank | 24 comment | 2 complexity | e6b46b95df178c2f7f1cd4897565e606 MD5 | raw file
  1/*
  2 * gl.c
  3 * --------------------------
  4 * Defines the gl rendering and initialization
  5 * functions appInit, appDeinit, and appRender.
  6 */
  7
  8#include "gl.h"
  9
 10static unsigned int textureID;
 11
 12// Called from the app framework. is onSurfaceCreated
 13void appInit()
 14{
 15	glShadeModel(GL_SMOOTH);
 16	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
 17	glClearDepthf(1.0f);
 18	glEnable(GL_DEPTH_TEST);
 19	glDepthFunc(GL_LEQUAL);
 20	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
 21
 22	//Generate the new texture
 23	glGenTextures(1, &textureID);
 24	//Bind the texture
 25	glBindTexture(GL_TEXTURE_2D, textureID);
 26
 27	glEnable(GL_TEXTURE_2D);
 28
 29	//Set tex params
 30	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 31	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 32
 33	//Different possible texture parameters, e.g
 34	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 35	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 36
 37	//Generate the tex image
 38	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, colors);
 39
 40	//Disable tex (clean up)
 41	glDisable(GL_TEXTURE_2D);
 42}
 43
 44// Called from the app framework.
 45void appDeinit()
 46{
 47
 48}
 49
 50// Called from the app framework.
 51/* The tick is current time in milliseconds, width and height
 52 * are the image dimensions to be rendered.
 53 */
 54void appRender()
 55{
 56	float vertices[] =
 57	{ 0.0f, 0.0f, 512.0f, 0.0f, 0.0f, 1024.0f, 512.0f, 1024.0f };
 58
 59	float texture[] =
 60	{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f };
 61
 62	unsigned char indices[] =
 63	{ 0, 1, 3, 0, 3, 2 };
 64
 65	UpdateView();
 66
 67	glMatrixMode(GL_PROJECTION);
 68	glLoadIdentity();
 69	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 70
 71	if (flipped == 0)
 72	{
 73		glOrthof(0.0f, (float) maxx, (float) maxy, 0.0f, 1.0f, -1.0f); //--Device
 74	}
 75	else
 76	{
 77		glOrthof(0.0f, (float) maxx, 0.0f, (float) -maxy, 1.0f, -1.0f); //--Emulator
 78	}
 79
 80	//Draw the texture stuff --------------------------------------------------------------
 81	glEnable(GL_TEXTURE_2D);
 82
 83	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, maxy, GL_RGB,
 84			GL_UNSIGNED_BYTE, colors);
 85
 86	//Enable the vertex and coord arrays
 87	glEnableClientState(GL_VERTEX_ARRAY);
 88	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 89
 90	//Set the pointers
 91	glVertexPointer(2, GL_FLOAT, 0, vertices);
 92	glTexCoordPointer(2, GL_FLOAT, 0, texture);
 93
 94	//Actually draw the rectangle with the text on it
 95	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
 96
 97	//Disable the vertex and coord arrays (clean up)
 98	glDisableClientState(GL_VERTEX_ARRAY);
 99	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
100
101	//Disable tex (clean up)
102	glDisable(GL_TEXTURE_2D);
103}