Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <windows.h>
- #include <math.h>
- typedef struct{
- float x, y;
- } point2D_t;
- typedef struct{
- float r, g, b;
- } color_t;
- typedef struct{
- float v[3];
- } vector2D_t;
- typedef struct{
- float m[3][3];
- } matrix2D_t;
- vector2D_t point2vector(point2D_t p){
- vector2D_t vec;
- vec.v[0]=p.x;
- vec.v[1]=p.y;
- vec.v[2]=1;
- return vec;
- }
- point2D_t vector2point(vector2D_t vec){
- point2D_t p;
- p.x=vec.v[0];
- p.y=vec.v[1];
- return p;
- }
- vector2D_t operator * (matrix2D_t mat, vector2D_t vec){
- vector2D_t vec1;
- for(int i=0;i<3;i++){
- vec1.v[i]=0;
- for(int j=0;j<3;j++){
- vec1.v[i]+=mat.m[i][j]*vec.v[j];
- }
- }
- return vec1;
- }
- matrix2D_t operator * (matrix2D_t mat1, matrix2D_t mat2){
- matrix2D_t mat;
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++){
- mat.m[i][j]=0;
- for(int k=0;k<3;k++){
- mat.m[i][j]+=mat1.m[i][k]*mat2.m[k][j];
- }
- }
- }
- return mat;
- }
- matrix2D_t createIdentity(){
- matrix2D_t mat;
- for(int i=0;i<3;i++){
- for(int j=0;j<3;j++){
- mat.m[i][j]=0;
- }
- mat.m[i][i]=1;
- }
- return mat;
- }
- matrix2D_t translationMTX(float dx, float dy){
- matrix2D_t mat=createIdentity();
- mat.m[0][2]=dx;
- mat.m[1][2]=dy;
- return mat;
- }
- matrix2D_t scalingMTX(float sx, float sy){
- matrix2D_t mat=createIdentity();
- mat.m[0][0]=sx;
- mat.m[1][1]=sy;
- return mat;
- }
- matrix2D_t rotationMTX(float a){
- matrix2D_t mat=createIdentity();
- mat.m[0][0]=cos(a);
- mat.m[0][1]=-sin(a);
- mat.m[1][0]=sin(a);
- mat.m[1][1]=cos(a);
- return mat;
- }
- void drawPolygon(point2D_t p[], int n){
- glBegin(GL_POLYGON);
- for(int i=0; i<n; i++){
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawPolyline(point2D_t p[], int n){
- glBegin(GL_LINE_LOOP);
- for(int i=0; i<n; i++){
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawPolygon(point2D_t p[], color_t col, int n){
- glColor3f(col.r, col.g, col.b);
- glBegin(GL_POLYGON);
- for(int i=0; i<n; i++){
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawPolygon(point2D_t p[], color_t col[], int n){
- glBegin(GL_POLYGON);
- for(int i=0; i<n; i++){
- glColor3f(col[i].r, col[i].g, col[i].b);
- glVertex2f(p[i].x,p[i].y);
- }
- glEnd();
- }
- void drawCircle(float r, float xp, float yp, int n){
- point2D_t p[360];
- float teta;
- for(int i=0; i<n; i++){
- teta=(float)i*6.28/n;
- p[i].x=xp+r*cos(teta);
- p[i].y=yp+r*sin(teta);
- }
- drawPolygon(p,n);
- }
- void drawLine(float x1, float y1, float x2, float y2){
- glBegin(GL_LINES);
- glVertex2f(x1,y1);
- glVertex2f(x2,y2);
- glEnd();
- }
- typedef struct {
- int n;
- point2D_t p[100];
- } object2D_t;
- object2D_t createCircle(float r, int n){
- object2D_t obj;
- obj.n=n;
- for(int i=0;i<n;i++){
- float s=i*6.28/n;
- obj.p[i].x=r*cos(s);
- obj.p[i].y=r*sin(s);
- }
- return obj;
- }
- void userdraw(void) {
- glLineWidth(2);
- for(int i=0; i<100; i+=5){
- /* drawLine(i,0,100,i);
- drawLine(100,i,100-i,100);
- drawLine(100-i,100,0,100-i);
- drawLine(0,100-i,i,0);
- point2D_t p[4]={{i,0},{100,i},{100-i,100},{0,100-i}};
- drawPolyline(p,4);
- */
- float x1,y1,x2,y2;
- float a,b;
- for(int i=0; i<360; i+=10){
- a=i/57.3;
- b=(i+120)/57.3;
- x1=50+50*cos(a);
- y1=50+50*sin(a);
- x2=50+50*cos(b);
- y2=50+50*sin(b);
- drawLine(x1,y1,x2,y2);
- }
- for(int i=0; i<360; i+=10){
- a=i/57.3;
- b=(i+120)/57.3;
- x1=50+25*cos(a);
- y1=50+25*sin(a);
- x2=50+25*cos(b);
- y2=50+25*sin(b);
- drawLine(x1,y1,x2,y2);
- }
- }
- }
- void display(void) {
- glClear( GL_COLOR_BUFFER_BIT);
- userdraw();
- glutSwapBuffers();
- }
- int main(int argc, char **argv){
- glutInit(&argc,argv);
- glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB );
- glutInitWindowPosition(100,100);
- glutInitWindowSize(600,600);
- glutCreateWindow ("Drawing by Achmad Basuki");
- glClearColor(0.0, 0.0, 0.0, 0.0);
- gluOrtho2D(0, 100, 0, 100);
- glutIdleFunc(display);
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement