-
Notifications
You must be signed in to change notification settings - Fork 5
/
BenthosOpenGLESRenderer.h
156 lines (128 loc) · 5.74 KB
/
BenthosOpenGLESRenderer.h
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
147
148
149
150
151
152
153
154
155
156
#ifndef GLSLRENDER_H
#define GLSLRENDER_H
//
// BenthosOpenGLESRenderer.h
// SeafloorExplore
//
// Modified from Brad Larson's Molecules Project in 2011-2012 for use in The SeafloorExplore Project
//
// Copyright (C) 2012 Matthew Johnson-Roberson
//
// See COPYING for license details
//
//
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#import "Benthos.h"
extern NSString *const kBenthosShadowCalculationStartedNotification;
extern NSString *const kBenthosShadowCalculationUpdateNotification;
extern NSString *const kBenthosShadowCalculationEndedNotification;
#define MAX_BOND_VBOS 2
// OpenGL helper functions
void normalize(GLfloat *v);
typedef struct {
GLubyte redComponent;
GLubyte greenComponent;
GLubyte blueComponent;
GLfloat atomRadius;
} BenthosAtomProperties;
// van der Waals radius used here
// http://www.umass.edu/microbio/rasmol/rasbonds.htm
static const BenthosAtomProperties atomProperties[NUM_ATOMTYPES] = {
{120, 120, 120, 1.55f}, // CARBON
{230, 230, 230, 1.10f}, // HYDROGEN
{240, 40, 40, 1.35f}, // OXYGEN
{ 48, 80, 248, 1.40f}, // NITROGEN
{255, 255, 48, 1.81f}, // SULFUR
{255, 128, 0, 1.88f}, // PHOSPHOROUS
{224, 102, 51, 1.95f}, // IRON
{ 0, 255, 0, 1.50f}, // UNKNOWN
{200, 200, 90, 1.50f}, // SILICON
{144, 224, 80, 1.47f}, // FLUORINE
{ 31, 240, 31, 1.75f}, // CHLORINE
{166, 41, 41, 1.85f}, // BROMINE
{148, 0, 148, 1.75f}, // IODINE
{ 61, 255, 0, 1.95f}, // CALCIUM
{125, 128, 176, 1.15f}, // ZINC
{255, 217, 143, 1.75f}, // CADMIUM
{171, 92, 242, 1.02f}, // SODIUM
{138, 255, 0, 0.72f}, // MAGNESIUM
};
@interface BenthosOpenGLESRenderer : NSObject
{
GLint backingWidth;
GLint backingHeight;
CATransform3D currentCalculatedMatrix;
BOOL isFirstDrawingOfModel, isFrameRenderingFinished, isSceneReady, isRenderingCancelled;
float atomRadiusScaleFactor, bondRadiusScaleFactor, overallModelScaleFactor;
float currentModelScaleFactor;
EAGLContext *context;
GLuint viewRenderbuffer, viewFramebuffer, viewDepthBuffer;
// OpenGL performance tuning statistics
NSInteger totalNumberOfVertices, totalNumberOfTriangles;
// Binned atom types
// 16384 atoms per indexed VBO per atom type
// 16384 bonds per indexed VBO
NSMutableData *atomVBOs[NUM_ATOMTYPES], *atomIndexBuffers[NUM_ATOMTYPES];
GLuint atomVertexBufferHandles[NUM_ATOMTYPES], atomIndexBufferHandle[NUM_ATOMTYPES], numberOfIndicesInBuffer[NUM_ATOMTYPES];
GLuint bondVertexBufferHandle[MAX_BOND_VBOS], bondIndexBufferHandle[MAX_BOND_VBOS], numberOfBondIndicesInBuffer[MAX_BOND_VBOS];
unsigned int numberOfAtomVertices[NUM_ATOMTYPES], numberOfBondVertices[MAX_BOND_VBOS], numberOfAtomIndices[NUM_ATOMTYPES], numberOfBondIndices[MAX_BOND_VBOS];
NSMutableData *bondVBOs[MAX_BOND_VBOS], *bondIndexBuffers[MAX_BOND_VBOS];
unsigned int currentBondVBO;
dispatch_queue_t openGLESContextQueue;
dispatch_semaphore_t frameRenderingSemaphore;
}
@property(readwrite, retain, nonatomic) EAGLContext *context;
@property(readwrite) BOOL isFrameRenderingFinished, isSceneReady;
@property(readonly) NSInteger totalNumberOfVertices, totalNumberOfTriangles;
@property(readwrite, nonatomic) float atomRadiusScaleFactor, bondRadiusScaleFactor, overallModelScaleFactor;
@property(readonly) dispatch_queue_t openGLESContextQueue;
// Initialization and teardown
- (id)initWithContext:(EAGLContext *)newContext;
// OpenGL matrix helper methods
- (void)convertMatrix:(GLfloat *)matrix to3DTransform:(CATransform3D *)transform3D;
- (void)convert3DTransform:(CATransform3D *)transform3D toMatrix:(GLfloat *)matrix;
- (void)convert3DTransform:(CATransform3D *)transform3D to3x3Matrix:(GLfloat *)matrix;
- (void)print3DTransform:(CATransform3D *)transform3D;
- (void)printMatrix:(GLfloat *)fixedPointMatrix;
- (void)apply3DTransform:(CATransform3D *)transform3D toPoint:(GLfloat *)sourcePoint result:(GLfloat *)resultingPoint;
// Model manipulation
- (void)rotateModelFromScreenDisplacementInX:(float)xRotation inY:(float)yRotation;
- (void)scaleModelByFactor:(float)scaleFactor;
- (void)translateModelByScreenDisplacementInX:(float)xTranslation inY:(float)yTranslation;
- (void)resetModelViewMatrix;
// OpenGL drawing support
- (BOOL)createFramebuffersForLayer:(CAEAGLLayer *)glLayer;
- (void)destroyFramebuffers;
- (void)configureLighting;
- (void)clearScreen;
- (void)startDrawingFrame;
- (void)configureProjection;
- (void)presentRenderBuffer;
// Actual OpenGL rendering
- (void)renderFrameForModel:(BenthosModel *)model;
// Model 3-D geometry generation
- (void)configureBasedOnNumberOfAtoms:(unsigned int)numberOfAtoms numberOfBonds:(unsigned int)numberOfBonds;
- (void)addVertex:(GLfloat *)newVertex forAtomType:(BenthosAtomType)atomType;
- (void)addIndex:(GLushort *)newIndex forAtomType:(BenthosAtomType)atomType;
- (void)addIndices:(GLushort *)newIndices size:(unsigned int)numIndices forAtomType:(BenthosAtomType)atomType;
- (void)addBondVertex:(GLfloat *)newVertex;
- (void)addBondIndex:(GLushort *)newIndex;
- (void)addBondIndices:(GLushort *)newIndices size:(unsigned int)numIndices;
- (void)addAtomToVertexBuffers:(BenthosAtomType)atomType atPoint:(Benthos3DPoint)newPoint;
- (void)addBondToVertexBuffersWithStartPoint:(Benthos3DPoint)startPoint endPoint:(Benthos3DPoint)endPoint bondColor:(GLubyte *)bondColor bondType:(BenthosBondType)bondType;
// OpenGL drawing routines
- (void)bindVertexBuffersForModel;
- (void)drawModel;
- (void)freeVertexBuffers;
- (void)initiateModelRendering;
- (void)terminateModelRendering;
- (void)cancelModelRendering;
- (void)waitForLastFrameToFinishRendering;
@end
#endif