3

This is the second time I'm making a game engine, but I'm a little stuck right now, since I cannot figure out why this is happening, no matter what object I send, OpenGL only draws a White Triangle on the center of the screen, like this.

I've even coppied my old code from my last engine on the Renderer and the Camera Objects, still acts the same, so I´m guessing it has something to do with the Render Script.

Renderer:

Renderer2D::Renderer2D(const Shader& shader) {
    this->shader = shader;
    this->Init();
}

Renderer2D::~Renderer2D() {
    glDeleteVertexArrays(1, &this->QuadVAO);
}

void Renderer2D::Render(Texture & texture, iVec2 position, iVec2 size, float rotation, iVec3 color) {
    this->shader.Use();
    iMat4 model;

    using namespace glm;
    model = translate(model, iVec3(position.x, position.y, 0.0f));
    /*
    model = translate(model, iVec3(size.x * 0.5f, size.y * 0.5f, 0.0f));
    model = rotate(model, rotation, iVec3(0.0f, 0.0f, 1.0f));
    model = translate(model, iVec3(size.x * -0.5f, size.y * -0.5f, 0.0f));
    */
    model = glm::scale(model, iVec3(size.x, size.y, 1.0f));

    this->shader.SetMatrix4("model2D", model);
    this->shader.SetVector3f("color2D", color);

    glActiveTexture(GL_TEXTURE0);
    texture.Bind();

    glBindVertexArray(this->QuadVAO);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
}

void Renderer2D::Init() {
    U16 VBO;

    float vertices[] = {
        // Pos      // Tex
        0.0f, 1.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 0.0f,

        0.0f, 1.0f, 0.0f, 1.0f,
        1.0f, 1.0f, 1.0f, 1.0f,
        1.0f, 0.0f, 1.0f, 0.0f
    };

    glGenVertexArrays(1, &this->QuadVAO);
    glGenBuffers(1, &VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindVertexArray(this->QuadVAO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

Vertex Shader:

#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords>

out vec2 TexCoords;

uniform mat4 view2D;
uniform mat4 model2D;
uniform mat4 projection2D;

void main()
{
    TexCoords = vertex.zw;
    gl_Position = projection2D * view2D * mode2Dl * vec4(vertex.xy, 0.0, 1.0);
}

Edit:

Vertex Shader:

#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords>

out vec2 TexCoords;

uniform mat4 view2D;
uniform mat4 model2D;
uniform mat4 projection2D;

void main()
{
    TexCoords = vertex.zw;
    gl_Position = projection2D * view2D * model2D * vec4(vertex.xy, 0.0, 1.0);
}

Fragment Shader:

#version 330 core
in vec2 TexCoords;
out vec4 color2D; //Fixed the error!

uniform sampler2D image2D;
uniform vec3 color2D;

void main()
{    
    color2D = vec4(color2D, 1.0) * texture(image2D, TexCoords);
}  

Resources.cpp

Shader Resources::LoadShaderFromFile(const char * vertexSource, const char * fragmentSource) {
    using namespace std;
    string vertexCode;
    string fragmentCode;

    try {
        ifstream vertexShaderFile(vertexSource);
        ifstream fragmentShaderFile(fragmentSource);
        stringstream vShaderStream, fShaderStream;

        vShaderStream << vertexShaderFile.rdbuf();
        fShaderStream << fragmentShaderFile.rdbuf();

        vertexShaderFile.close();
        fragmentShaderFile.close();

        vertexCode = vShaderStream.str();
        fragmentCode = fShaderStream.str();
    }
    catch (exception e) {
        cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
    }

    const char *vShaderCode = vertexCode.c_str();
    const char *fShaderCode = fragmentCode.c_str();

    Shader shader;
    shader.Compile(vShaderCode, fShaderCode);

    return shader;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
HalloweenJack
  • 281
  • 2
  • 15
  • What version of OpenGL are you running? Are you aware that as of OpenGL 3+ quads are deprecated? – Patrick Nov 09 '18 at 02:27
  • 1
    @Patrick I just added Quads on the title because I´m trying to draw quads, but I´m Actually drawing 2 triangles next to each other, just updated the question with that code. – HalloweenJack Nov 09 '18 at 02:33
  • Could you try changing the z coordinates of the triangles to -1.0f instead of 0.0f. I believe the triangle is clipping since positive z is out of screen. – Patrick Nov 09 '18 at 02:44
  • @Patrick `model = translate(model, iVec3(position.x, position.y, -1.0f)); ` Changed this line, is that what you mean? – HalloweenJack Nov 09 '18 at 03:04
  • @Patrick I'm starting to think is a view port error or something since, no matter what coordinates I enter, triangle will always be the same position with no texture. – HalloweenJack Nov 09 '18 at 03:04
  • No in your vertex array try changing the z values to zero. I just realised I said `-1.0f instead of 0.0f` I meant to say -1.0f instead of 1.0f. – Patrick Nov 09 '18 at 03:07
  • @Patrick It just flipped the triangle to the down left side of the screen, that meaning it's compleately ignoring the position sent. It only follows the vertices coordinates and only makes 1 triangle! Still have no idea how to solve it... Also, it totally ignores the indices 7 to 12 on the vertices array... – HalloweenJack Nov 09 '18 at 03:13
  • Seems like some sort of transform is being applied incorrectly try setting all transform matrices to identity matrix (glm::mat4(1.0f)) – Patrick Nov 09 '18 at 03:16
  • How should I do that? Sorry I still don't get glm that well... – HalloweenJack Nov 09 '18 at 03:19
  • Also if I may ask: why are you using imat4 for your model matrix? Are you only transforming by integral amounts or something? – Patrick Nov 09 '18 at 03:19
  • Just set model to `glm::mat4(1.0f)` also try using floating point matrix `glm::mat4` instead of integer matrix `imat4` – Patrick Nov 09 '18 at 03:20
  • is just my naming convention, its a `#define` for `glm::mat4` – HalloweenJack Nov 09 '18 at 03:21
  • Nothing changed when I made ` model = glm::mat4(1.0f);` – HalloweenJack Nov 09 '18 at 03:23
  • Ohh I think I may see the problem. It looks like your interleaving vertex and uv data but you define in `glVertexAttribPointer` call that the vertex buffer has four components per attribute, try changing second argument to 2 instead of 4. – Patrick Nov 09 '18 at 03:28
  • @Patrick Well, that's something, at least it renders the full quare now, but still no texture and not the desired position! – HalloweenJack Nov 09 '18 at 03:35
  • Could you post your shader code its hard to tell the problem without it – Patrick Nov 09 '18 at 03:36
  • Also you aren't rendering texture properly because you aren't sending uv coords to gpu anymore. Your options are to send a vec4 which has xy as vertex coords and zw as uv coords or you could create a new buffer for uv data. – Patrick Nov 09 '18 at 03:39
  • @Patrick Just Posted the Vertex and Fragment code, not sure what you mean with I'm not sending the uv coords anymore tho... – HalloweenJack Nov 09 '18 at 03:52
  • If you use a size of 2 for glVertexAttribPointer then you'll be missing the uv components, but it looks like you aren't doing that... I would suggest debugging texture data, format and binding in your Texture class if your not seeing the texture. **Edit:** I just noticed that you are multiplying fragment color by sprite color why are you doing this? is this a dot product? Try just using `color = texture(image, TexCoords)` – Patrick Nov 09 '18 at 04:05
  • I'll try that, but what about the position and the size being incorrect? Is it because of the shader? It just takes the normal vertices position – HalloweenJack Nov 09 '18 at 04:10
  • The original size of 4 should work with your shader. – Patrick Nov 09 '18 at 04:13
  • Then why isn’t it resizing? Does it have to do with the texture error? – HalloweenJack Nov 09 '18 at 04:20
  • Why isn't what resizing? – Patrick Nov 09 '18 at 04:26

1 Answers1

5

You'r shader doesn't even compile. When you declare the matrix uniforms, then you use the names model2D, view2D and projection2D:

uniform mat4 view2D;
uniform mat4 model2D;
uniform mat4 projection2D;

But when you use the matrices, then you use the names view, model and projection:

gl_Position = projection * view * model * vec4(vertex.xy, 0.0, 1.0);

I recommend to check if the shader objects compiled successfully and if the program object link successfully.

If the compiling of a shader succeeded can be checked by glGetShaderiv and the parameter GL_COMPILE_STATUS.

e.g.

GLuint shaderObj = .... ;
glCompileShader( shaderObj );

GLint status = GL_TRUE;
glGetShaderiv( shaderObj, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint logLen;
    glGetShaderiv( shaderObj, GL_INFO_LOG_LENGTH, &logLen );
    std::vector< char >log( logLen );
    GLsizei written;
    glGetShaderInfoLog( shaderObj, logLen, &written, log.data() );
    std::cout << "compile error:" << std::endl << log.data() << std::endl;
}

If the linking of a program was successful can be checked by glGetProgramiv and the parameter GL_LINK_STATUS.

e.g.

GLuint progObj = ....;
glLinkProgram( progObj );

GLint status = GL_TRUE;
glGetProgramiv( progObj, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint logLen;
    glGetProgramiv( progObj, GL_INFO_LOG_LENGTH, &logLen );
    std::vector< char >log( logLen );
    GLsizei written;
    glGetProgramInfoLog( progObj, logLen, &written, log.data() );
    std::cout  << "link error:" << std::endl << log.data() << std::endl;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • got the following error, link error: Vertex info ----------- (0) : error C5145: must write to gl_Position. – HalloweenJack Nov 09 '18 at 21:39
  • Just Updated the code with the Resources.cpp but I think there is no error while loading the shader. – HalloweenJack Nov 09 '18 at 21:41
  • @MarceloColonia Ther is a typo in your code: `mode2Dl` instead of `model2D` – Rabbid76 Nov 09 '18 at 21:50
  • sorry that typo is because I manually wrote the update instead of copying it, the original code is correct. – HalloweenJack Nov 09 '18 at 21:53
  • @MarceloColonia You got a link error, possibly you swapped the vertex and the fragment shader or you didn't proper load the shaders form the file and the source code strings are empty. – Rabbid76 Nov 09 '18 at 21:56
  • already checked, sources are fine, but I have detected an error on texture loading, could it have any effect on that error message at all? – HalloweenJack Nov 09 '18 at 23:04
  • Thanks, I found out there was actually an error on fragment shader, I was exporting color instead of color2D, it works now. – HalloweenJack Nov 10 '18 at 00:01