1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 /* OpenGLMaterial:
14  *  Encapsulates an OpenGL material.
15  *
16  * All materials are for front and back faces.
17  * OpenGLMaterial reference counts so copying or assigning a
18  * material keeps the same display list until the object is
19  * changed.
20  *
21  * operator==() returns true iff the two objects refer to the
22  * same display list.  operator!=() returns true iff the two
23  * objects do not refer to the same display list.  Materials
24  * that don't refer to the same display list but have exactly
25  * the same colors and shininess compare not-equal.
26  *
27  * <, <=, >, >= define an arbitrary ordering of materials.
28  */
29 
30 #ifndef BZF_OPENGL_MATERIAL_H
31 #define BZF_OPENGL_MATERIAL_H
32 
33 #include "common.h"
34 #include "bzfgl.h"
35 
36 class OpenGLMaterial
37 {
38 public:
39     OpenGLMaterial();
40     OpenGLMaterial(const GLfloat* specularRGB,
41                    const GLfloat* emissiveRGB,
42                    GLfloat shininess = 0.0f);
43     OpenGLMaterial(const OpenGLMaterial&);
44     ~OpenGLMaterial();
45     OpenGLMaterial& operator=(const OpenGLMaterial&);
46 
47     bool        operator==(const OpenGLMaterial&) const;
48     bool        operator!=(const OpenGLMaterial&) const;
49     bool        operator<(const OpenGLMaterial&) const;
50 
51     const GLfloat*  getSpecularColor() const;
52     const GLfloat*  getEmissiveColor() const;
53     GLfloat     getShininess() const;
54 
55     bool        isValid() const;
56     void        execute() const;
57 
58 private:
59     class Rep
60     {
61     public:
62         ~Rep();
63         void        ref();
64         void        unref();
65         void        execute();
66         static Rep* getRep(const GLfloat* specular,
67                            const GLfloat* emissive,
68                            GLfloat shininess);
69     private:
70         Rep(const GLfloat* specular,
71             const GLfloat* emissive,
72             GLfloat shininess);
73         static void freeContext(void*);
74         static void initContext(void*);
75     public:
76         int     refCount;
77         Rep*        prev;
78         Rep*        next;
79         GLuint      list;
80         GLfloat     specular[4];
81         GLfloat     emissive[4];
82         GLfloat     shininess;
83         static Rep* head;
84     };
85     Rep*        rep;
86 };
87 
88 //
89 // OpenGLMaterial
90 //
91 
getSpecularColor()92 inline const GLfloat*   OpenGLMaterial::getSpecularColor() const
93 {
94     return rep->specular;
95 }
96 
getEmissiveColor()97 inline const GLfloat*   OpenGLMaterial::getEmissiveColor() const
98 {
99     return rep->emissive;
100 }
101 
getShininess()102 inline GLfloat      OpenGLMaterial::getShininess() const
103 {
104     return rep->shininess;
105 }
106 
107 #endif // BZF_OPENGL_MATERIAL_H
108 
109 // Local Variables: ***
110 // mode: C++ ***
111 // tab-width: 4 ***
112 // c-basic-offset: 4 ***
113 // indent-tabs-mode: nil ***
114 // End: ***
115 // ex: shiftwidth=4 tabstop=4
116