sh_o
sh_o

Reputation: 1

Possibility of instancing in glut or compatible operation of glut and glfw

This is a situation where many identical objects must be drawn in a 3D engine developed with glut. However, since the object drawing performance does not meet the requirements, I would like to use the instancing technology in the opengl tutorial.

I confirmed that the instancing technology was implemented using the glfw implementation and looked for examples of compatible operation of glut and glfw on Google and Stack Overflow, but could not confirm it. It seems that a conflict may occur during initialization or drawing.

What I want is to use a method of instancing with a glut implementation, or to make glut and glfw compatible and insert an example implemented in glfw. Another way is to tell me how to tune the code sample below to achieve performance such as instancing.

void CModelOBJ::PreCompileRender(UINT nPrecompiledID)
{
    m_nPreCompiledID = nPrecompiledID;
    glNewList(m_nPreCompiledID, GL_COMPILE);
    if (!_loaded) {
        loadMaterialsTexture ();
        _loaded = true;
    }

    int vt_size = (int)vertexes.size ();
    int tc_size = (int)texcoords.size ();
    int no_size = (int)normals.size ();

    for (unsigned int h=0; h<(unsigned int)parts.size (); ++h) {
        int index = findMaterialIndex(parts[h].name);
        vector<sFace> &faces = parts[h].faces;

        if (0 <= index) {

            sMaterial &material = materials[index];

            if (material.texture) {
                glEnable(GL_LIGHTING);
                glEnable(GL_LIGHT0);
                glEnable(GL_TEXTURE_2D);
                glBindTexture (GL_TEXTURE_2D, material.texture);
            }
            else
                //glColor3f(material.Kd[0], material.Kd[1], material.Kd[2]);
                glColor4f(material.Kd[0], material.Kd[1], material.Kd[2],0.2);
        }
        else {
            glDisable (GL_TEXTURE_2D);
            glDisable(GL_LIGHTING);
            glDisable(GL_LIGHT0);

            glColor3f (red, green, blue);
        }


        for (unsigned int i=0, ip=(unsigned int)faces.size (); i<ip; ++i) {
            glBegin (GL_POLYGON);
            for (int j=0, jn=faces[i].n; j<jn; ++j) {
                int &v  = faces[i].v[j];
                int &vt = faces[i].vt[j];
                int &vn = faces[i].vn[j];

                if (0 < vn && vn <= no_size) {
                    sVertex &no = normals[vn - 1];
                    glNormal3f (no.x, no.y, no.z);
                }
                if (0 < vt && vt <= tc_size) {
                    sTexCoord &tc = texcoords[vt - 1];
                    glTexCoord2f (tc.u, tc.v);
                }
                if (0 < v && v <= vt_size) {
                    sVertex &vt = vertexes[v - 1];
                    glVertex3f (scale*vt.x, scale*vt.y, scale*vt.z);
                }
            }
            glEnd ();
        }
    }
    glDisable(GL_CULL_FACE);
    glEndList();
}
void CModelOBJ::Draw ()
{
    glPushMatrix();

        glTranslatef(pos.x, pos.y, pos.z);

        // Rotate the model
        glRotatef(rot_origin.x + rot.x, 1.0f, 0.0f, 0.0f);
        glRotatef(rot_origin.y + rot.y, 0.0f, 1.0f, 0.0f);
        glRotatef(rot_origin.z + rot.z, 0.0f, 0.0f, 1.0f);

        glCallList(m_nPreCompiledID);

    glPopMatrix();

    
    glDisable (GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
}

This code draws an object prepared in advance using PreCompileRender.

I only confirmed that the instancing in the opengl tutorial works properly, but I don't know how to add this to the code of the engine implemented with glut.

Upvotes: 0

Views: 20

Answers (0)

Related Questions