1

My cubes looks like: enter image description here

But I want this

enter image description here

I can achive this result if I specify normals in VAO and send it, but i draw the cubes with EBO

auto context = QOpenGLContext::currentContext();
    auto functions = context->functions();
    auto additionalFunctions = context->extraFunctions();
    //float side = diagonal/qSqrt(3);
    unsigned int VBO;
    QVector3D vertices[] = {
 
        QVector3D(-0.5f,0.5f,-0.5f),
        QVector3D(-0.5f,0.5f,0.5f),
        QVector3D(0.5f,0.5f,-0.5f),
        QVector3D(0.5f,0.5f,0.5f),
         
        QVector3D(-0.5f,-0.5f,-0.5f),
        QVector3D(-0.5f,-0.5f,0.5f),
        QVector3D(0.5f,-0.5f,-0.5f), 
        QVector3D(0.5f,-0.5f,0.5f),    
 
    };
    
    unsigned int indices[] = {
        0,1,2,
        1,2,3,
 
        4,5,6,
        5,6,7,
        
        0,1,5,
        0,4,5,
        
        2,3,7,
        2,6,7,
    
        0,2,6,
        0,4,6,
       
        1,5,7,
        1,3,7
    };
  

   functions->glGenBuffers(1, &EBO);
   functions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
   functions->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), 
                           indices, GL_STATIC_DRAW);
   functions->glGenBuffers(1, &VBO);
   additionalFunctions->glGenVertexArrays(1, &VAO);
   additionalFunctions->glBindVertexArray(VAO);
   functions->glBindBuffer(GL_ARRAY_BUFFER, VBO);
   functions->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 
                           vertices, GL_STATIC_DRAW);
   functions->glEnableVertexAttribArray(0);
   functions->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), 
                                    nullptr);

vertex shader


#version 130
in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

fragment shader

#version 130
out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
void main()
{
    
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;
    vec3 result = ambient * objectColor;
    FragColor = vec4(lightColor * objectColor, 1.0);
}

So, my question is, is it possible to draw cube with EBO with this 8 verteces and specify this material only with shaders and how i can do it without calculating normals manually in CPU?

  • 1
    Normals aren't really something you can generate in your vertex shader. They require an understanding of the mesh as a whole, along with all the vertices of a triangle. Ultimately this sounds like an XY problem, why are you trying to avoid generating normals in advance? Every single modern 3D modelling software for computer graphics supports exporting normals. – vandench Mar 17 '22 at 13:02
  • I thought that i can generate normals in geometry shader and it will faster – Андрей Петров Mar 17 '22 at 13:05
  • So, how can i store normals if i draw cube with EBO ? I need loop over two arrays and generate another array for Normals? How to resolve this problem correctly? – Андрей Петров Mar 17 '22 at 13:08
  • You'll need another buffer attachment in your VAO containing the normals. If you're using an IBO then you'll of course need to ensure your normals are in the order specified by the IBO. – vandench Mar 17 '22 at 13:14
  • there are 2 options 1. compute normal in Geometry shader using cross product of edges of rendered triangle/quad 2. in Fragment shader use GLSL inbuild differentials of frag coordinate never done this but people are doing it... the first option is simple and straighforward however my experience is that Geometry shaders are not very safe accross gfx vendors and drivers however did not code Geometry shaders for years so the situation might changed ... – Spektre Mar 18 '22 at 07:37
  • And for the lighting just add it to the fragment shader see [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) – Spektre Mar 18 '22 at 07:50

0 Answers0