0

I created a program that plotted 3D data as a surface. Defined 3 point lighting [key/back/fill] with

glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT2, GL_DIFFUSE, light_grey);
glLightfv(GL_LIGHT1, GL_AMBIENT, amb_level);

and later drew surface using

glBegin(GL_TRIANGLES);

    glColor4f(ct.v1.r,ct.v1.g,ct.v1.b,a);
    glNormal3f(ct.v1.nx,ct.v1.nz,ct.v1.ny);
    glVertex3f(ct.v1.x, ct.v1.z, ct.v1.y);

    glColor4f(ct.v2.r,ct.v2.g,ct.v2.b,a);
    glNormal3f(ct.v2.nx,ct.v2.nz,ct.v2.ny);
    glVertex3f(ct.v2.x, ct.v2.z, ct.v2.y);

    glColor4f(ct.v3.r,ct.v3.g,ct.v3.b,a);
    glNormal3f(ct.v3.nx,ct.v3.nz,ct.v3.ny);
    glVertex3f(ct.v3.x, ct.v3.z, ct.v3.y);

glEnd();

and that created lovely lit scene, but with aliasing

 3 point lighting

.... so moved to FFSA and using VBOs now ... passing colour and vertex data on a triangle by triangle basis to simple shader

    void draw_triangle2(c_triangle ct,bool f)
{
    g_vertex_buffer_data[0][0]=ct.v1.x;
g_vertex_buffer_data[0][1]=ct.v1.z;
g_vertex_buffer_data[0][2]=ct.v1.y;
g_vertex_buffer_data[1][0]=ct.v2.x;
g_vertex_buffer_data[1][1]=ct.v2.z;
g_vertex_buffer_data[1][2]=ct.v2.y;
g_vertex_buffer_data[2][0]=ct.v3.x;
g_vertex_buffer_data[2][1]=ct.v3.z;
g_vertex_buffer_data[2][2]=ct.v3.y;


glBindBuffer(GL_ARRAY_BUFFER, my_gl_vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data[0][0])*9,   g_vertex_buffer_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, my_gl_vertexbuffer);

glVertexAttribPointer(
    0,                 
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);

g_color_buffer_data[0][0]=ct.v1.r;
g_color_buffer_data[0][1]=ct.v1.g;
g_color_buffer_data[0][2]=ct.v1.b;
g_color_buffer_data[1][0]=ct.v2.r;
g_color_buffer_data[1][1]=ct.v2.g;
g_color_buffer_data[1][2]=ct.v2.b;
g_color_buffer_data[2][0]=ct.v3.r;
g_color_buffer_data[2][1]=ct.v3.g;
g_color_buffer_data[2][2]=ct.v3.b;
glBindBuffer(GL_ARRAY_BUFFER, my_gl_colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data[0][0])*9, g_color_buffer_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, my_gl_colorbuffer);   
glVertexAttribPointer(
        1,                                
        3,                                
        GL_FLOAT,                         
        GL_FALSE,                         
        0,                                
        (void*)0                          
    );

glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

};

the vertex shader is

#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;

out vec3 fragmentColor;
uniform mat4 MVP;

void main(){
gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

fragmentColor = vertexColor;
}

fragment shader

#version 330 core

in vec3 fragmentColor;

out vec3 color;

void main(){

color = fragmentColor;
}

but have lost lighting + normal passing.

Trawling tutorials presents a ways of using shaders to calculate lighting, I'm happy to use the fixed pipeline lighting and retain the glLighting block of code. And keep the shaders simple.

What do I need to do? Can I create a 3rd buffer of normals - if so how do I pass them to the fixed pipeine? And in which of these two shaders?

Thanks

user3591811
  • 95
  • 1
  • 1
  • 7

1 Answers1

2

You have two options:

  1. Implement lighting calculations in your own shaders.
  2. Stick with the fixed pipeline.

You can't use your own shaders, and still take advantage of the fixed function lighting features.

I would generally recommend option 1 because the fixed pipeline is deprecated in core OpenGL, and using your own shaders adds a lot of flexibility. But there's definitely some learning curve if you have not done shader programming before. If you're not ready to take the jump yet, you can use your previous fixed pipeline code with VBOs fairly easily. The main things you need to change:

  • Create another VBO for you normals, just like you did for positions and colors. Or store all three attributes interleaved in the same VBO.
  • Instead of glVertexAttribPointer() and glEnableVertexAttribArray(), use their equivalents for fixed function attributes: glVertexPointer(), glNormalPointer(), glColorPointer(), glEnableClientState(GL_[VERTEX|NORMAL|COLOR]_ARRAY).
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133