Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GLUT Shapes Demo
- *
- * Written by Nigel Stewart November 2003
- *
- * This program is test harness for the sphere, cone
- * and torus shapes in GLUT.
- *
- * Spinning wireframe and smooth shaded shapes are
- * displayed until the ESC or q key is pressed. The
- * number of geometry stacks and slices can be adjusted
- * using the + and - keys.
- */
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdlib.h>
- #include <math.h>
- void drawTriangle(int R, int G, int B, float x1, float y1, float x2, float y2, float x3, float y3)
- {
- glBegin(GL_TRIANGLES);
- glColor3ub(R, G, B);
- glVertex2f(x1, y1);
- glColor3ub(G, B, R);
- glVertex2f(x2, y2);
- glColor3ub(B, R, G);
- glVertex2f(x3, y3);
- glEnd();
- }
- void drawCircle(int points)
- {
- glBegin(GL_LINE_LOOP);
- //set the color in 3 float number (red, green, blue)
- glColor3f(0.0, 1.0, 0.0);
- float angle, x, y;
- for (int i = 0; i < points; i++){
- angle = 6.2832 * i / points;
- x = 0.5 * cos(angle);
- y = 0.5 * sin(angle);
- glVertex2f(x, y);
- }
- glEnd();
- }
- void square(float R, float G, float B){
- glColor3f(R,G,B);
- glBegin(GL_POLYGON);
- glVertex3f(0, 0, 0.5);
- glVertex3f(0.5, 0, 0.5);
- glVertex3f(0.5, 0.5, 0.5);
- glVertex3f(0, 0.5, 0.5);
- glEnd();
- }
- void cube(float size){
- glPushMatrix(); //save a copy of the current matrix
- glScalef(size,size,size);
- square(1, 0, 0); //red color side
- glPushMatrix();
- glRotatef(90, 0, 1, 0); //rotate by y
- square(0, 1, 0); //green color side
- glPopMatrix();
- glPushMatrix();
- glRotatef(-90, 1, 0, 0);
- square(0, 0, 1);
- glPopMatrix();
- glPushMatrix();
- glRotatef(180, 0, 1, 0);
- square(0, 1, 1);
- glPopMatrix();
- glPushMatrix();
- glRotatef(-90, 0, 1, 0);
- square(1, 0, 1);
- glPopMatrix();
- glPushMatrix();
- glRotatef(90, 1, 0, 0);
- square(1, 1, 0);
- glPopMatrix();
- glPopMatrix(); // restore matrix to its state before cube() was created.
- }
- void display(){
- //clear memory color buffer
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- //drawTriangle(66, 135, 245, -0.5, -0.5, 0.5, -0.5, 0, 0.5);
- //drawCircle(64);
- //drawTriangle(0, 0, 255, -1, -1, 0, 1, 1, -1);
- //glTranslatef(0.00005, 0.0, 0.0);
- static double x = 15;
- x = x + 0.15;
- glRotatef(x,0,0,1);
- glPushMatrix();
- drawTriangle(0, 0, 255, 0, 0, 0.5, 0.5, 0.5, 0);
- glPopMatrix();
- glPushMatrix();
- drawTriangle(0, 0, 255, 0, 0, -0.5, -0.5, -0.5, 0);
- glPopMatrix();
- glPushMatrix();
- drawTriangle(0, 0, 255, 0, 0, 0, -0.5, 0.5, -0.5);
- glPopMatrix();
- glPushMatrix();
- drawTriangle(0, 0, 255, 0, 0, 0, 0.5, -0.5, 0.5);
- glPopMatrix();
- /*drawTriangle(247, 128, 30,-0.5,0.5,0.5,1,0,0.5);
- glPushMatrix();
- glTranslatef(0.03, 0, 0);
- glScalef(0.4,0.5,0.7);
- glRotatef(150,0,1,0);
- drawTriangle(64, 135, 255, 0.5,0.5,0.5,-0.5,-0.5,-0.5);
- glPopMatrix();
- */
- /*glPushMatrix();
- glTranslatef(0.00005, 0.0, 0.0);
- drawTriangle(0, 0, 255, -1, -1, 0, 1, 1, -1);
- glPopMatrix();*/
- //switch
- //memory buffer
- glutSwapBuffers();
- }
- //reshape windows
- void reshape(int WIDTH, int HEIGHT){
- //sets viewport size from X to win width and from Y to win height
- glViewport(0, 0, WIDTH, HEIGHT);
- }
- void onInit(){
- //glClearColor(R,G,B,A) sets windows background RGB color + alpha channel
- glClearColor(0.0, 0.0, 0.0, 1.0);
- }
- int main(int argc, char **argv){
- //glutInit is used to initialize the GLUT library
- glutInit(&argc, argv);
- // glutInitDisplayMode sets the initial display modes
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
- // glutInitWindowSize sets windows size in pixels (W, H)
- glutInitWindowSize(720, 720);
- // glutWindowsPosition sets windows position, from left down corner
- glutInitWindowPosition(100,100);
- // glutCreateWindows create windows and sets windows title
- glutCreateWindow ("My first OpenGL Windows");
- onInit();
- glutDisplayFunc(display);
- glutIdleFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement