Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:06:53+00:00 2026-06-10T10:06:53+00:00

I have a obj file which stores data this way: v value1 value2 value3

  • 0

I have a obj file which stores data this way:

v value1 value2 value3  
f value1 value2 value3

First I calculate a normal for the face and then assign for each vertex of that face:

for(int i = 0; i < verticesInd.size(); i+=3)
{
    glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(vertices[verticesInd[i + 1]]) - glm::vec3(vertices[verticesInd[i]]), glm::vec3(vertices[verticesInd[i + 2]]) - glm::vec3(vertices[verticesInd[i]])));
    out_Normals[i] = normal;
    out_Normals[i + 1] = normal;
    out_Normals[i + 2] = normal;
}

For achieve a flat shading I can duplicate vertices:

for(int i = 0; i < verticesInd.size(); i++)
{
    out_Vertices.push_back(vertices[verticesInd[i]]);
} 

and then draw the object using glDrawArrays:

glDrawArrays(GL_TRIANGLES, 0, out_Vertices.size());

For achieve a smooth shading I need to average normals for each vertex but I have no idea how to find adjacent faces.

Edit1: I didn’t notice a single s parameter before f:

v value1 value2 value3
s 1  
f value1 value2 value3

Edit2: Normals averaging

glm::vec3 tNormal;
for(int i = 0; i < vertices.size(); i++)
{
    for(int  j = 0; j < verticesInd.size(); j++)
    {
        if(verticesInd[j] == i)
        {
            tNormal += faceNormals[j / 3];
        }
    }
    aNormals.push_back(glm::normalize(tNormal));
    tNormal = glm::vec3(0,0,0);
}

Edit 3 Face normals:

for(int i = 0; i < verticesInd.size(); i+=3)
{
    glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(vertices[verticesInd[i + 1]]) - glm::vec3(vertices[verticesInd[i]]), glm::vec3(vertices[verticesInd[i + 2]]) - glm::vec3(vertices[verticesInd[i]])));
    faceNormals.push_back(normal);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:06:54+00:00Added an answer on June 10, 2026 at 10:06 am

    In most object formats adjacent faces should be sharing vertices. Finding the smooth shaded normal at a vertex should just then be a question of averaging the normal of any face which uses that vertex.

    I suggest that you create an additional new array the same size as your existing vertex array.

    Iterate over each face, and for each vertex index, add that vertice’s face normal to the new array.

    At the end of the process, normalise the result normal vectors and then use that instead of the previously computed face normals.

    If I understand your data structures correctly, it would look something like this:

    glm::vec3 aNormals[];   // one for each vertex - use the appropriate constructor
    
    for (int i = 0; i < verticesInd.size(); ++i) {
        int f = i / 3;                 // which face is this index part of (3 per face?)
        int v = verticesInd[i];        // which vertex number is being used
        aNormals[v] += faceNormals[f]; // add the face normal to this vertex
    }
    
    // now normalise aNormals
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data from *.obj file. I know how draw with GL_TRIANGLES . How
I have an enum in a file: goog.provide('animals.Fish'); animals.Fish = function(obj) { this.name_ =
I have an Application Object. In Global.asax file i use this: Application[obj] = test;
suppose I have a object: obj:{ child:{ x:12, y:50 }, key1:value1, key2:value2 } if
I have several obj-c classes, each of which require a number of constants that
I have this code: var obj = function (i) { this.a = i; this.init
I have a couple functions like this: object obj.getChild(childIndex) int obj.numChildren() So I am
I have code that looks like this: obj.foo(); // obj might, or might not
I have a servlet which serves an image file which was stored in a
i have a huge CU file which contains my project , im trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.