2a. The triangles scale increases (expands) when the 'e' key is pressed and decreases (contracts) when the 'c' key is pressed.

Similar documents
Erik Anchondo cse 520 lab 4

Philip Calderon CSE 520 Lab 3 Color Shader

1. Write a shader program that animates the morphing of Figure 'A' to Figure 'B' and back.

1)Write a shader program that renders a regular pentagon with textured image like the one shown below.

1) Here is the various stages of the tetrahedron:

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

Yazhuo Liu Homework 3

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

FAKULTI TEKNOLOGI MAKLUMAT DAN KOMUNIKASI BITM INTERACTIVE COMPUTER GRAPHICS LAB SESSION 4. C++ - OpenGL

Lecture 3. Understanding of OPenGL programming

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

This Lecture. Why OpenGL? Introduction to OpenGL. Programmer s View

Using OpenGL with CUDA

To Do. Demo: Surreal (HW 3) This Lecture. Introduction to OpenGL. Outline. Foundations of Computer Graphics (Spring 2012)

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Source code: #include <iostream>

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Exercise 1 Introduction to OpenGL

20 GLuint objects; 36 Scale += 0.1; 37 break; 38 case GLUT_KEY_DOWN:


C OMPUTER G RAPHICS Thursday

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

The GLSL API. Mike Bailey. Oregon State University. Geometry Shader. Program. The GLSL Shader-creation Process. create. compile. Vertex.

Basic Graphics Programming

This Lecture. Introduction to OpenGL. Outline. Why OpenGL? Programmer s View. Foundations of Computer Graphics

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

Introduction to Computer Graphics with OpenGL/GLUT

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Announcements OpenGL. Computer Graphics. Spring CS4815

... Print PROGRAMS\Final Project final\planes\planeshoot.c 1

CS 4204 Computer Graphics

Precept 2 Aleksey Boyko February 18, 2011

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

1 /* 4 C:\opencv\build\include. 6 C:\opencv\build\x86\vc10\lib

Display Lists in OpenGL

To Do. Demo: Surreal (now 15+ years ago) This Lecture. Outline. Computer Graphics. CSE 167 [Win 19], Lecture 6: OpenGL 1 Ravi Ramamoorthi

Programming with OpenGL Part 3: Three Dimensions

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Chapter 13 Selection and Feedback

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

6. Make use of glviewport() to display two sine curves on the same screen, one on the

Interaction Computer Graphics I Lecture 3

Basic Graphics Programming

Drawing Primitives. OpenGL basics

Introduction to OpenGL: Part 2

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Computer graphics MN1

Assignment 1. Simple Graphics program using OpenGL

Lectures OpenGL Introduction

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Fourier Series. Given a periodic function, x(t), (-T <t <T): + b n sin n t T. a n cos n t T. 2 a 0 + Z T. a n = 1 T. x(t) cos n t T. b n = 1 T.

An Overview GLUT GLSL GLEW

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computer Graphics. OpenGL

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

Computer Graphics (CS 4731) & 2D Graphics Systems

(21) OpenGL GUI. OpenGL GUI 1 UNIX MAGAZINE UNIX. SGI (Silicon Graphics Inc.) Windows PC GUI. UNIX Windows GUI. Java. 1 prefposition() X X

Introduction to OpenGL

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

compatibility mode core mode

RECITATION - 1. Ceng477 Fall

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Order of Transformations

Computer Graphics Course 2005

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

Bob s Concise Introduction to Doxygen

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

Andrew Yenalavitch Homework 1 CSE Fall 2014

PART-I: Lab for MCS-051 (Advanced Internet Technologies)

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Transformation, Input and Interaction. Hanyang University

CSE4030 Introduction to Computer Graphics

OpenGL refresher. Advanced Computer Graphics 2012

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Computer Graphics Introduction to OpenGL

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Interaction. CSCI 420 Computer Graphics Lecture 3

COMPUTER GRAPHICS LAB # 3

Interaction. CSCI 480 Computer Graphics Lecture 3

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

OpenGL/GLUT Intro. Week 1, Fri Jan 12

CS 548: COMPUTER GRAPHICS INTRODUCTION TO OPENGL AND GLUT SPRING 2015 DR. MICHAEL J. REALE

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Transcription:

Erik Anchondo 1-29-19 cse 520 lab 3 1. Wrote a shader program to display three colored triangles, with one of each triangle being red, green, and blue. The colors change which triangle they are applied to when the user presses the 't' key. 2a. The triangles scale increases (expands) when the 'e' key is pressed and decreases (contracts) when the 'c' key is pressed.

2b. The triangles rotate in the positive direction along the x, y, and z axis when the user presses the 'x', 'y', and 'z' keys. The triangles also rotates in the negative direction when the user presses the 'X', 'Y', 'Z' keys.

Here is the full source code to the program: //color.cpp #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <unistd.h> #define GLEW_STATIC 1 #include <GL/glew.h> #include <GL/glu.h> #include <GL/glut.h> using namespace std; GLuint programobject = 0; GLuint vertexshaderobject = 0; GLuint fragmentshaderobject = 0; static GLint win = 0; //colors int triangle_1 = 1; int triangle_2 = 2; int triangle_3 = 3; //rotations float theta[3]; int thetaloc[3]; int aloc; float x = 0; float y = 0; float z = 0; //scaling float thes[3]; int thesloc[3]; int sloc; float s_x = 1; float s_y = 1; float s_z = 1; int readshadersource(char *filename, GLchar **shader ) FILE *fp; int count, pos, shadersize;

fp = fopen( filename, "r"); if (!fp ) return 0; pos = (int) ftell ( fp ); fseek ( fp, 0, SEEK_END ); shadersize = ( int ) ftell ( fp ) - pos; fseek ( fp, 0, SEEK_SET ); if ( shadersize <= 0 ) printf("shader %s empty\n", filename); return 0; *shader = (GLchar *) malloc( shadersize); if ( *shader == NULL ) printf("memory allocation error\n"); count = (int) fread(*shader, 1, shadersize, fp); (*shader)[count] = '\0'; if (ferror(fp)) count = 0; fclose(fp); return 1; int installshaders(const GLchar *vertex, const GLchar *fragment) GLint vertcompiled, fragcompiled; // status values GLint linked; vertexshaderobject = glcreateshader(gl_vertex_shader); fragmentshaderobject = glcreateshader(gl_fragment_shader); glshadersource(vertexshaderobject, 1, &vertex, NULL); glshadersource(fragmentshaderobject, 1, &fragment, NULL); glcompileshader(vertexshaderobject); glgetshaderiv(vertexshaderobject, GL_COMPILE_STATUS, &vertcompiled); glcompileshader( fragmentshaderobject ); glgetshaderiv( fragmentshaderobject, GL_COMPILE_STATUS, &fragcompiled); printf("vertcompiled, fragcompiled: %d, %d\n", vertcompiled, fragcompiled); if (!vertcompiled!fragcompiled) return 0;

programobject = glcreateprogram(); glattachshader( programobject, vertexshaderobject); glattachshader( programobject, fragmentshaderobject); gllinkprogram(programobject); glgetprogramiv(programobject, GL_LINK_STATUS, &linked); if (!linked) return 0; return 1; int init(void) const char *version; GLchar *VertexShaderSource, *FragmentShaderSource; int loadstatus = 0; version = (const char *) glgetstring(gl_version); if (version[0]!= '2' version[1]!= '.') printf("this program requires OpenGL 2.x, found %s\n", version); exit(1); readshadersource( (char *) "color.vert", &VertexShaderSource ); //link vert file readshadersource( (char *) "color.frag", &FragmentShaderSource ); //link frag file loadstatus = installshaders(vertexshadersource, FragmentShaderSource); gluseprogram(programobject); //colors GLchar names[][20] = "red", "green", "blue" ; GLint loc[10]; for ( int i = 0; i < 3; ++i ) loc[i] = glgetuniformlocation(programobject, names[i]); if (loc[i] == -1) printf("no such uniform named %s\n", names[i]); gluniform3f(loc[0], 1.0, 0.0, 0.0); gluniform3f(loc[1], 0.0, 1.0, 0.0); gluniform3f(loc[2], 0.0, 0.0, 1.0); //scaling

GLchar names2[][20] = "sx", "sy", "sz" ; for ( int i = 0; i < 3; i++ ) thesloc[i] = glgetattriblocation ( programobject, names2[i] ); if ( thesloc[i] < 0 ) printf("no such name %s\n", names2[i]); //rotations char names1[][20] = "ax", "ay", "az" ; for ( int i = 0; i < 3; i++ ) thetaloc[i] = glgetattriblocation ( programobject, names1[i] ); if ( thetaloc[i] < 0 ) printf("no such name %s\n", names1[i]); return loadstatus; static void Reshape(int width, int height) glviewport(0, 0, width, height); glmatrixmode(gl_projection); glloadidentity(); glfrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0f, 0.0f, -15.0f); void CleanUp(void) gldeleteshader(vertexshaderobject); gldeleteshader(fragmentshaderobject); gldeleteprogram(programobject);

glutdestroywindow(win); static void Idle(void) glutpostredisplay(); void change_color() triangle_1++; triangle_2++; triangle_3++; if (triangle_1 > 3)triangle_1 = 1; if (triangle_2 > 3)triangle_2 = 1; if (triangle_3 > 3)triangle_3 = 1; glutpostredisplay(); void rotate_object(char rotation_axis, float degrees, int rotation_dir) degrees *= rotation_dir; if (rotation_axis == 'x')x += degrees; if (rotation_axis == 'y')y += degrees; if (rotation_axis == 'z')z += degrees; glutpostredisplay(); void scale_object(float scale_size, int scale_dir) scale_size *= scale_dir; s_x += scale_size; s_y += scale_size; s_z += scale_size; glutpostredisplay(); static void Key(unsigned char key, int x, int y) switch(key) case 't': change_color(); case 'x': rotate_object('x', 0.2, 1); case 'y': rotate_object('y', 0.2, 1); case 'z':

rotate_object('z', 0.2, 1); case 'e': scale_object(0.020, 1); case 'c': scale_object(0.020, -1); case 'X': rotate_object('x', 0.2, -1); case 'Y': rotate_object('y', 0.2, -1); case 'Z': rotate_object('z', 0.2, -1); case 27: CleanUp(); exit(0); glutpostredisplay(); void display(void) GLfloat vec[4]; int loc; glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glclearcolor( 0.0, 0.0, 0.0, 0.0 ); glcolor3f ( 1, 0, 0 ); loc = glgetattriblocation(programobject, "vertex_color"); aloc = glgetattriblocation(programobject, "a"); sloc = glgetattriblocation(programobject, "s"); glbegin ( GL_TRIANGLES ); if (loc == -1) printf("no such attribute named %s\n", "vertex_color"); glvertexattrib3f (aloc, x, y, z); glvertexattrib3f (sloc, s_x, s_y, s_z); glvertexattrib1f(loc, triangle_1); glvertex3f (0.0, 1.0, 0.0); glvertex3f (1.0, 0.0, 0.0); //rotate triangles //scale triangles //set color to triangle

glvertex3f (-1.0, 0.0, 0.0); glvertexattrib1f(loc, triangle_2); glvertex3f (0.0-1, 1.0-1, 0.0); glvertex3f (1.0-1, 0.0-1, 0.0); glvertex3f (-1.0-1, 0.0-1, 0.0); //set color to triangle glvertexattrib1f(loc, triangle_3); //set color to triangle glvertex3f (0.0 + 1, 1.0 + 1, 0.0); glvertex3f (1.0 + 1, 0.0 + 1, 0.0); glvertex3f (-1.0 + 1, 0.0 + 1, 0.0); glend(); glutswapbuffers(); glflush(); int main(int argc, char *argv[]) int success = 0; glutinit(&argc, argv); glutinitwindowposition( 0, 0); glutinitwindowsize(600, 600); glutinitdisplaymode(glut_rgb GLUT_DOUBLE GLUT_DEPTH); win = glutcreatewindow(argv[0]); glutreshapefunc(reshape); glutkeyboardfunc(key); glutdisplayfunc(display); glutidlefunc(idle); glewinit(); success = init(); printf("success=%d\n", success ); if ( success ) glutmainloop(); return 0; Here is the vert. shader code: //color.vert attribute float vertex_color; //get color from cpp attribute vec4 a; //get rotation x,y,z from cpp attribute vec4 s; //get scaling x,y,z from cpp varying float color_select; //pass to frag shader void main(void)

vec4 v4; v4 = gl_vertex; color_select = vertex_color; mat4 m0 = mat4 //x rotation ( 1, 0, 0, 0, 0, cos(a.x), -sin(a.x), 0, 0, sin(a.x), cos(a.x), 0, 0, 0, 0, 1 ); mat4 m1 = mat4 //y rotation ( cos(a.y), 0, sin(a.y), 0, 0, 1, 0, 0, -sin(a.y), 0, cos(a.y), 0, 0, 0, 0, 1 ); mat4 m2 = mat4 //z rotation ( cos(a.z), -sin(a.z), 0, 0, sin(a.z), cos(a.z), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); mat4 m3 = mat4 ( s.x, 0, 0, 0, 0, s.y, 0, 0, 0, 0, s.z, 0, 0, 0, 0, 1 ); v4 = m0 * m1 * m2 * m3 * v4; gl_position = gl_projectionmatrix * gl_modelviewmatrix * v4; Here is the frag. shader code: //color.frag uniform vec3 red; uniform vec3 green; uniform vec3 blue; varying float color_select; void main(void)

vec3 color; if (color_select < 1.1) color = red; else if (color_select > 2.9) color = blue; else color = green; gl_fragcolor = vec4(color, 1.0); //red //green //blue Summary: The purpose for this lab was to show how color changing, rotations, and scaling animations work using GLSL shaders. This will be extremely useful for the project.