//
//  VertexArray.h
//
//  Created by döme on 28.07.2009.
//

/*
 * Copyright (c) 2009 Doemoetoer Gulyas.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import <Foundation/Foundation.h>

#import <OpenGLES/ES1/gl.h>

#import "geometry.h"

enum
{
	VA_FORMAT_ILLEGAL,
	VA_FORMAT_POS3F,
	VA_FORMAT_POS3F_NORMAL3F_TEX2F,
	VA_FORMAT_POS3F_TEX2F,
	VA_FORMAT_POS3F_COL4B_TEX2F,
	VA_FORMAT_
};

typedef struct Position3f
{
	float	pos[3];
} Position3f;

typedef struct PositionTex3f2f
{
	float	pos[3];
	float	texcoord[2];
} PositionTex3f2f;

typedef struct PositionColorTex3f4b2f
{
	float	pos[3];
	uint8_t	color[4];
	float	texcoord[2];
} PositionColorTex3f4b2f;

typedef struct PositionNormalTex3f3f2f
{
	float	pos[3];
	float	normal[3];
	float	texcoord[2];
} PositionNormalTex3f3f2f;


/* ref counted stuff would be needed for lazy copying of vertex arrays

typedef struct gl_refcounted_s
{
	union {
		GLint	intr;
		GLuint	uintr;
		GLushort* indicesr;
		PositionNormalTex3f3f2f* verticesr;
		size_t	sizer;
	} rsrc;

	int refcount;
	void (*dealloc)(struct gl_refcounted_s*);

} gl_refcounted_t;

gl_refcounted_t* glref_retain(gl_refcounted_t*);
gl_refcounted_t* glref_release(gl_refcounted_t*);

gl_refcounted_t* glref_withVertices(PositionNormalTex3f3f2f*);
gl_refcounted_t* glref_withInt(GLint);
gl_refcounted_t* glref_withUint(GLuint);
*/

@class ES2Shader;

@interface VertexArray : NSObject
{
@public

	void*		_vertices;
	size_t		numVertices;
	int			vertexFormat;

	GLushort*	indices;
	size_t		numIndices;
	
	GLuint		vertexBufferName, indexBufferName, vertexArrayObject;
	
	GLenum		usageHint;

	GLint		mode;
	
	BOOL		useVAO;
	BOOL		needsVertexBufferUpdate;
	BOOL		needsIndexBufferUpdate;

}

@property(readonly) size_t numVertices;

+ (VertexArray*) coneWithFraction: (float) fraction;
+ (VertexArray*) sharedCylinder;
+ (VertexArray*) sharedDisk;
+ (VertexArray*) sharedQuad;

+ (VertexArray*) sharedSphere;
+ (VertexArray*) sharedXSemiSphere;

+ (void) releaseSharedObjects;

- (size_t) sizeofFormat;

- (void) draw;
- (void) drawRangeFrom: (int) fromi to: (int) toi;

- (void) convertTriangleIndicesToLines;
- (void) convertPseudoQuadIndicesToLines;

- (void) applyVertexTransform: (m16) m;
- (void) disposeOfNormals;

- (size_t) memoryUsage;

@end


