Register
Handmade Hero»Forums»Code»stb_truetype generates red text
50 posts
stb_truetype generates red text
1 month, 1 week ago Edited by C_Worm on Feb. 26, 2020, 7:53 p.m.
Hey!

I managed to render a quad with all the glyphs from a font using stb_truetype.


However the color of the text is all red and i guess that's because the value is only coming in at the red channel.

but how can you make it alternate the color.

i have set all my color values to 1.0 but it doesn't matter.


here's my code:

main.cpp

  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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include "glad\glad.c"
#include "GLFW\glfw3.h"

#include <stdio.h>
#include <time.h>
#include <math.h>

#define STB_TRUETYPE_IMPLEMENTATION
#include "include\stb_truetype.h"

#define STB_IMAGE_IMPLEMENTATION
#include "include\stb_image.h"

#include <CJGL_Input.h>
#include "include\CJ_Vector.h"
#include "include\CJ_GameBuffer.h"
#include "include\CJ_Window.h"
#include "include\CJ_Shader.h"

#define SCR_WIDTH 1920
#define SCR_HEIGHT 1080

#define INDEX_SIZE_OFFSET 6 * sizeof(unsigned int)

#define NUMOBJECTS 50000
#define TILE_WIDTH 16
#define TILE_HEIGHT 16
#define NUMTILES_16_x 2
#define NUMTILES_16_y 4

#define nTILES NUMTILES_16_x * NUMTILES_16_y

#define GROUND_MAP 0
#define GROUND_TILE_FIRST 1
#define GROUND_TILE_LAST GROUND_TILE_FIRST + nTILES 

#define BUILD_MENU GROUND_TILE_LAST + 1



GLenum err;
bool running = true;

struct Time
{
	double startTime;
	double endTime;
	double dt_s;
	double dt_ms;

	double getMiliSeconds()
	{
		return dt_ms;
	}
	double getSeconds()
	{
		return dt_s;
	}

	void start()
	{
		dt_s = startTime - endTime;
		dt_ms = dt_s * 1000.0f;
		endTime = startTime;
		startTime = glfwGetTime();
	}
};


void updateVertexPosition(float *bufferData, GameBuffer gameBuffer);
void updateVertexColor(float *bufferData, GameBuffer gameBuffer);
void updateVertexTexCoord(float *bufferData, GameBuffer gameBuffer);

void map(float **pData, unsigned int VBO)
{
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	*pData = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
}
void unMap()
{
	glUnmapBuffer(GL_ARRAY_BUFFER);
}	

struct TextureInfo
{
	char *filePath;
	int width, height, nChannels;
};

void loadTextures(TextureInfo *texInfo, unsigned char **data)
{
	*data = stbi_load(texInfo->filePath, &texInfo->width, &texInfo->height, &texInfo->nChannels, 0);
	if(!data)
	{
		printf("Couldn't load texture file!\n");
	}
}

void draw(int startingIndex, int amountOfObjectsToDraw);

int main()
{
	printf("GROUND_TILE_FIRST: %d\n", GROUND_TILE_FIRST);
	printf("GROUND_TILE_LAST: %d\n", GROUND_TILE_LAST);
	printf("BUILD_MENU: %d\n", BUILD_MENU);

	srand(time(NULL));

	GLFWwindow *window = windowSetup(SCR_WIDTH, SCR_HEIGHT);
	unsigned int shaderProgram = compileShaderProgram();
	GLuint VAO, posVBO, colVBO, texVBO, IBO;
	GLfloat MAIN_GAMEBUFFER[NUMOBJECTS * 16] = {};

	GLuint indexBuffer[NUMOBJECTS * 6] = {};

	//0, 1, 2, 2, 3, 0    4, 5, 6, 6, 7, 4

	for(int i = 0; i < NUMOBJECTS; i++)
	{
		indexBuffer[0 + i * 6] = i * 4 + 0;
		indexBuffer[1 + i * 6] = i * 4 + 1;
		indexBuffer[2 + i * 6] = i * 4 + 2;
		indexBuffer[3 + i * 6] = i * 4 + 2;
		indexBuffer[4 + i * 6] = i * 4 + 3;
		indexBuffer[5 + i * 6] = i * 4 + 0;
	}

	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &posVBO);
	glGenBuffers(1, &colVBO);
	glGenBuffers(1, &texVBO);
	glGenBuffers(1, &IBO);

	glBindVertexArray(VAO);

	glBindBuffer(GL_ARRAY_BUFFER, posVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MAIN_GAMEBUFFER), 0, GL_STREAM_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

	glBindBuffer(GL_ARRAY_BUFFER, colVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MAIN_GAMEBUFFER), 0, GL_DYNAMIC_DRAW);

	glEnableVertexAttribArray(3);
	glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

	glBindBuffer(GL_ARRAY_BUFFER, texVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(MAIN_GAMEBUFFER), 0, GL_STREAM_DRAW);

	glEnableVertexAttribArray(7);
	glVertexAttribPointer(7, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexBuffer), indexBuffer, GL_STATIC_DRAW);

	// LOADING TEXTURE FILES
	unsigned char *data[3] = {};
	TextureInfo textureInfo[3] = {};
	textureInfo[0].filePath = "..\\assets\\mount.bmp";
	textureInfo[1].filePath = "..\\assets\\myPng.png";

	for(int i = 0; i < 2; i++)
	{
		loadTextures(&textureInfo[i], &data[i]);
	}

	stbtt_fontinfo *fontInfo = {};
	stbtt_bakedchar bakedCharData[37] = {};
	unsigned char fontBuffer[200000] = {};
	unsigned char fontData[SCR_WIDTH*SCR_HEIGHT] = {};
	FILE *hFontFile = fopen("C:\\windows\\fonts\\arial.ttf", "rb");
	if(!hFontFile)
	{
		printf("fail open file\n");
	}

	fread(fontBuffer, 1, sizeof(fontBuffer), hFontFile);

	stbtt_BakeFontBitmap(fontBuffer, 0, 126.0f, fontData, SCR_WIDTH, SCR_HEIGHT, 32, 69, bakedCharData);

	// LOADING TEXTURE ID'S
	GLuint textureID[3] = {};
	for(int i = 0; i < 3; i++)
	{
		glGenTextures(1, &textureID[i]);
	}

	glBindTexture(GL_TEXTURE_2D, textureID[0]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureInfo[0].width, textureInfo[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, data[0]);
	glGenerateMipmap(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, textureID[1]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo[1].width, textureInfo[1].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data[1]);
	glGenerateMipmap(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, textureID[2]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, SCR_WIDTH, SCR_HEIGHT, 0, GL_RED, GL_UNSIGNED_BYTE, fontData);

	stbtt_aligned_quad q = {};
	float x = 0.0f;
	float y = 0.0f;
	char *text = "a";
	stbtt_GetBakedQuad(bakedCharData, SCR_WIDTH, SCR_HEIGHT, *text-32, &x, &y, &q, 1); 

	stbi_image_free(data[0]);
	stbi_image_free(data[1]);
	stbi_image_free(fontData);

	double mouseCurPosX = 0;
	double mouseCurPosY = 0;
	char windowTitle[120] = {};
	float *pPosData = 0;
	float *pColData = 0;
	float *pTexData = 0;
	int winWidth = 0;
	int winHeight = 0;
	int winX = 0;
	int winY = 0;
	int iColorLoc = glGetUniformLocation(shaderProgram, "iColorData");
	int winDimLoc = glGetUniformLocation(shaderProgram, "windowDimensions");

	GameBuffer gameBuffer[NUMOBJECTS] = {};
	for(int i = 0; i < NUMOBJECTS; i++)
	{
		gameBuffer[i].bufferIndex = i;
	}



	gameBuffer[0].createObject(0.0f, 0.0f, SCR_WIDTH * 1, SCR_HEIGHT * 1);
	gameBuffer[0].setTextureDivision(1.0f, 1.0f);
	gameBuffer[0].pickTextureFragment(0.0f, 0.0f);
	gameBuffer[0].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f); 
	

	gameBuffer[BUILD_MENU].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f); 
	gameBuffer[BUILD_MENU].createObject(0.0f, 0.0f, 64, 100.0f);

	gameBuffer[BUILD_MENU + 1].setSolidColor(1.0f, 1.0f, 1.0f, 0.5f); 
	gameBuffer[BUILD_MENU + 1].createObject(500.0f, 500.0f, 80.0f, 100.0f);


	gameBuffer[BUILD_MENU].setTextureDivision(14.0f, 1.0f);
	gameBuffer[BUILD_MENU].pickTextureFragment(1.0f, 0.0f);
	gameBuffer[BUILD_MENU + 1].setTextureDivision(14.0f, 1.0f);
	gameBuffer[BUILD_MENU + 1].pickTextureFragment(1.0f, 0.0f);

	for ( int y = 0; y < NUMTILES_16_y; y++)
	{
		for ( int x = 0; x < NUMTILES_16_x; x++)
		{
			static int index = 0;
			static int tileMapIndex = 0;
			if(tileMapIndex == 8)
			{
				tileMapIndex = 0;
			}
			gameBuffer[GROUND_TILE_FIRST + index].createObject(200.0f + x * TILE_WIDTH, 200.0f +  y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
			gameBuffer[GROUND_TILE_FIRST + index].setTextureDivision(14.0f, 1.0f);
			gameBuffer[GROUND_TILE_FIRST + index].pickTextureFragment(3.0f, 0.0f);
			gameBuffer[GROUND_TILE_FIRST + index].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f);

			tileMapIndex++;
			index++;
		}
	}


	float texPosX = 0.0f;
	float texPosY = 0.0f;

	glUseProgram(shaderProgram);
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	printf("INDEX_SIZE_OFFSET: %lld\n", 6 * sizeof(unsigned char) );
	printf("GROUND_TILE_LAST: %lld\n", GROUND_TILE_LAST );
	printf("BUILD_MENU: %lld\n", BUILD_MENU );
	printf("GROUND_TILE_LAST * INDEX_SIZE_OFFSET: %lld\n", GROUND_TILE_LAST * INDEX_SIZE_OFFSET);
	printf("BUILD_MENU * INDEX_SIZE_OFFSET: %lld\n", (BUILD_MENU) * INDEX_SIZE_OFFSET);

	printf("\n gameBuffer[GROUND_TILE_FIRST].getX():  %0.2f\n", 	 gameBuffer[GROUND_TILE_FIRST + 1].getX());
	printf("\n gameBuffer[GROUND_TILE_FIRST].getWidth():  %0.2f\n",  gameBuffer[GROUND_TILE_FIRST + 1].getWidth());
	printf("\n gameBuffer[GROUND_TILE_FIRST].getY():  %0.2f\n", 	 gameBuffer[GROUND_TILE_FIRST + 1].getY());
	printf("\n gameBuffer[GROUND_TILE_FIRST].getHeight():  %0.2f\n", gameBuffer[GROUND_TILE_FIRST + 1].getHeight());

	Time time = {};

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


	while(!glfwWindowShouldClose(window) && running) 
	{
		time.start();

		glfwGetWindowSize(window, &winWidth, &winHeight);
		glUniform2f(winDimLoc, (float)winWidth, (float)winHeight);
		checkInput(window);

		glClearColor(0.0f, 0.2f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glfwGetCursorPos(window, &mouseCurPosX, &mouseCurPosY);
		//sprintf(windowTitle, "CJGL Game - MouseCursor: %4.3f, %4.3f - Window (width/height): %d/%d - FrameTime: %0.3f - winX %d, winY %d",
		//	mouseCurPosX, mouseCurPosY, 
		//	winWidth, winHeight, 
		//	time.getMiliSeconds(), winX, winY);

		//glfwSetWindowTitle(window, windowTitle);

		glViewport(0.0f, 0.0f, winWidth, winHeight);
		// arg 3 & 4 bestämmer storlek på "kartan"
		// ändra bara x och y för att flytta viewporten
		//glViewport(winX, winY, 5760, 3240);
		
		if(mouseCurPosX >= winWidth - 1)
		{
			winX -= 1000;
		}
		if(mouseCurPosY >= winHeight + 1)
		{
			winY += 1000;
		}
		if(mouseCurPosX <= 1)
		{
			winX += 1000;
		}
		if(mouseCurPosY <= 1)
		{
			winY -= 1000;
		}

		if(keyPressed[LEFT_MOUSE_BUTTON])
		{
			for(int i = 0; i < (GROUND_TILE_LAST); i++)
			{
				if(  ((mouseCurPosX - winX) >= gameBuffer[GROUND_TILE_FIRST + i].getX()) &&
				     ((mouseCurPosX - winX) <= (gameBuffer[GROUND_TILE_FIRST + i].getX() + gameBuffer[GROUND_TILE_FIRST + i].getWidth())) &&
				     ((mouseCurPosY + winY) >= gameBuffer[GROUND_TILE_FIRST + i].getY()) &&
				     ((mouseCurPosY + winY) <= (gameBuffer[GROUND_TILE_FIRST + i].getY() + gameBuffer[GROUND_TILE_FIRST + i].getHeight()))
				  )
				{
					gameBuffer[GROUND_TILE_FIRST + i].pickTextureFragment(5.0f, 0.0f);
				}
			}
		}

		for(int i = 0; i < (GROUND_TILE_LAST); i++)
		{
			gameBuffer[i].setX(gameBuffer[i].getX() + winX * time.getSeconds());
			gameBuffer[i].setY(gameBuffer[i].getY() - winY * time.getSeconds());
		}
		winX = 0.0f;
		winY = 0.0f;

		gameBuffer[BUILD_MENU].setX(-winX);
		gameBuffer[BUILD_MENU].setY(winY);


		if(keyPressed[LEFT_MOUSE_BUTTON])
		{
		}
		if(keyPressed[RIGHT_MOUSE_BUTTON])
		{
		}

		if(keyPressed[ESC])
		{
			running = false;
		}

		if(keyPressed['W'])
		{
		}
		else
		{
		}

		if(keyPressed['S'])
		{
		}
		if(keyPressed['A'])
		{
		}
		if(keyPressed['D'])
		{
		}

		map(&pColData, colVBO);
		for(int i = 0; i < NUMOBJECTS; i++)
		{
			updateVertexColor(pColData, gameBuffer[i]);
		}
		unMap();

		map(&pTexData, texVBO);
		for(int i = 0; i < NUMOBJECTS; i++)
		{
			updateVertexTexCoord(pTexData, gameBuffer[i]);
		}
		unMap();

		map(&pPosData, posVBO);
		for(int i = 0; i < NUMOBJECTS; i++)
		{
			updateVertexPosition(pPosData, gameBuffer[i]);
		}
		unMap();



		glBindTexture(GL_TEXTURE_2D, textureID[2]);
		draw(0, 1);

		glBindTexture(GL_TEXTURE_2D, textureID[0]);
		draw(GROUND_TILE_FIRST, GROUND_TILE_LAST);

		glBindTexture(GL_TEXTURE_2D, textureID[0]);
		// BUILDING FIRST MÅSTE VA I PARENTES FÖR TÄNK PÅ VAD #define expanderar till för något 
		draw(BUILD_MENU, 2);


		glfwSwapBuffers(window);
		glfwPollEvents();
	}


	for(int i = 0; i < sizeof(textureID) / sizeof(GLuint); i++)
	{
		glDeleteTextures(1, &textureID[i]);
	}

	glDeleteProgram(shaderProgram);
	glDeleteBuffers(1, &posVBO);
	glDeleteBuffers(1, &colVBO);
	glDeleteBuffers(1, &texVBO);
	glDeleteBuffers(1, &IBO);
	glDeleteVertexArrays(1, &VAO);
	
	glfwDestroyWindow(window);
	glfwTerminate();


	return 0;
}

void draw(int startingIndex, int amountOfObjectsToDraw)
{	
	int indexSizeOffset = 6 * sizeof(unsigned int); 
	glDrawElements(GL_TRIANGLES, ((amountOfObjectsToDraw + 0) * 6) , GL_UNSIGNED_INT, (void*)((startingIndex) * indexSizeOffset));
}

void updateVertexPosition(float *bufferData, GameBuffer gameBuffer)
{
	bufferData[0  + gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[0];
	bufferData[4  + gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[4];
	bufferData[8  + gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[8];
	bufferData[12 + gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[12];

	bufferData[1 +  gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[1];
	bufferData[5 +  gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[5];
	bufferData[9 +  gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[9];
	bufferData[13 + gameBuffer.bufferIndex * 16]  = gameBuffer.objPosData[13];

}

void updateVertexColor(float *bufferData, GameBuffer gameBuffer)
{
	bufferData[0  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[0];
	bufferData[1  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[1];
	bufferData[2  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[2];
	bufferData[3  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[3];
                                                                               
	bufferData[4  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[4];
	bufferData[5  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[5];
	bufferData[6  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[6];
	bufferData[7  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[7];
                                                                               
	bufferData[8  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[8];
	bufferData[9  + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[9];
	bufferData[10 + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[10];
	bufferData[11 + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[11];
                                                                               
	bufferData[12 + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[12];
	bufferData[13 + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[13];
	bufferData[14 + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[14];
	bufferData[15 + gameBuffer.bufferIndex * 16]  = gameBuffer.objColData[15];

}

void updateVertexTexCoord(float *bufferData, GameBuffer gameBuffer)
{
	bufferData[0  + gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[0];
	bufferData[1 +  gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[1];

	bufferData[4  + gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[4];
	bufferData[5 +  gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[5];

	bufferData[8  + gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[8];
	bufferData[9 +  gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[9];

	bufferData[13 + gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[13];
	bufferData[12 + gameBuffer.bufferIndex * 16]  = gameBuffer.objTexData[12];
}



Gamebuffer.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
struct GameBuffer
{
	unsigned int bufferIndex;

	float x;
	float y;
	float w;
	float h;

	float texDenomX;
	float texDenomY;

	float objPosData[16];
	float objColData[16];
	float objTexData[16];

	void setBufferIndex(unsigned int index)
	{
		bufferIndex = index;
	}

	// POSITIONS

	void createObject(float inX, float inY, float inW, float inH)
	{
		x = inX;
		y = inY;
		w = inW;
		h = inH;

		objPosData[0]  = x ;
		objPosData[4]  = x ;
		objPosData[8]  = x + w ;
		objPosData[12]  = x + w ;
	
		objPosData[1]  = y ;
		objPosData[5]  = y + h ;
		objPosData[9]  = y + h ;
		objPosData[13]  = y ;
	}

	void setX(float inX)
	{
		x = inX;

		objPosData[0]  = x ;
		objPosData[4]  = x ;
		objPosData[8]  = x + w ;
		objPosData[12]  = x + w ;

	}

	void setY(float inY)
	{
		y = inY;

		objPosData[1]   = y ;
		objPosData[5]   = y + h;
		objPosData[9]   = y + h;
		objPosData[13]  = y ;

	}

	float getX()
	{
		return x;
	}

	float getY()
	{
		return y;
	}

	float getWidth()
	{
		return w;
	}

	float getHeight()
	{
		return h;
	}

	// COLORS
	void setSolidColor(float r,float g,float b,float a)
	{
		objColData[0]  = r;
		objColData[1]  = g;
		objColData[2]  = b;
		objColData[3]  = a;

		objColData[4]  = r;
		objColData[5]  = g;
		objColData[6]  = b;
		objColData[7]  = a;

		objColData[8]  = r;
		objColData[9]  = g;
		objColData[10] = b;
		objColData[11] = a;

		objColData[12] = r;
		objColData[13] = g;
		objColData[14] = b;
		objColData[15] = a;
	}



	// TEXTURES

	void setTextureDivision(float inTexDenom_X, float inTexDenom_Y)
	{
		texDenomX = 1.0f / inTexDenom_X;
		texDenomY = 1.0f / inTexDenom_Y;
		
	}

	void pickTextureFragment(float texFragX, float texFragY)
	{
		// up left
		objTexData[0]  =  0.0f + (texFragX * texDenomX); 
		objTexData[1]  =  0.0f + (texFragY * texDenomY); 

		// down left
		objTexData[4]  =  0.0f + (texFragX * texDenomX);
		objTexData[5]  =  texDenomY + (texFragY * texDenomY);

		// down right
		objTexData[8]  =  texDenomX + (texFragX * texDenomX);
		objTexData[9]  =  texDenomY + (texFragY * texDenomY);

		// up right
		objTexData[12] =  texDenomX + (texFragX * texDenomX);
		objTexData[13] =  0.0f + (texFragY * texDenomY);

	}
};


vertexShader.glsl

 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
#version 330 core

layout (location = 0) in vec3 inPos;
layout (location = 3) in vec4 inCol;
layout (location = 7) in vec2 inTex;

out vec4 colorVS;
out vec2 texCoord;

uniform vec2 windowDimensions;


void main()
{

	vec4 vertexPosition;
	vertexPosition.xyz = inPos.xyz;
	vertexPosition.x = (2 * vertexPosition.x / windowDimensions.x) - 1.0f;
	vertexPosition.y = 1.0f - ( 2 * vertexPosition.y / windowDimensions.y);
	vertexPosition.z  = 0.0f;
	vertexPosition.w  = 1.0f;

	gl_Position = vertexPosition;

	texCoord = inTex;
	colorVS.r = inCol.r;
	colorVS.g = inCol.g;
	colorVS.b = inCol.b;
	colorVS.a = inCol.a;
}




fragmentShader.glsl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#version 330 core

in vec4 colorVS;
in vec2 texCoord;
out vec4 fragmentColor;

uniform vec4 iColorData;
uniform sampler2D ourTexture;

void main()
{
	
	vec4 result_Color = colorVS;

	fragmentColor = texture(ourTexture, texCoord) * result_Color;

}
Simon Anciaux
742 posts
stb_truetype generates red text
1 month, 1 week ago
I haven't look precisely but you're sending a texture with only the red channel to the GPU. So when you sample the texture with "texture(ourTexture, texCoord)", you will get a texel with a value of "vec4( red, 0, 0, 1)". So you need to change your fragment shader to create a value that uses the red channel as the alpha and the rest is white: vec4( 1, 1, 1, red ). You will probably need to have a specialized fragment shader for the text.

For example:
1
2
3
4
5
void main()
{
	vec4 result_Color = colorVS;
	fragmentColor = vec4( 1, 1, 1, texture(ourTexture, texCoord).r ) * result_Color;
}


Another way would be to send a 4 channel white texture with the alpha containing the right information, but it would waste 3/4 of the texture memory.
Mārtiņš Možeiko
2040 posts / 1 project
stb_truetype generates red text
1 month, 1 week ago Edited by Mārtiņš Možeiko on Feb. 27, 2020, 3:33 a.m.
Alternative would be to use ARB_texture_swizzle extension (in core since v3.3):

1
2
GLint swizzle[] = { GL_ONE, GL_ONE, GL_ONE, GL_RED };
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle);


This code will make GLSL to produce (1,1,1,r) value whenever you are sampling texture that has only one channel (GL_RED).