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

  • SEARCH
  • Home
  • 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 8717757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:22:16+00:00 2026-06-13T06:22:16+00:00

The program reads vertices and indices form a textile, and stores the vertices in

  • 0

The program reads vertices and indices form a textile, and stores the vertices in a vector, and indices in an array. Heres is quad.txt –

4 2 numVertices numIndices

-1 -1 0 v1 v2 v3

1 -1 0

1 1 0

-1 1 0

1 2 3

1 3 4

I then render the object to screen – No problem. But when I try to calculate the normals and flat shade the object I get problems. The object is rendered with GL_TRINANGLES. It looks like this https://i.stack.imgur.com/9tXD4.jpg (didn’t have enough reputation to upload it directly…)

It’s like the triangles get shaded separately… Anyway here is the code:

#include "Object.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
#include <glm.hpp>


using namespace std;

const int NMAX = 20000;

GLuint indicesArr[NMAX];

vector<glm::vec3> drawFigure;   glm::vec3 figure;
vector<glm::vec3>Vertices;      glm::vec3 vertex;
vector<glm::vec3>Normals;       glm::vec3 normal;
vector<glm::vec3>Temp;          glm::vec3 temp;


int numVertices, numIndices;

//slices = number of subdivisions around the axis
Object::Object() :
mIsInitialised(false)
{
}

Object::~Object()
{
if(mIsInitialised)
{
    drawFigure.clear();
    Vertices.clear();
    Normals.clear();
    Temp.clear();
}
}


bool Object::loadTxtFile(const string &filename)
{
ifstream in(filename);
in >> numVertices >> numIndices;

while (!in.eof())
{
    for (unsigned int i = 1; i <= numVertices; i++)
    {
        in >> vertex.x >> vertex.y >> vertex.z;
        Vertices.push_back(vertex);
    }

    for (unsigned int i = 1; i <= numIndices * 3; i++)
        in >> indicesArr[i];
}

for (unsigned int i = 1; i <= (numIndices * 3); i++)
{
    figure.x = Vertices[indicesArr[i] - 1].x;
    figure.y = Vertices[indicesArr[i] - 1].y;
    figure.z = Vertices[indicesArr[i] - 1].z;
    drawFigure.push_back(figure);
}

for (unsigned int i = 0; i < (numIndices * 3); i+=3)
{
    temp.x = drawFigure[i].x;
    temp.y = drawFigure[i].y;
    temp.z = drawFigure[i].z;
    Temp.push_back(temp);

    temp.x = drawFigure[i + 1].x;
    temp.y = drawFigure[i + 1].y;
    temp.z = drawFigure[i + 1].z;
    Temp.push_back(temp);

    temp.x = drawFigure[i + 2].x;
    temp.y = drawFigure[i + 2].y;
    temp.z = drawFigure[i + 2].z;
    Temp.push_back(temp);

    normal = glm::normalize(glm::cross(Temp[i + 2] - Temp[i], Temp[i + 1] - Temp[i]));
    Normals.push_back(normal);
}
mIsInitialised = true;
return true;  //Return true if successfully loaded
}


void Object::draw()
{

if(!mIsInitialised) { return; }

//Tell OpenGL about our vertex and colour data
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &drawFigure.front());

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, &Normals.front());

//draw the .txt-file
glDrawArrays(GL_TRIANGLES, 0, (numIndices * 3));

//restore the state GL back
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
  • 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-13T06:22:17+00:00Added an answer on June 13, 2026 at 6:22 am

    You need one normal per vertex. You are currently calculating is one normal per triangle. Take a look at the following post about computing the normals: http://devmaster.net/forums/topic/1065-calculating-normals-of-a-mesh/

    As the post describe there is two ways to compute the normal per vertex:

    1. The vertex normal is the average of the adjacent face normals (See #1 in the devmaster post)
    2. The vertex normal is the angle weighted average of the adjacent face normals (See #6 in the devmaster post)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program reads emails (really just a .txt file structured like an email) and
This program reads strings of numbers from a txt file, converts them to integers,
My program reads device paths like /dev/rdisk0 from input and then it looks in
program reads the following data from a .txt file: f 3.40 f 4.00 m
Part of my program reads directory, then calculates hash for each file in folder.
In my program, it reads a file called datafile.txt... inside the datafile.txt is a
My SSIS program reads as input from a .csv file. The file has about
Below is the program that I wrote. /******************************************************************************* * This program reads EOF from
I have a program that reads from a file that grabs 4 bytes from
I have a program that reads data from a file line-by-line. I would like

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.