0 valutazioniIl 0% ha trovato utile questo documento (0 voti)
30 visualizzazioni22 pagine
Each of these Elements> will be flled in as we go. Inteiaction code consists of the cuiient view state, plus event handleis foi mouse clicks and diags.
Each of these Elements> will be flled in as we go. Inteiaction code consists of the cuiient view state, plus event handleis foi mouse clicks and diags.
Copyright:
Attribution Non-Commercial (BY-NC)
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Each of these Elements> will be flled in as we go. Inteiaction code consists of the cuiient view state, plus event handleis foi mouse clicks and diags.
Copyright:
Attribution Non-Commercial (BY-NC)
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Te Overall Structure project5.cpp Headers Interaction Code Set-up Code Render Code Main Function Each of these Elements will be flled in as we go. Headers #include <stdio.h> #include "Angel.h" We need. . . stdio.h because well be ieading data fles. Angel.h foi the OGL and GLUT defnitions. Interaction Code variable rotation x = o variable rotation y = o variable position z = -, function view-matrix() procedure mouse-click(button, state, x, y) procedure mouse-lef-drag(x, y) procedure mouse-right-drag(x, y) procedure mouse-drag(x, y) Te inteiaction code consists of the cuiient view state, plus event handleis foi mouse clicks and diags. function view-matrix() = return Translate(0, 0, position z ) RotateX(rotation x ) RotateY(rotation y ) Tis function ietuins a tiansfoimation matiix foi the cui- ient view paiameteis. procedure mouse-click(button, state, x, y) = start-rotation x rotation x start-rotation y rotation y start-position z position z start-button button start-x x start-y y Te usei clicks the mouse at the start of an inteiaction. Note the state of the view and the mouse at that time. procedure mouse-lef-drag(x, y) = rotation x start-rotation x + 90 (x start x ) window w rotation y start-rotation y + I80 (y start y ) window h if rotation x > o then rotation x +90 if rotation x < -o then rotation x 90 if rotation y > :8o then rotation y rotation y 360 if rotation y < -:8o then rotation y rotation y + 360 Lef mouse diag peifoims a ielative tumble inteiaction. procedure mouse-right-drag(x, y) = position z start-position z + 3 (y start y ) window h Right mouse diag peifoims a ielative dolly inteiaction. procedure mouse-drag(x, y) = if start button == GLUT_LEFT_BUTTON then mouse-lef-drag(x, y) if start button == GLUT_RIGHT_BUTTON then mouse-right-drag(x, y) glutPostRedisplay() Te action of the diag is deteimined by the button piessed at the stait of it. Te diag piobably updated the view, so iequest a iediaw. Set-up Set-up Code procedure setup() = Data Structure Data Loading Normal Calculation Shader Initialization Uniform Initialization Vertex Array Creation We have seveial tasks to complete when the piogiambegins. Te setup pioceduie takes caie of them all. Data Structure structure Vertex = vec3 p vec3 n structure Triangle = GLuint a GLuint b GLuint c Given Vertex v, iefei to its position as v p and noimal as v n . Given Triangle t, veitex indices aie t a , t b , and t c . In C, if the aiiay of Vertex stiuctuies is declaied vertices and the aiiay of Triangle stiuctuies is declaied triangles. . . If v is the i th veitex, v p vertices[i].p v n vertices[i].n v px vertices[i].p.x If t is the j th tiiangle, t a triangles[j].a t b triangles[j].b t c triangles[j].c Ten, foi example, the thiee veitex positions of tiiangle j aie. . . vertices[triangles[j].a].p vertices[triangles[j].b].p vertices[triangles[j].c].p Te Two Tables vertices 0 p x p y p z n x n y n z I p x p y p z n x n y n z 2 p x p y p z n x n y n z
n I p x p y p z n x n y n z Stride = sizeof (Vertex) sizeof (float) = p is , foats, ofset by o bytes n is , foats, ofset by :: bytes triangles 0 a b c I a b c 2 a b c
m I a b c Stride = sizeof (Triangle) a, b, and c aie GLuint Element count is 3 m. Data Loading streamfopen(flename, "r") if stream is not NULL fscanf(stream, " %d %d", &n, &m) vertices new Vertex[n] triangles new Triangle[m] foreach Vertex v in vertices fscanf(stream, " %f %f %f", &v px , &v py , &v pz ) foreach Triangle t in triangles fscanf(stream, " %d %d %d", &t a , &t b , &t c ) Normal Calculation foreach Triangle consisting of Vertices (a, b, c) let n normalize((b p a p ) (c p a p )) a n a n +n b n b n +n c n c n +n foreach Vertex v v n normalize(v n ) Te noimal of a veitex is the noimalized sumof the noimals of all of the tiiangles that touch that veitex. Shader Initialization programInitShader(vshadei,.glsl, fshadei,.glsl) glUseProgram(program) vPositionLocation glGetAttribLocation(program, vPosition) vNormalLocation glGetAttribLocation(program, vNoimal) ProjectionLocation glGetUniformLocation(program, Piojection) ModelViewLocation glGetUniformLocation(program, ModelView) Tese locations will let us specify values foi shadei vaiiables duiing veitex aiiay cieation and iendeiing. Uniform Initialization variable AmbientProduct (o., o., o., :.o) variable DifuseProduct (o.8, o.8, o.8, :.o) variable SpecularProduct (o.:, o.:, o.:, :.o) variable LightPosition (o.o, :.o, o.o, o.o) glUniform4fv(glGetUniformLocation(program, AmbientPioduct), :, AmbientProduct) glUniform4fv(glGetUniformLocation(program, DifusePioduct), :, DifuseProduct) glUniform4fv(glGetUniformLocation(program, SpeculaiPioduct), :, SpecularProduct) glUniform4fv(glGetUniformLocation(program, LightPosition), :, LightPosition) glUniform1f(glGetUniformLocation(program, Shininess), :oo.o) Tese uniforms give the pioduct of the light souice coloi and the mateiial coloi. Choose youi favoiite. Vertex Array Creation Vertex Array Bufer Creation Triangle Array Bufer Creation Vertex Attribute Pointers Heie, we tiansfei oui 3D data to the GPU. I. We initialize a GPU bufei foi oui veitices. 2. We initialize a GPU bufei foi oui tiiangles. 3. We tell OGL the layout of the veitex bufei. Vertex Array Bufer Creation glGenBuffers(:, &vbo) glBindBuffer(GL ARRAY BUFFER, vbo) glBufferData(GL ARRAY BUFFER, n sizeof (Vertex), vertices, GL STATIC DRAW) Triangle Array Bufer Creation glGenBuffers(:, &tbo) glBindBuffer(GL ELEMENT ARRAY BUFFER, tbo) glBufferData(GL ELEMENT ARRAY BUFFER, m sizeof (Triangle), triangles, GL STATIC DRAW) Geneiate, bind, and fll bufeis foi the veitices and tiiangles. Vertex Attribute Pointers glEnableVertexAttribArray(vPositionLocation); glVertexAttribPointer(vPositionLocation, 3, GL FLOAT, GL FALSE, sizeof (Vertex), BUFFER OFFSET( o)); glEnableVertexAttribArray(vNormalLocation); glVertexAttribPointer(vNormalLocation, 3, GL FLOAT, GL FALSE, sizeof (Vertex), BUFFER OFFSET(::)); Tell OGL the layout of the data that we just copied into the veitex bufei. Render Code procedure display() P Perspective(43, I.777, 0.I, I00.0) Mview-matrix() glUniformMatrix4fv(ProjectionLocation, :, GL TRUE, P) glUniformMatrix4fv(ModelViewLocation, :, GL TRUE, M) glClear(GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT) glDrawElements(GL TRIANGLES, 3 m, GL UNSIGNED INT, 0) glutSwapBuffers() Set the matiix unifoims and diaw 3 m tiiangle indices. Main Function int main(int argc, char *argv[]) { glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(96, 54); glutInit(&argc, argv); glutCreateWindow(argv[]); glutMouseFunc(mouse-click); glutMotionFunc(mouse-drag); glutDisplayFunc(display); glewInit(); setup(argv[1]); glutMainLoop(); return ; }