00001
00032 #include "linden_common.h"
00033
00034 #include "llglimmediate.h"
00035 #include "llvertexbuffer.h"
00036
00037 LLGLImmediate gGL;
00038
00039 const U32 immediate_mask = LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_COLOR | LLVertexBuffer::MAP_TEXCOORD;
00040
00041 LLGLImmediate::LLGLImmediate()
00042 {
00043 mCount = 0;
00044 mMode = LLVertexBuffer::TRIANGLES;
00045 mBuffer = new LLVertexBuffer(immediate_mask, 0);
00046 mBuffer->allocateBuffer(4096, 0, TRUE);
00047 mBuffer->getVertexStrider(mVerticesp);
00048 mBuffer->getTexCoordStrider(mTexcoordsp);
00049 mBuffer->getColorStrider(mColorsp);
00050 }
00051
00052 void LLGLImmediate::translatef(const GLfloat& x, const GLfloat& y, const GLfloat& z)
00053 {
00054 flush();
00055 glTranslatef(x,y,z);
00056 }
00057
00058 void LLGLImmediate::pushMatrix()
00059 {
00060 flush();
00061 glPushMatrix();
00062 }
00063
00064 void LLGLImmediate::popMatrix()
00065 {
00066 flush();
00067 glPopMatrix();
00068 }
00069
00070 void LLGLImmediate::blendFunc(GLenum sfactor, GLenum dfactor)
00071 {
00072 flush();
00073 glBlendFunc(sfactor, dfactor);
00074 }
00075
00076 void LLGLImmediate::begin(const GLuint& mode)
00077 {
00078 if (mode != mMode)
00079 {
00080 if (mMode == LLVertexBuffer::QUADS ||
00081 mMode == LLVertexBuffer::LINES ||
00082 mMode == LLVertexBuffer::TRIANGLES ||
00083 mMode == LLVertexBuffer::POINTS)
00084 {
00085 flush();
00086 }
00087 else if (mCount != 0)
00088 {
00089 llerrs << "gGL.begin() called redundantly." << llendl;
00090 }
00091
00092 mMode = mode;
00093 }
00094 }
00095
00096 void LLGLImmediate::end()
00097 {
00098 if (mCount == 0)
00099 {
00100 return;
00101
00102 }
00103
00104 if ((mMode != LLVertexBuffer::QUADS &&
00105 mMode != LLVertexBuffer::LINES &&
00106 mMode != LLVertexBuffer::TRIANGLES &&
00107 mMode != LLVertexBuffer::POINTS) ||
00108 mCount > 2048)
00109 {
00110 flush();
00111 }
00112 }
00113
00114 void LLGLImmediate::flush()
00115 {
00116 if (mCount > 0)
00117 {
00118 #if 0
00119 if (!glIsEnabled(GL_VERTEX_ARRAY))
00120 {
00121 llerrs << "foo 1" << llendl;
00122 }
00123
00124 if (!glIsEnabled(GL_COLOR_ARRAY))
00125 {
00126 llerrs << "foo 2" << llendl;
00127 }
00128
00129 if (!glIsEnabled(GL_TEXTURE_COORD_ARRAY))
00130 {
00131 llerrs << "foo 3" << llendl;
00132 }
00133
00134 if (glIsEnabled(GL_NORMAL_ARRAY))
00135 {
00136 llerrs << "foo 7" << llendl;
00137 }
00138
00139 GLvoid* pointer;
00140
00141 glGetPointerv(GL_VERTEX_ARRAY_POINTER, &pointer);
00142 if (pointer != &(mBuffer[0].v))
00143 {
00144 llerrs << "foo 4" << llendl;
00145 }
00146
00147 glGetPointerv(GL_COLOR_ARRAY_POINTER, &pointer);
00148 if (pointer != &(mBuffer[0].c))
00149 {
00150 llerrs << "foo 5" << llendl;
00151 }
00152
00153 glGetPointerv(GL_TEXTURE_COORD_ARRAY_POINTER, &pointer);
00154 if (pointer != &(mBuffer[0].uv))
00155 {
00156 llerrs << "foo 6" << llendl;
00157 }
00158 #endif
00159
00160 mBuffer->setBuffer(immediate_mask);
00161 mBuffer->drawArrays(mMode, 0, mCount);
00162
00163 mVerticesp[0] = mVerticesp[mCount];
00164 mTexcoordsp[0] = mTexcoordsp[mCount];
00165 mColorsp[0] = mColorsp[mCount];
00166 mCount = 0;
00167 }
00168 }
00169
00170 void LLGLImmediate::vertex3f(const GLfloat& x, const GLfloat& y, const GLfloat& z)
00171 {
00172 if (mCount >= 4096)
00173 {
00174
00175 return;
00176 }
00177
00178 mVerticesp[mCount] = LLVector3(x,y,z);
00179 mCount++;
00180 if (mCount < 4096)
00181 {
00182 mVerticesp[mCount] = mVerticesp[mCount-1];
00183 mColorsp[mCount] = mColorsp[mCount-1];
00184 mTexcoordsp[mCount] = mTexcoordsp[mCount-1];
00185 }
00186 }
00187
00188 void LLGLImmediate::vertex2i(const GLint& x, const GLint& y)
00189 {
00190 vertex3f((GLfloat) x, (GLfloat) y, 0);
00191 }
00192
00193 void LLGLImmediate::vertex2f(const GLfloat& x, const GLfloat& y)
00194 {
00195 vertex3f(x,y,0);
00196 }
00197
00198 void LLGLImmediate::vertex2fv(const GLfloat* v)
00199 {
00200 vertex3f(v[0], v[1], 0);
00201 }
00202
00203 void LLGLImmediate::vertex3fv(const GLfloat* v)
00204 {
00205 vertex3f(v[0], v[1], v[2]);
00206 }
00207
00208 void LLGLImmediate::texCoord2f(const GLfloat& x, const GLfloat& y)
00209 {
00210 mTexcoordsp[mCount] = LLVector2(x,y);
00211 }
00212
00213 void LLGLImmediate::texCoord2i(const GLint& x, const GLint& y)
00214 {
00215 texCoord2f((GLfloat) x, (GLfloat) y);
00216 }
00217
00218 void LLGLImmediate::texCoord2fv(const GLfloat* tc)
00219 {
00220 texCoord2f(tc[0], tc[1]);
00221 }
00222
00223 void LLGLImmediate::color4ub(const GLubyte& r, const GLubyte& g, const GLubyte& b, const GLubyte& a)
00224 {
00225 mColorsp[mCount] = LLColor4U(r,g,b,a);
00226 }
00227
00228 void LLGLImmediate::color4ubv(const GLubyte* c)
00229 {
00230 color4ub(c[0], c[1], c[2], c[3]);
00231 }
00232
00233 void LLGLImmediate::color4f(const GLfloat& r, const GLfloat& g, const GLfloat& b, const GLfloat& a)
00234 {
00235 color4ub((GLubyte) (llclamp(r, 0.f, 1.f)*255),
00236 (GLubyte) (llclamp(g, 0.f, 1.f)*255),
00237 (GLubyte) (llclamp(b, 0.f, 1.f)*255),
00238 (GLubyte) (llclamp(a, 0.f, 1.f)*255));
00239 }
00240
00241 void LLGLImmediate::color4fv(const GLfloat* c)
00242 {
00243 color4f(c[0],c[1],c[2],c[3]);
00244 }
00245
00246 void LLGLImmediate::color3f(const GLfloat& r, const GLfloat& g, const GLfloat& b)
00247 {
00248 color4f(r,g,b,1);
00249 }
00250
00251 void LLGLImmediate::color3fv(const GLfloat* c)
00252 {
00253 color4f(c[0],c[1],c[2],1);
00254 }
00255