Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SER332 Lecture 24

SER332 Lecture 24

Introduction to Graphics and Game Development
Coding Review
(201804)

Javier Gonzalez-Sanchez

April 24, 2018
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs SER332 Introduction to Graphics and Game Development Lecture 24:

    Coding Review Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 2 jgs

    Main // MAIN void main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(0, 0); glutInitWindowSize(WIDTH, HEIGHT); glutCreateWindow("Coding review"); // CALLBACK glutDisplayFunc(display); // SHADE AND DEPTH glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); // LIGHT GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); // LOOP glutMainLoop(); }
  3. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 3 jgs

    Create Texture // TEXTURE GLuint texture_from_algorithm() { GLuint texture; const int TexHeight = 128; const int TexWidth = 128; GLubyte textureImage[TexHeight][TexWidth][3]; for (int i = 0; i < TexHeight; i++) for (int j = 0; j < TexWidth; j++) { textureImage[i][j][0] = 127 + i; textureImage[i][j][1] = 0; textureImage[i][j][2] = 127 + j; } glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TexWidth, TexHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, textureImage); return texture; }
  4. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4 jgs

    Display Callback // DISPLAY void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // PROJECTION glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, WIDTH, HEIGHT); gluPerspective(45, 1, 1, 1000); // VIEW glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // LOOKAT gluLookAt(0.0f, 40.0f, 320.0, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f); // SQUARE glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, texture_from_algorithm());
  5. jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 5

    glBegin(GL_TRIANGLES); glVertex3f(0.0, 50.0, 50.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(50.0,50.0, 50.0); glNormal3f( 0.0, 0.0, 1.0); glVertex3f(50.0, 0.0, 50.0); glNormal3f( 0.0, 0.0, 1.0); glVertex3f(50.0, 0.0, 50.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 50.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 50.0, 50.0); glNormal3f(0.0, 0.0, 1.0); glTexCoord2f(0.0, 1.0); glTexCoord2f(1.0, 1.0); glTexCoord2f(1.0, 0.0); glTexCoord2f(0.0, 0.0); glEnd(); glDisable(GL_TEXTURE_2D); // SWAP glutSwapBuffers(); }
  6. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6 jgs

    Homework Review the full example at https://github.com/javiergs/SER332/blob/master/Lecture24/main.cpp
  7. jgs SER332 Introduction to Graphics Javier Gonzalez-Sanchez [email protected] Spring 2018

    Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.