TDME2  1.9.200
Texture2DRenderShader.cpp
Go to the documentation of this file.
2 
3 #include <tdme/tdme.h>
8 #include <tdme/engine/Engine.h>
9 
12 
21 
22 Texture2DRenderShader::Texture2DRenderShader(Renderer* renderer)
23 {
24  this->renderer = renderer;
25  initialized = false;
26  isRunning = false;
27 }
28 
30 {
31 }
32 
34 {
35  return initialized;
36 }
37 
39 {
40  auto shaderVersion = renderer->getShaderVersion();
43  "shader/" + shaderVersion + "/texture2D",
44  "render_vertexshader.vert"
45  );
46  if (vertexShaderId == 0) return;
47 
50  "shader/" + shaderVersion + "/texture2D",
51  "render_fragmentshader.frag"
52  );
53  if (fragmentShaderId == 0) return;
54 
61  }
62  if (renderer->linkProgram(programId) == false) return;
63 
64  // uniforms
66  if (uniformTextureUnit == -1) return;
67 
68  // create vbos
69  auto created = false;
70  auto vboManaged = Engine::getInstance()->getVBOManager()->addVBO("texture2d_render_shader.vbo", 2, false, false, created);
71  vboVertices = (*vboManaged->getVBOIds())[0];
72  vboTextureCoordinates = (*vboManaged->getVBOIds())[1];
73 
74  //
75  initialized = true;
76 }
77 
79 {
80  Engine::getInstance()->getVBOManager()->removeVBO("texture2d_render_shader.vbo");
81 }
82 
84 {
85  auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
86  renderer->useProgram(contextIdx, programId);
89  isRunning = true;
90 }
91 
93 {
94  isRunning = false;
95 }
96 
97 void Texture2DRenderShader::renderTexture(Engine* engine, const Vector2& position, const Vector2& dimension, int textureId, int width, int height) {
98  //
99  auto contextIdx = renderer->CONTEXTINDEX_DEFAULT;
100 
101  //
102  auto screenWidth = width != -1?width:(engine->getScaledWidth() == -1?engine->getWidth():engine->getScaledWidth());
103  auto screenHeight = height != -1?height:(engine->getScaledHeight() == -1?engine->getHeight():engine->getScaledHeight());
104  float textureLeft = position.getX();
105  float textureTop = position.getY();
106  float textureWidth = dimension.getX();
107  float textureHeight = dimension.getY();
108 
109  //
110  auto x0 = ((textureLeft) / (screenWidth / 2.0f)) - 1.0f;
111  auto y0 = ((screenHeight - textureTop) / (screenHeight / 2.0f)) - 1.0f;
112  auto x1 = ((textureLeft + textureWidth) / (screenWidth / 2.0f)) - 1.0f;
113  auto y1 = ((screenHeight - textureTop) / (screenHeight / 2.0f)) - 1.0f;
114  auto x2 = ((textureLeft + textureWidth) / (screenWidth / 2.0f)) - 1.0f;
115  auto y2 = ((screenHeight - textureTop - textureHeight) / (screenHeight / 2.0f)) - 1.0f;
116  auto x3 = ((textureLeft) / (screenWidth / 2.0f)) - 1.0f;
117  auto y3 = ((screenHeight - textureTop - textureHeight) / (screenHeight / 2.0f)) - 1.0f;
118 
119  // texture coordinates
120  {
121  auto fbTextureCoordinates = ObjectBuffer::getByteBuffer(contextIdx, 6 * 2 * sizeof(float))->asFloatBuffer();
122 
123  if (renderer->getRendererType() == Renderer::RENDERERTYPE_VULKAN) {
124  fbTextureCoordinates.put(+0.0f); fbTextureCoordinates.put(0.0f);
125  fbTextureCoordinates.put(+1.0f); fbTextureCoordinates.put(0.0f);
126  fbTextureCoordinates.put(+1.0f); fbTextureCoordinates.put(1.0f);
127 
128  fbTextureCoordinates.put(+1.0f); fbTextureCoordinates.put(1.0f);
129  fbTextureCoordinates.put(+0.0f); fbTextureCoordinates.put(1.0f);
130  fbTextureCoordinates.put(+0.0f); fbTextureCoordinates.put(0.0f);
131  } else {
132  fbTextureCoordinates.put(+0.0f); fbTextureCoordinates.put(+1.0f);
133  fbTextureCoordinates.put(+1.0f); fbTextureCoordinates.put(+1.0f);
134  fbTextureCoordinates.put(+1.0f); fbTextureCoordinates.put(0.0f);
135 
136  fbTextureCoordinates.put(+1.0f); fbTextureCoordinates.put(0.0f);
137  fbTextureCoordinates.put(+0.0f); fbTextureCoordinates.put(0.0f);
138  fbTextureCoordinates.put(+0.0f); fbTextureCoordinates.put(+1.0f);
139  }
140 
141  renderer->uploadBufferObject(contextIdx, vboTextureCoordinates, fbTextureCoordinates.getPosition() * sizeof(float), &fbTextureCoordinates);
142  }
143 
144  // vertices
145  {
146  auto fbVertices = ObjectBuffer::getByteBuffer(contextIdx, 6 * 3 * sizeof(float))->asFloatBuffer();
147 
148  fbVertices.put(x0); fbVertices.put(y0); fbVertices.put(0.0f);
149  fbVertices.put(x1); fbVertices.put(y1); fbVertices.put(0.0f);
150  fbVertices.put(x2); fbVertices.put(y2); fbVertices.put(0.0f);
151 
152  fbVertices.put(x2); fbVertices.put(y2); fbVertices.put(0.0f);
153  fbVertices.put(x3); fbVertices.put(y3); fbVertices.put(0.0f);
154  fbVertices.put(x0); fbVertices.put(y0); fbVertices.put(0.0f);
155 
156  renderer->uploadBufferObject(contextIdx, vboVertices, fbVertices.getPosition() * sizeof(float), &fbVertices);
157  }
158 
159  // disable culling
161  renderer->disableCulling(contextIdx);
162 
163  // use program
164  useProgram();
165 
166  // bind color buffer texture
167  renderer->setTextureUnit(contextIdx, 0);
168  renderer->bindTexture(contextIdx, textureId);
169 
170  //
173 
174  // draw
175  renderer->drawTrianglesFromBufferObjects(contextIdx, 2, 0);
176 
177  // unbind buffers
178  renderer->unbindBufferObjects(contextIdx);
179 
180  // unbind texture
181  renderer->bindTexture(contextIdx, renderer->ID_NONE);
182 
183  // unuse program
184  unUseProgram();
185 
186  // enabe culling
187  renderer->enableCulling(contextIdx);
189 
190 }
Engine main class.
Definition: Engine.h:131
static Engine * getInstance()
Returns engine instance.
Definition: Engine.h:612
int32_t getWidth()
Definition: Engine.h:1029
int32_t getHeight()
Definition: Engine.h:1036
static VBOManager * getVBOManager()
Definition: Engine.h:635
int32_t getScaledWidth()
Definition: Engine.h:1043
int32_t getScaledHeight()
Definition: Engine.h:1050
VBOManager_VBOManaged * addVBO(const string &vboId, int32_t ids, bool useGPUMemory, bool shared, bool &created)
Adds a VBO to manager or retrieve VBO if existing.
Definition: VBOManager.cpp:31
void removeVBO(const string &vboId)
Removes a VBO from manager.
Definition: VBOManager.cpp:73
virtual void setTextureUnit(int contextIdx, int32_t textureUnit)=0
Sets up texture unit.
virtual int32_t loadShader(int32_t type, const string &pathName, const string &fileName, const string &definitions=string(), const string &functions=string())=0
Loads a shader.
virtual void enableCulling(int contextIdx)=0
Enable culling.
virtual void unbindBufferObjects(int contextIdx)=0
Unbind buffer objects.
virtual int32_t createProgram(int type)=0
Creates a shader program.
virtual void setProgramAttributeLocation(int32_t programId, int32_t location, const string &name)=0
Bind attribute to a input location.
virtual void enableBlending()=0
Enables blending.
virtual void disableBlending()=0
Disables blending.
virtual void setProgramUniformInteger(int contextIdx, int32_t uniformId, int32_t value)=0
Set up a integer uniform value.
virtual bool linkProgram(int32_t programId)=0
Links attached shaders to a program.
virtual void bindTexture(int contextIdx, int32_t textureId)=0
Binds a texture with given id or unbinds when using ID_NONE.
void setLighting(int contextIdx, int32_t lighting)
Set current lighting model.
Definition: Renderer.h:504
virtual void attachShaderToProgram(int32_t programId, int32_t shaderId)=0
Attaches a shader to a program.
virtual int32_t getProgramUniformLocation(int32_t programId, const string &name)=0
Returns location of given uniform variable.
virtual void drawTrianglesFromBufferObjects(int contextIdx, int32_t triangles, int32_t trianglesOffset)=0
Draw triangles from buffer objects.
virtual void disableCulling(int contextIdx)=0
Disable culling.
virtual void uploadBufferObject(int contextIdx, int32_t bufferObjectId, int32_t size, FloatBuffer *data)=0
Uploads buffer data to buffer object.
virtual void bindTextureCoordinatesBufferObject(int contextIdx, int32_t bufferObjectId)=0
Bind texture coordinates buffer object.
virtual void useProgram(int contextIdx, int32_t programId)=0
Use shader program.
virtual void bindVerticesBufferObject(int contextIdx, int32_t bufferObjectId)=0
Bind vertices buffer object.
Buffers used to transfer data between main memory to graphics board memory.
Definition: ObjectBuffer.h:24
void renderTexture(Engine *engine, const Vector2 &position, const Vector2 &dimension, int textureId, int width=-1, int height=-1)
Render texture.
Vector2 class representing vector2 mathematical structure and operations with x, y components.
Definition: Vector2.h:20
float getY() const
Definition: Vector2.h:111
float getX() const
Definition: Vector2.h:94
Byte buffer class.
Definition: ByteBuffer.h:27
Float buffer class.
Definition: FloatBuffer.h:18