00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00033 #include "gl_graphic_core/gl_texture.h"
00034 #include "gl_graphic_core/gl_graphic_core.h"
00035
00036 #include "gpucalc/log_manager.h"
00037 #include "gpucalc/application.h"
00038 #include "gpucalc/framebuffer.h"
00039 #include "gpucalc/string_util.h"
00040 #include "gpucalc/graphic_core.h"
00041
00042
00043 #include <GL/glew.h>
00044
00045 namespace gpucalc
00046 {
00047 GLTexture::~GLTexture()
00048 {
00049 glDeleteTextures(1, &mGLTextureID);
00050 LogManager::getSingleton().logMessage(dynamic_cast<Bindable *>(this), "Texture ID deleted from OpenGL memory.", LML_Normal);
00051 }
00052
00053
00054 void GLTexture::initialize()
00055 {
00056 bind();
00057
00058 glTexParameteri(mGLTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00059 glTexParameteri(mGLTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00060 glTexParameteri(mGLTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP);
00061 glTexParameteri(mGLTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP);
00062
00063 glTexImage2D(mGLTextureTarget, 0, mGLTextureInternalFormat, mSizeX, mSizeY, 0, mGLTextureFormat, mGLTextureType, 0);
00064 mDefaultGraphicCore->checkErrors("GLTexture::setData");
00065 }
00066
00067
00068 void GLTexture::uploadToGPU()
00069 {
00070 if (!mIsUploadedToGPU)
00071 {
00072 switch (mDefaultGraphicCore->getVendor())
00073 {
00074 case NVIDIA:
00075 case MESA:
00076 case ATI:
00077 setNVData();
00078 break;
00079
00080
00081
00082
00083
00084 default:
00085 Except<ERR_NOT_IMPLEMENTED>(dynamic_cast<Bindable *>(this), "Uploading data for non-nvidia and non-ati cards is not implemented!", "GLTexture::setData()", __FILE__, __LINE__);
00086 break;
00087 }
00088 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00089 mIsUploadedToGPU = true;
00090 mDefaultGraphicCore->checkErrors("GLTexture::uploadToGPU()");
00091 }
00092 }
00093
00094
00095 void GLTexture::setNVData()
00096 {
00097 bind();
00098
00099 size_t RequestedLengthGPU = mSizeX * mSizeY;
00100
00101 if (mActualLengthGPU != RequestedLengthGPU)
00102 {
00103 Except<ERR_NOT_IMPLEMENTED>(dynamic_cast<Bindable *>(this), "Uploading non-rectangle data is not implemented!", "GLTexture::setNVData()", __FILE__, __LINE__);
00104
00105 Warning<ERR_INVALIDPARAMS>(dynamic_cast<Bindable *>(this), "We use some external bytes to upload non-rectangle data in GPU-side.", "GLTexture::setNVData()", __FILE__, __LINE__);
00106
00107 size_t NumberOfFullRows = mActualLengthGPU / mSizeX;
00108 size_t LengthOfLastRow = mActualLengthGPU - NumberOfFullRows * mSizeX;
00109
00111 glTexSubImage2D(mGLTextureTarget, 0, 0, 0, mSizeX, NumberOfFullRows, mGLTextureFormat, mGLTextureType, mGLPointer);
00112
00114 size_t PointerOffset = NumberOfFullRows * mSizeX * mNumberOfComponents * mTypeSize;
00115 glTexSubImage2D(mGLTextureTarget, 0, 0, NumberOfFullRows, LengthOfLastRow, 1, mGLTextureFormat, mGLTextureType,
00116 static_cast<char *>(mGLPointer) + PointerOffset);
00117 }
00118 else
00119 {
00120 glTexSubImage2D(mGLTextureTarget, 0, 0, 0, mSizeX, mSizeY, mGLTextureFormat, mGLTextureType, mGLPointer);
00121 LogManager::getSingleton().logMessage(dynamic_cast<Bindable *>(this), "OpenGL texture uploaded data using NVIDIA specific method. ID: " +
00122 auxillary::StringUtil::toString(mGLTextureID), LML_Trivial);
00123 }
00124 }
00125
00126
00127 void GLTexture::setATIData()
00128 {
00129 size_t RequestedLengthGPU = mSizeX * mSizeY;
00130
00131 if (mActualLengthGPU == RequestedLengthGPU)
00132 {
00133 FrameBuffer * Helper = mDefaultGraphicCore->getActiveFrameBuffer();
00134 Helper->bind();
00135 {
00136 Helper->drawBuffer(this);
00137 glRasterPos2i(0,0);
00138 glDrawPixels(mSizeX, mSizeY, mGLTextureFormat, mGLTextureType, mGLPointer);
00139 Helper->detach(this);
00140 }
00141 Helper->unbind();
00142 LogManager::getSingleton().logMessage(dynamic_cast<Bindable *>(this), "OpenGL texture uploaded data using ATI specific method. ID: " +
00143 auxillary::StringUtil::toString(mGLTextureID), LML_Trivial);
00144 }
00145 else
00146 {
00147 Except<ERR_NOT_IMPLEMENTED>(dynamic_cast<Bindable *>(this), "Uploading non-rectangle data is not implemented!", "GLTexture::setATIData()", __FILE__, __LINE__);
00148 }
00149
00150
00151 }
00152 }