Cg Programs

Published on February 2017 | Categories: Documents | Downloads: 59 | Comments: 0 | Views: 354
of 11
Download PDF   Embed   Report

Comments

Content

// gasket2.cpp : main project file. #include "stdafx.h"

/* recursive subdivision of triangle to form Sierpinski gasket */ /* number of recursive steps given on command line */

#include <windows.h> #include <math.h> #include <GL/glut.h>

/* initial triangle */

GLfloat v[3][2]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};

int n;

void triangle( GLfloat *a, GLfloat *b, GLfloat *c)

/* specify one triangle */ { glVertex2fv(a); glVertex2fv(b); glVertex2fv(c); }

void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int m) {

/* triangle subdivision using vertex numbers */

GLfloat v0[2], v1[2], v2[2]; int j; if(m>0) { for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2; for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2; for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;

divide_triangle(a, v0, v1, m-1); divide_triangle(c, v1, v2, m-1); divide_triangle(b, v2, v0, m-1); } else triangle(a,b,c); /* draw triangle at end of recursion */ }

void display() { glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLES); divide_triangle(v[0], v[1], v[2], n); glEnd(); glFlush(); }

void myinit() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glClearColor (1.0, 1.0, 1.0, 1.0); glColor3f(0.0,0.0,0.0); }

int main(int argc, char **argv) { // n=atoi(argv[1]); /* or set number of subdivision steps here */ n=4; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("Sierpinski Gasket"); glutDisplayFunc(display);

myinit(); glutMainLoop(); } // triangle.cpp : main project file.

#include "stdafx.h"

/* * OpenGLSamples Examples * VC++ users should create a Win32 Console project and link * the program with glut32.lib, glu32.lib, opengl32.lib * */

#include <stdio.h> #include <windows.h> #include <GL/gl.h> #include <GL/glut.h> using namespace System; // Standard header for MS Windows applications // Open Graphics Library (OpenGL) header // The GL Utility Toolkit (GLUT) Header

#define KEY_ESCAPE 27

typedef struct { int width; int height;

char* title;

float field_of_view_angle; float z_near; float z_far; } glutWindow;

glutWindow win;

void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Depth Buffer glLoadIdentity(); glTranslatef(0.0f,0.0f,-3.0f); // Clear Screen and

/* * Triangle code starts here * 3 vertices, 3 colors. */ glBegin(GL_TRIANGLES); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glColor3f(1.0f,0.0f,0.0f);

glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();

glutSwapBuffers(); }

void initialize () { glMatrixMode(GL_PROJECTION); // select projection matrix glViewport(0, 0, win.width, win.height); // set the viewport glMatrixMode(GL_PROJECTION); // set matrix mode glLoadIdentity(); // reset projection matrix GLfloat aspect = (GLfloat) win.width / win.height; gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far); perspective projection matrix glMatrixMode(GL_MODELVIEW); // specify which matrix is the current matrix glShadeModel( GL_SMOOTH ); glClearDepth( 1.0f ); // specify the clear value for the depth buffer glEnable( GL_DEPTH_TEST ); glDepthFunc( GL_LEQUAL ); glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // specify implementation-specific hints // set up a

glClearColor(0.0, 0.0, 0.0, 1.0); // specify clear values for the color buffers

}

void keyboard ( unsigned char key, int mousePositionX, int mousePositionY ) { switch ( key ) { case KEY_ESCAPE: exit ( 0 ); break;

default: break; } }

int main(int argc, char **argv) { // set window values win.width = 640; win.height = 480; win.title = "OpenGL/GLUT Example. Visit http://openglsamples.sf.net "; win.field_of_view_angle = 45; win.z_near = 1.0f;

win.z_far = 500.0f;

// initialize and run program glutInit(&argc, argv); // GLUT initialization

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); // Display Mode glutInitWindowSize(win.width,win.height); size glutCreateWindow(win.title); create Window glutDisplayFunc(display); // register Display Function glutIdleFunc( display ); register Idle Function glutKeyboardFunc( keyboard ); register Keyboard Handler initialize(); glutMainLoop(); // run GLUT mainloop return 0; } // gasket.cpp : main project file. // // // set window

//

#include "stdafx.h" #include <stdio.h> #include <windows.h> #include <GL/gl.h> #include <GL/glut.h> using namespace System; // Standard header for MS Windows applications // Open Graphics Library (OpenGL) header

void myinit() { // attributes glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); //red //white background

// setup viewing glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,50.0,0.0,50.0); // (GLdouble L,R,B,T) glMatrixMode(GL_MODELVIEW); }

void display() { // GLfloat verticies[3][3] = {{0.0,0.0,0.0}, {25.0,50.0,0.0}, {50.0,0.0,0.0}}; GLfloat verticies[3][2] = {{0.0,0.0}, {25.0,50.0}, {50.0,0.0}};

//

GLfloat p[3] = {7.5,0.0,0.0}; GLfloat p[2] = {7.5,0.0};

// arbitrary point inside triangle

int j,k; int rand(); glClear(GL_COLOR_BUFFER_BIT); // clear the window

glBegin(GL_POINTS);

for(k=0; k<25000; k++) { j = rand()%3; // pick vertex at random

p[0] = (p[0] + verticies[j][0])/2.0; p[1] = (p[1] + verticies[j][1])/2.0;

// plot point // glVertex3fv(p); glVertex2fv(p);

} glEnd(); glFlush(); }

void main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("OpenGL Example"); glutDisplayFunc(display); myinit(); attributes // initialization function to set openGL state variables dealing with viewing and

// - parameters that we prefer to set once, independently of the display function glutMainLoop();

}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close