Handmade Hero»Forums»Code
107 posts
why no rectangle??
Edited by C_Worm on
Basic program trying to render a rectangle with indices.

I've checked my vertexattribs many times now, and can't figure out why my rectangle isn't showing!

maybe the problem is somewhere else, but i really can't find it.

Have check with glGetError() but doesn't get any errors.



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

layout (location = 0) in vec3 iPos;
layout (location = 1) in vec4 iCol;
layout (location = 2) in vec2 iTexCoords;


void main()
{
	vec4 position;
    position.x = iPos.x;
    position.y = iPos.y;
    position.z  = 1.0f;
    position.w  = 0.0f;

	gl_Position = position; 
}


FS_SolidColor.glsl
1
2
3
4
5
6
7
8
#version 330 core

out vec4 fragResult;

void main()
{
	fragResult = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}



Textrenderer.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
#include <stdio.h>
#include "glad\glad.c"
#include "GLFW\glfw3.h"


#define MAX_ENTITIES 2000
#define POS_ATTRIBS (12 * MAX_ENTITIES)
#define COL_ATTRIBS (16 * MAX_ENTITIES)
#define TEX_ATTRIBS (8 * MAX_ENTITIES)
#define MAX_ATTRIBS ((POS_ATTRIBS) + (COL_ATTRIBS) + (TEX_ATTRIBS))

bool running = true;

struct V2f
{
    float x;
    float y;
};

struct V3f
{
    float x;
    float y;
    float z;
};

struct V4f
{
    float x;
    float y;
    float z;
    float w;
};

struct Vertex
{
    
    V3f pos;
     V4f col;
     V2f texCoord;
};

struct Entity
{
    V2f pos;
    V2f dimension;
    
    Vertex vtx[4];
    /*

vtx[0]:
{ 0.0f, 0.0f, 0.0f,    0.0f, 0.0.f, 0.0f, 0.0f,     0.0f 0.0f, }
vtx[1]:
{ 0.0f, 0.5f, 0.0f,    0.0f, 0.0.f, 0.0f, 0.0f,     0.0f 0.0f, }
vtx[2]:
{ 0.5f, 0.0f, 0.0f,    0.0f, 0.0.f, 0.0f, 0.0f,     0.0f 0.0f, }
vtx[3]:
{ 0.5f, 0.5f, 0.0f,    0.0f, 0.0.f, 0.0f, 0.0f,     0.0f 0.0f, }

*/
    void Init(float _x, float _y, float _w, float _h)
    {
        pos.x = _x;
        pos.y = _y;
        dimension.x = _w;
        dimension.y = _h;
        
        vtx[0].pos.x = pos.x;
        vtx[0].pos.y = pos.y;
        
        vtx[1].pos.x = pos.x;
        vtx[1].pos.y = pos.y + dimension.y;
        
        vtx[2].pos.x = pos.x + dimension.x;
        vtx[2].pos.y = pos.y;
        
        vtx[3].pos.x = pos.x + dimension.x;
        vtx[3].pos.y = pos.y + dimension.y;
        
    }
};



typedef enum 
{ 
    windowed = 0, fullscreen = 1 
}WINDOW_MODE;

GLFWwindow *InitGLFW(unsigned int winWidth,unsigned int winHeight, char *winTitle, WINDOW_MODE windowMode);

unsigned int LoadShaderProgram(char *path_VS, char *path_FS);




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
#include "include\TextRenderer.h"

int main( )
{
    GLFWwindow *window = InitGLFW(1080, 600, "TextRenderer", windowed);
    
    
    unsigned int shaderProgram_SolidColor = LoadShaderProgram("shader\\VS.glsl", "shader\\FS_SolidColor.glsl");
    
    
    unsigned int vtxIndices[MAX_ENTITIES * 6] = {};
    
    // 0, 1, 2, 1, 2, 3
    for(int i = 0; i < MAX_ENTITIES; i++)
    {
        vtxIndices[(i * 6) +  0] = (i * 4) + 0;
        vtxIndices[(i * 6) +  1] = (i * 4) + 1;
        vtxIndices[(i * 6) +  2] = (i * 4) + 2;
        vtxIndices[(i * 6) +  3] = (i * 4) + 1;
        vtxIndices[(i * 6) +  4] = (i * 4) + 2;
        vtxIndices[(i * 6) +  5] = (i * 4) + 3; 
    }
    unsigned int VBO;
    unsigned int VAO;
    unsigned int IBO;
    
    
    unsigned int MaxMemory = sizeof(float) * MAX_ATTRIBS;
    unsigned int MemoryOffset_Col = sizeof(float) * POS_ATTRIBS;
    unsigned int MemoryOffset_Tex = MemoryOffset_Col + (sizeof(float) * COL_ATTRIBS);
    
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &IBO);
    glGenVertexArrays(1, &VAO);
    
    glBindVertexArray(VAO);
    
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, MaxMemory, 0, GL_DYNAMIC_DRAW);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)0);
    
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, false, sizeof(Vertex), (void*)sizeof(V3f));
    
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex),  (void*)(sizeof(V3f) + sizeof(V4f)));
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vtxIndices), vtxIndices, GL_STATIC_DRAW);
    
    Entity entity[2000] = {};
    entity[0].Init(0.0f, 0.0f, -0.5f, -0.5f);
    
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferSubData(GL_ARRAY_BUFFER, 0, 4 * sizeof(Vertex), entity[0].vtx);
    
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glUseProgram(shaderProgram_SolidColor);
    
    GLenum err;
    
    
    while(running && !glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0f, 0.4f, 0.f, 1.0f);
        glViewport(0, 0, 1080, 600);
        
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
        
        while((err = glGetError()) != GL_NO_ERROR)
        {
            printf("%d", err);
        }
        
        glfwSwapBuffers(window);
        glfwPollEvents();
        
    }
    
    glDeleteProgram(shaderProgram_SolidColor);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &IBO);
    glDeleteVertexArrays(1, &VAO);
    
    glfwTerminate();
    return 0;
    
}

GLFWwindow *InitGLFW(unsigned int winWidth,unsigned int winHeight, char *winTitle, WINDOW_MODE windowMode)
{
    GLFWwindow *windowResult = {};
    
    if(!glfwInit())
    {
        printf("glfwInit() failed\n");
        return 0;
    }
    
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    if(windowMode == windowed)
    {
        windowResult = glfwCreateWindow(winWidth, winHeight, winTitle, 0, 0);
    }
    else if (windowMode == fullscreen)
    {
        windowResult = glfwCreateWindow(winWidth, winHeight, winTitle, glfwGetPrimaryMonitor(), 0);
    }
        if(!windowResult)
        {
            glfwTerminate();
            return 0;
        }
    
    glfwMakeContextCurrent(windowResult);
    glfwSwapInterval(1);
    
    if(!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
    {
        printf("failed to load GLAD\n");
    }
    
    return windowResult;
}

unsigned int LoadShaderProgram(char *path_VS, char *path_FS)
{
    char *textBuffer_VS = (char*)malloc(sizeof(char) * 1024);
    char *textBuffer_FS = (char*)malloc(sizeof(char) * 1024);
    FILE *fHandle;
    unsigned int vertexShader = 0;
    unsigned int fragmentShader = 0;
    unsigned int shaderProgram = 0;
    int success = 0;
    char infoLog[1024] = {};
    
    fHandle = fopen(path_VS, "rb");
    if(!fHandle)
    {
        printf("Couldn't open %s\n", path_VS);
    }
    
    fread(textBuffer_VS, sizeof(char), 1024, fHandle);
    fclose(fHandle);
    
    fHandle = fopen(path_FS, "rb");
    if(!fHandle)
    {
        printf("Coudln't open %s", path_FS);
    }
    
    fread(textBuffer_FS, sizeof(char), 512, fHandle);
    fclose(fHandle);
    
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &textBuffer_VS, 0);
    glCompileShader(vertexShader);
    
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if(!success)
    {
        glGetShaderInfoLog(vertexShader, 512, 0, infoLog);
        printf("---------VERTEX SHADER--------------\n%s\n", infoLog);
    }
    
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &textBuffer_FS, 0);
    glCompileShader(fragmentShader);
    
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if(!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, 0, infoLog);
        printf("---------FRAGMENT SHADER--------------\n%s\n", infoLog);
    }
    
    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if(!success)
    {
        glGetShaderInfoLog(shaderProgram, 512, 0, infoLog);
        printf("---------SHADER PROGRAM----------------\n%s\n", infoLog);
    }
    
    free(textBuffer_VS);
    free(textBuffer_FS);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    
    
    return shaderProgram;
}
Mārtiņš Možeiko
2562 posts / 2 projects
why no rectangle??
Edited by Mārtiņš Možeiko on
Three errors:

1) You are doing glClearColor after glClear. That means on first frame glClear will use default color (black). You should first call glClearColor, and only then glClear

2) You are passing GL_UNSINGED_BYTE to glDrawElements which means it expects indices as "unsigned char" type. But your indices are "unsigned int" type (vtxIndices array). Either use GL_UNSIGNED_INT, or switch to "unsigned char".

3) in VS.glsl you are setting w coordinate to 0. GPU performs perspective divide on outgoing gl_Position.
Which means it is doing "point = gl_Position.xyz / gl_Position.w" operation. By dividing with 0 you are making all vertices go to Infinity Land. Set "position.w" to 1.0.
107 posts
why no rectangle??
you're the best!

thank you!!