# How to view 3D models in OpenGL

OpenGL is one of the most commonly used tools in creating 3D models. It is also one of the programming interfaces taught in institutions of higher learning. If you would like professional help with assignments derived from this area, contact us right away.

## View 3D model in OpenGL

In this assignment, you will learn the transformation of model and 3D viewing.
You need to download and link glm(OpenGL Mathematics) for matrix expression and calculation. You will be given a file – Tris.txt which contains the vertex coordinates of each triangleand its corresponding colors. The first line is the number of triangles.

And then you need to render 6 figures of different views.
You need to use keyboard to control the switch between different results. When you press “1”,
the window will show the result of first figure and “2” for the second one and so on.
The followings are the parameters:
1） Orthographic projection:

 left right bottom top near far -8 8 -6 6 1 100
Camera transformation:
 Eye position Optical center View-up vector x y z x y z x y z 0 0 10 0 0 0 0 1 0
2) Orthographic projection:
 left right bottom top near far -4 4 -3 3 1 100
Camera transformation:
 Eye position Optical center View-up vector x y z x y z x y z 0 10 0 0 0 0 1 0 2
3) Model Transformation: Translate (-1) in y-direction. Orthographic projection:
 left right bottom top near far -4 4 -3 3 1 100
Camera transformation:
 Eye position Optical center View-up vector x y z x y z x y z 0 10 0 0 0 0 1 0 2
4) Model Transformation: Translate (-1) in y-direction. Perspective projection:
 field of view aspect near far 20 degree 4/3 1 100
Camera transformation:
 Eye position Optical center View-up vector x y z x y z x y z 0 0 20 0 0 0 1 1 0
5) Model Transformation: Translate (-1) in y-direction. Perspective projection:
 field of view aspect near far 30 degree 4/3 1 100
Camera transformation:
 Eye position Optical center View-up vector

 x y z x y z x y z 10 10 10 0 0 0 0 1 0
6) Model Transformation: 1. Rotate 45 degrees about x-axis. 2. Translate (-1) in y-direction. Perspective projection:
 field of view aspect near far 70 degree 4/3 1 100
Camera transformation:
 Eye position Optical center View-up vector x y z x y z x y z 3 3 3 0 0 0 0 1 0

The results look like these:

Solution

main.cpp

#include “utilVisualizer.h”

int main(intargc, char **argv) {

// insert code here…

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

glutInitWindowSize(640, 480);

glutReshapeFunc(reshape);

//Change here with your name and surname as indicated in the instruction pdf

glutCreateWindow(“Titoloprovvisorio”);

glutDisplayFunc(display);

glutKeyboardFunc(keyboardFunction);

init();

glutMainLoop();

return 0;

utilVisualizer.h

#ifndefutilVisualizer_h

#define utilVisualizer_h

#include

#include

#include

#include

#ifdef __APPLE__

#include

#include

#include

#else

#include

#include

#include

#include

#endif

#include

#include

// Include GLM

#include “glm/glm/glm.hpp”

#include “glm/glm/gtc/matrix_transform.hpp”

#include “glm/glm/gtx/transform.hpp”

using namespace glm;

using namespace std;

enumcameraOptions{OPT1, OPT2, OPT3, OPT4, OPT5, OPT6};

//void setUpDisplay();

intnumTs;

int option;

//the id of the shader program

GLuintprogram_id;

struct triangle{

vec3 V1;

vec3 V2;

vec3 V3;

vec4 colorV1;

vec4 colorV2;

vec4 colorV3;

};

vector mesh;

cerr<< “Unable to open file “<

exit(-2);

}

if(numTs==0){

cerr<< ” Invalid number of primitives”<

exit(-2);

}

for(unsigned int ii=0; ii

triangle aux;

//intauxI = 0;

mesh.push_back(aux);

}

}

voidinit(){

option = cameraOptions::OPT1;

glEnable(GL_DEPTH_TEST);

glClearColor(0.0, 0.0, 0.0, 1.0);

}

voiddrawObject(){

for (unsigned int ii=0; ii

triangletodraw = mesh.at(ii);

glBegin(GL_TRIANGLES);

glColor4f(todraw.colorV1[0], todraw.colorV1[1], todraw.colorV1[2], todraw.colorV1[3]);

glVertex3f(todraw.V1[0], todraw.V1[1], todraw.V1[2]);

glColor4f(todraw.colorV2[0], todraw.colorV2[1], todraw.colorV2[2], todraw.colorV2[3]);

glVertex3f(todraw.V2[0], todraw.V2[1], todraw.V2[2]);

glColor4f(todraw.colorV3[0], todraw.colorV3[1], todraw.colorV3[2], todraw.colorV3[3]);

glVertex3f(todraw.V3[0], todraw.V3[1], todraw.V3[2]);

glEnd();

}

}

void display(){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

switch (option) {

casecameraOptions::OPT1:

glMatrixMode(GL_PROJECTION);

glOrtho(-8.0, 8.0, -6.0, 6.0, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

break;

casecameraOptions::OPT2 :

glMatrixMode(GL_PROJECTION);

glOrtho(-4.0, 4.0, -3.0, 3.0, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 2.0);

break;

casecameraOptions::OPT3 :

glMatrixMode(GL_PROJECTION);

glOrtho(-4.0, 4.0, -3.0, 3.0, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glTranslatef(0.0, -1.0, 0.0);

//

break;

casecameraOptions::OPT4:

glMatrixMode(GL_PROJECTION);

gluPerspective(20.0, 4.0/3.0, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0);

glTranslatef(0.0, -1.0, 0.0);

//

break;

casecameraOptions::OPT5:

glMatrixMode(GL_PROJECTION);

gluPerspective(30.0, 4.0/3.0, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glTranslatef(0.0, -1.0, 0.0);

break;

casecameraOptions::OPT6:

glMatrixMode(GL_PROJECTION);

gluPerspective(70.0, 4.0/3.0, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(3.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotatef(45.0, 1.0, 0.0, 0.0);

glTranslatef(0.0, -1.0, 0.0);

break;

default:

break;

}

drawObject();

glutSwapBuffers();

}

voidkeyboardFunction(unsigned char key, int mx, int my){

switch (key) {

case ‘1’:

option = cameraOptions::OPT1;

break;

case ‘2’:

option = cameraOptions::OPT2;

break;

case ‘3’:

option = cameraOptions::OPT3;

break;

case ‘4’:

option = cameraOptions::OPT4;

break;

case ‘5’:

option = cameraOptions::OPT5;

break;

case ‘6’:

option = cameraOptions::OPT6;

break;

case ‘Q’:

case ‘q’:

case 27:

exit(0);

default:

break;

}

glutPostRedisplay();

}

void reshape(int width, int height){

glViewport(0, 0, width, height);

}

#endif /* utilVisualizer_h */

#include

#include

#include

#include

#ifdef __APPLE__

#include

#include

#include

#else

#include

#include

#include

#include

#endif

#include

#include

// Include GLM

#include “glm/glm/glm.hpp”

#include “glm/glm/gtc/matrix_transform.hpp”

#include “glm/glm/gtx/transform.hpp”

using namespace glm;

using namespace std;

enumcameraOptions{OPT1, OPT2, OPT3, OPT4, OPT5, OPT6};

//void setUpDisplay();

intnumTs;

int option;

//the id of the shader program

GLuintprogram_id;

struct triangle{

vec3 V1;

vec3 V2;

vec3 V3;

vec4 colorV1;

vec4 colorV2;

vec4 colorV3;

};

vector mesh;

vectorg_vertex_buffer_data;

GLfloat *vertices;

GLfloatlight_diffuse[] = {1.0, 1.0, 1.0, 1.0};

GLfloatlight_position[] = {1.0, 1.0, 1.0, 0.0};

cerr<< “Unable to open file “<

exit(-2);

}

if(numTs==0){

cerr<< ” Invalid number of primitives”<

exit(-2);

}

for(unsigned int ii=0; ii

triangle aux;

//intauxI = 0;

mesh.push_back(aux);

for(unsigned intkk=0; kk<3;kk++)

g_vertex_buffer_data.push_back(aux.V1[kk]);

for(unsigned intkk=0; kk<3;kk++)

g_vertex_buffer_data.push_back(aux.V2[kk]);

for(unsigned intkk=0; kk<3;kk++)

g_vertex_buffer_data.push_back(aux.V2[kk]);

}

}

FILE* fp;

char *content = NULL;

longint count = 0;

if(fn != NULL){

fp = fopen(fn, “rt”);

if(fp!=NULL){

fseek(fp, 0, SEEK_END);

count = ftell(fp);

rewind(fp);

if(count > 0){

content = (char*)malloc(sizeof(char)*(count+1));

count = fread(content, sizeof(char), count, fp);

content[count] = ‘\0’;

}

fclose(fp);

}

}

return content;

}

std::string Line = “”;

}else{

printf(“Impossible to open %s. Are you in the right directory ? Don’t forget to read the FAQ !\n”, vertex_file_path);

getchar();

return 0;

}

std::string Line = “”;

}

GLint Result = GL_FALSE;

intInfoLogLength;

if ( InfoLogLength> 0 ){

}

if ( InfoLogLength> 0 ){

}

GLuintProgramID = glCreateProgram();

// Check the program

glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);

if ( InfoLogLength> 0 ){

std::vectorProgramErrorMessage(InfoLogLength+1);

glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);

printf(“%s\n”, &ProgramErrorMessage[0]);

}

returnProgramID;

}

voidinit(){

option = cameraOptions::OPT1;

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glEnable(GL_LIGHT0);

glEnable(GL_DEPTH_TEST);

glClearColor(0.0, 0.0, 0.0, 1.0);

// Set them to the correct path

vertices = new GLfloat[g_vertex_buffer_data.size()];

for(unsigned int ii=0; ii

vertices[ii] = g_vertex_buffer_data.at(ii);

}

void display(){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

for(unsigned int ii=0; ii

mat4projectionMatrix, viewMatrix, modelMatrix, MVP, transla, rota;

GLuintMatrixID = glGetUniformLocation(program_id, “MVP”);

switch (option) {

casecameraOptions::OPT1:

projectionMatrix = ortho(-8.0, 8.0, -6.0, 6.0, 1.0, 100.0);

viewMatrix = lookAt(vec3(0.0,0.0,10.0), vec3(0,0,0), vec3(0.0,1.0, 0.0));

modelMatrix = translate(vec3(0.0, 0.0, 0.0));//mat4(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);

MVP = projectionMatrix*viewMatrix*modelMatrix;

break;

casecameraOptions::OPT2 :

projectionMatrix = ortho(-4.0, 4.0, -3.0, 3.0, 1.0, 100.0);

viewMatrix = lookAt(vec3(0.0,10.0,0.0), vec3(0,0,0), vec3(1.0,0.0, 2.0));

modelMatrix = translate(vec3(0.0, 0.0, 0.0));

MVP = projectionMatrix*viewMatrix*modelMatrix;

break;

casecameraOptions::OPT3 :

projectionMatrix = ortho(-4.0, 4.0, -3.0, 3.0, 1.0, 100.0);

viewMatrix = lookAt(vec3(0.0,10.0,10.0), vec3(0,0,0), vec3(0.0,1.0, 0.0));

modelMatrix = translate(vec3(0.0, -1.0, 0.0));

MVP = projectionMatrix*viewMatrix*modelMatrix;

break;

casecameraOptions::OPT4:

projectionMatrix = perspective(20.0, 4.0/3.0, 1.0, 100.0);

viewMatrix = lookAt(vec3(0.0,0.0,20.0), vec3(0,0,0), vec3(1.0,1.0, 0.0));

modelMatrix = translate(vec3(0.0, -1.0, 0.0));

MVP = projectionMatrix*viewMatrix*modelMatrix;

break;

casecameraOptions::OPT5:

projectionMatrix = perspective(30.0, 4.0/3.0, 1.0, 100.0);

viewMatrix = lookAt(vec3(10.0,10.0,10.0), vec3(0,0,0), vec3(0.0,1.0, 0.0));

modelMatrix = translate(vec3(0.0, -1.0, 0.0));

MVP = projectionMatrix*viewMatrix*modelMatrix;

break;

casecameraOptions::OPT6:

projectionMatrix = perspective(70.0, 4.0/3.0, 1.0, 100.0);

viewMatrix = lookAt(vec3(3.0,3.0,3.0), vec3(0,0,0), vec3(0.0,1.0, 0.0));

transla = translate(mat4(1.0f), vec3(0.0f, -1.0f, 0.0f));

rota = rotate(45.0f, vec3(1.0f, 0.0f, 0.0f));

modelMatrix = rota*transla;

MVP = projectionMatrix*viewMatrix*modelMatrix;

break;

default:

break;

}

GLuintvertexbuffer;

glGenBuffers(1, &vertexbuffer);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glBufferData(GL_ARRAY_BUFFER, mesh.size()*9*sizeof(float), vertices, GL_STATIC_DRAW);

glUseProgram(program_id);

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(

0, // attribute. No particular reason for 0, but must match the layout in the shader.

3, // size

GL_FLOAT, // type

GL_FALSE, // normalized?

0, // stride

(void*)0 // array buffer offset

);

glDrawArrays(GL_TRIANGLES, 0, mesh.size()*9);

glDisableVertexAttribArray(0);

}

glutSwapBuffers();

}

voidkeyboardFunction(unsigned char key, int mx, int my){

switch (key) {

case ‘1’:

option = cameraOptions::OPT1;

break;

case ‘2’:

option = cameraOptions::OPT2;

break;

case ‘3’:

option = cameraOptions::OPT3;

break;

case ‘4’:

option = cameraOptions::OPT4;

break;

case ‘5’:

option = cameraOptions::OPT5;

break;

case ‘6’:

option = cameraOptions::OPT6;

break;

case ‘Q’:

case ‘q’:

case 27:

exit(0);

default:

break;

}

glutPostRedisplay();

}

void reshape(int width, int height){

glViewport(0, 0, width, height);

}