Archive Pages Design$type=blogging

Dual Rotating Triangle

A simple opengl c++ program that show triangle of different color rotates along the axis.

In VTU 6th Sem Syllabus COMPUTER GRAPHICS AND VISUALIZATION Lab, Student have to do a project using the OpenGL and C++. Many of the student have made a great mark by submitting some good projects like 3D Animated Robot. Many of student want to have simple program, hence today we have came with very simple program for you.

Dual Rotating Triangle

As you may understand with the name of project, there is two triangle and they are rotating. We have used the simple code for developing this project. Let start doing it. First we have defined a global variable for angle.
int GAngle=0;


The whole program is divided into three parts - Display, Timer and Main. We will code each of them one by one. First We will define the Display Function, where we are going to use simple primitive opengl function for drawing triangle GL_TRIANGLE. We use the global GAngle variable to rotate the drawn triangle. You can experiment with the angle and try how it turn out to be. You can also experiments with the vertices coordinate which draw triangles.


void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
glRotated(GAngle,0,1,0);

// Anti-Clockwise Winding
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(-1,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);

glColor3f(0,1,0);
glVertex3f(-1,0,0.5);
glVertex3f(1,0,0.5);
glVertex3f(0,1,0.5);

glEnd();

GAngle = GAngle + 4;

glFlush();

glutSwapBuffers();
}


Now We add the Timer Function to let the Triangle turn on that time. Code is given below-


void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}
Add main function and execute the program to get the output below.
int main(void)
{
// Init to double buffering

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("Dual Rotating Triangle");
glutDisplayFunc(Display);
glutTimerFunc(0,Timer,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1,3);
glTranslated(0,0,-2);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}


Below is Full Source code for this Project, Just Copy Paste and run it. Before that there are few points for future enchantments -
  • Add Keyboard/Mouse Interaction to start and stop rotation of Triangles.
  • Use Keyboard/Mouse Interaction to Change the Color of the Triangles.


#include <stdio.h>

#include "glut.h"
int GAngle=0;
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotated(GAngle,0,1,0);

// Anti-Clockwise Winding

glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(-1,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glColor3f(0,1,0);
glVertex3f(-1,0,0.5);
glVertex3f(1,0,0.5);
glVertex3f(0,1,0.5);
glEnd();

GAngle = GAngle + 4;

glFlush();

glutSwapBuffers();

}

void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}

int main(void)
{
// Init to double buffering

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("Dual Rotating Triangle");
glutDisplayFunc(Display);
glutTimerFunc(0,Timer,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1,3);
glTranslated(0,0,-2);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: Dual Rotating Triangle
Dual Rotating Triangle
A simple opengl c++ program that show triangle of different color rotates along the axis.
https://1.bp.blogspot.com/-Hk8XcdbVN5U/VuGNZpkeqoI/AAAAAAAAHnM/b8VLppT5KOM/s320/dual%2Brotating%2Btriangle.PNG
https://1.bp.blogspot.com/-Hk8XcdbVN5U/VuGNZpkeqoI/AAAAAAAAHnM/b8VLppT5KOM/s72-c/dual%2Brotating%2Btriangle.PNG
OpenGL Projects
http://www.openglprojects.in/2016/03/dual-rotating-triangle.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2016/03/dual-rotating-triangle.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago