summaryrefslogtreecommitdiff
path: root/tests/glean/geomrend.h
blob: 31678b28174b5d1dbe189926fe1a8fb9014cb187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// BEGIN_COPYRIGHT -*- glean -*-
// 
// Copyright (C) 1999,2000  Allen Akin   All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL ALLEN AKIN BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// 
// END_COPYRIGHT




// geomrend.h:  convenience object for rendering any geometry via
// a host of OpenGL paths: immediate mode (glVertex), vertex
// arrays with glDrawArrays, vertex arrays with glArrayElement,
// vertex arrays with glDrawElements, and any of the preceding
// methods stuffed in a display list.

#ifndef __geomrend_h__
#define __geomrend_h__

#include "glwrap.h"
#include <cassert>

namespace GLEAN {

// A helper class to store parameter array data.
class ArrayData {
public:
    GLint size;
    GLenum type;
    GLsizei stride;
    const GLvoid* pointer;
    
    ArrayData();
    void setData(GLint sizeIn, GLenum typeIn, GLsizei strideIn, const GLvoid* pointerIn);
};

class GeomRenderer {
 public:
    // These indicate the methods of passing the primitive data to OpenGL.  Note that whether
    // the arrays are locked or not is an independent variable, not part of the method.  See
    // setArraysLocked and getArraysLocked.
    enum DrawMethod {GLVERTEX_MODE, GLARRAYELEMENT_MODE, GLDRAWARRAYS_MODE, GLDRAWELEMENTS_MODE};

    // Sorry, no indices, and especially no silly edge flags. There's no vertex bit because
    // vertex data always implicitly enabled (you can't draw anything without vertex data).
    enum ParameterBits {COLOR_BIT = 1, TEXTURE_COORD_BIT = 2, NORMAL_BIT = 4};

    // Only a default constructor.
    GeomRenderer();
    
    // Used to set the method by which this GeomRenderer will pass the primitive data to the GL.
    // Default is GLVERTEX_MODE.
    void setDrawMethod(DrawMethod);
    DrawMethod getDrawMethod() const;
    
    // Used to set the various parameters that are either enabled or disabled.  Example usage:
    // to tell the GeomRenderer to pass vertex, color, and texcoord data, but not normals,
    // call setParameterBits(COLOR_BIT | TEXTURE_COORD_BIT).  (Vertex data is implicitly enabled
    // all the time.)  The default is that only vertex data is enabled.
    void setParameterBits(GLuint bits);
    GLuint getParameterBits() const;
    
    // Used to specify whether EXT_compiled_vertex_array should be used if present.  Default is false.
    // If set to true, the arrays are kept unlocked and only locked just before rendering calls are issued.
    // If you call setArraysCompiled(true) and the extension is not present, the function returns false
    // and acts as though you had passed false in as the argument.
    bool setArraysCompiled(bool);
    bool getArraysCompiled() const;

    // If you're using GLDRAWELEMENTS_MODE, GLARRAYELEMENT_MODE, or GLVERTEX_MODE, you need to give
    // it the indices to pass into the GL.
    void setVArrayIndices(GLuint count, GLenum type, const GLvoid* indices);

    // This hands the actual primitive data to the GeomRenderer.  It holds onto these as pointers,
    // rather than copying them, so don't delete the data until you're done with the GeomRenderer.
    // These are prototypically equivalent to their respective GL calls, except that there's an extra
    // argument on the front of the vertex function for how many elements are in the array (this is
    // atomic; if you pass in 5, it means there are 5 vertices, not 5 floats or bytes or whatever).
    // The lengths of all other arrays are assumed to be >= the size passed in for the vertex array.
    void setVertexPointer(GLuint arrayLength, GLint size, GLenum type, GLsizei stride, const GLvoid* pointer);
    void setColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid* pointer);
    void setTexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid* pointer);
    void setNormalPointer(GLenum type, GLsizei stride, const GLvoid* pointer);

    // Finally, the actual calls to do something with all this data.  You can either choose to render
    // it given the configuration, or generate a display list of rendering it with the given
    // configuration (uses GL_COMPILE mode to build the list).  Fails if insufficient data has
    // been given (i.e. if you don't give it an array for an enabled parameter, if you don't
    // give it an array of indices when it needs them).
    bool renderPrimitives(GLenum mode);
    bool generateDisplayList(GLenum mode, GLint& listHandleOut);

 private:
    bool isReadyToRender();

    // Helper functions for unpacking and translating the data from the indices, vertices, colors,
    // texcoords, and normals arrays.
    GLuint getIndex(int);
    void sendVertex(GLuint index);
    void sendColor(GLuint index);
    void sendTexCoord(GLuint index);
    void sendNormal(GLuint index);

    DrawMethod drawMethod;
    GLuint parameterBits;
    bool compileArrays;
    
    GLuint indicesCount;
    GLenum indicesType;
    const GLvoid* indices;

    GLuint arrayLength;
    
    ArrayData vertexData;
    ArrayData colorData;
    ArrayData texCoordData;
    ArrayData normalData;
};
   
} // namespace GLEAN

#endif // __geomrend_h__