30 bài tập code mẫu cho môn đồ họa , vẽ hình chữ nhật,hình vuông ,...vẽ điểm,hình tam giác, hình tròn , vẽ quy luật của trái đất và mặt trời,vẽ đường tròn và đường thẳng theo các thuật toán,vẽ hình tam giác, tạo hình quay quanh một trục quanh một điểm, vẽ theo thuật toán breseham thuật toán foodfill, thuật toán scanfoodfill, vẽ đường cong bezier, yên ngựa theo đường cong bezier.....
Trang 1Sau đây mình xin chia sẻ tài liệu về môn đồ họa máy tính cho các bạn đang theo học môn lập trình này
có thêm tài liệu để tham khảo khi lập trình.Chúc các bạn học tạp thật tốt với môn đồ họa này nhé
30 bài tập code mẫu
Trang 3GLboolean polySmooth = GL_TRUE;
static void init(void)
Trang 4static GLubyte indices[NFACE][4] = {
printf ("If this is GL Version 1.0, ");
printf ("vertex arrays are not supported.\n");
exit(1);
#endif
Trang 8#define MAXZ 8.0
#define MINZ -8.0
#define ZINC 0.4
static float solidZ = MAXZ;
static float transparentZ = MINZ;
static GLuint sphereList, cubeList;
static void init(void)
Trang 11glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW);
Trang 13Bài 4 mặt trăng,mặt trời
static int year = 0, day = 0;
static int ichosen = NON; // ghi lại xem đối tượng nào đang được chọn, NON//nghĩa là không có đối tượng nào hết
Trang 16// hàm xử lý thông điệp về mouse
void pick(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
/* chỉ xử lý khi người dùng click chuột trái */
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf); // khởi tạo hit records(void) glRenderMode (GL_SELECT); // chọn chế độ selectionglInitNames(); // khởi tạo stack tên
glPushName(0); // đặt tên cho đối tượng rỗng là 0
Trang 19void display(void)
Trang 21glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w, 5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
else
glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0); glMatrixMode(GL_MODELVIEW);
Trang 22init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape); glutKeyboardFunc (keyboard); glutMainLoop();
Trang 23glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);}
Trang 24void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();
Trang 26glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow(argv[0]); init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard); glutMainLoop();
glBegin(GL_POLYGON);
glVertex3f(0.25,0.25,0.0);
Trang 28Bài 8 vẽ đường tròn theo thuật toán breseham
Trang 29P=P+4*(x-y)+10; y=y-0.1;
}
x=x+0.1;
glVertex2f(x0+x,y0+y); glVertex2f(x0+x,y0-y); glVertex2f(x0-x,y0+y); glVertex2f(x0-x,y0-y);
glVertex2f(x0+y,y0+x); glVertex2f(x0+y,y0-x); glVertex2f(x0-y,y0+x); glVertex2f(x0-y,y0-x); }
Trang 30int main()
{
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(400,100);
Trang 31}
x=x+0.1;
glVertex2f(x0+x,y0+y); glVertex2f(x0+x,y0-y); glVertex2f(x0-x,y0+y); glVertex2f(x0-x,y0-y);
glVertex2f(x0+y,y0+x); glVertex2f(x0+y,y0-x); glVertex2f(x0-y,y0+x);
Trang 34spin = spin + 2.0; /* xoay thêm 2 deg cho mỗi lần lặp */
/* các thao tác cần làm khi cửa sổ bị thay đổi kích thước */
void reshape(int w, int h)
glutIdleFunc(spinDisplay); /* khi chương trình đang trong trạng
thái idle (không phải xử lý gì cả) thì sẽ thực hiện hàm spinDisplay */break;
Trang 35case GLUT_MIDDLE_BUTTON: /* khi nhấn nút giữa */
/* hàm main của chương trình */
int main(int argc, char** argv)
Trang 36glBegin (GL_TRIANGLES); // vẽ tam giác
glColor3f (1.0, 0.0, 0.0); // đỉnh thứ nhất màu red
Trang 38static int slices = 16;
static int stacks = 16;
/* GLUT callback Handlers */
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
Trang 40glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidCone(1,1,slices,stacks); glPopMatrix();
Trang 42const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
Trang 43const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
Trang 44glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
* Copyright (c) 1993-1997, Silicon Graphics, Inc
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
Trang 45* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE IN NO EVENT SHALL SILICON
* GRAPHICS, INC BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
Trang 46* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished rights reserved under the copyright laws of the
* United States Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N Shoreline Blvd., Mountain View, CA 94039-7311 *
* OpenGL(R) is a registered trademark of Silicon Graphics, Inc */
Trang 48glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
#ifdef GL_VERSION_1_1
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
#else
glTexImage2D(GL_TEXTURE_2D, 0, 4, checkImageWidth, checkImageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
Trang 49glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
Trang 50gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
Trang 51* Copyright (c) 1993-1997, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE IN NO EVENT SHALL SILICON
* GRAPHICS, INC BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
Trang 52* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement
* Unpublished rights reserved under the copyright laws of the
* United States Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N Shoreline Blvd., Mountain View, CA 94039-7311
Trang 53#include <stdlib.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);
}
void display(void)
{
GLdouble eqn[4] = {0.0, 1.0, 0.0, 0.0}; GLdouble eqn2[4] = {1.0, 0.0, 0.0, 0.0};
/* clip left half x < 0 */
glClipPlane (GL_CLIP_PLANE1, eqn2); glEnable (GL_CLIP_PLANE1);
Trang 55int main(int argc, char** argv)
* Copyright (c) 1993-1997, Silicon Graphics, Inc
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc not be used in advertising
* or publicity pertaining to distribution of the software without specific,
Trang 56* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE IN NO EVENT SHALL SILICON
* GRAPHICS, INC BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement
* Unpublished rights reserved under the copyright laws of the
* United States Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N Shoreline Blvd., Mountain View, CA 94039-7311
*
Trang 57* OpenGL(R) is a registered trademark of Silicon Graphics, Inc */
/*
* colormat.c
* After initialization, the program will be in
* ColorMaterial mode Interaction: pressing the
* mouse buttons will change the diffuse reflection values */
#include<windows.h>
#include <GL/glut.h>
#include <stdlib.h>
GLfloat diffuseMaterial[4] = { 0.5, 0.5, 0.5, 1.0 };
/* Initialize material property, light source, lighting model,
* and depth buffer
Trang 58glEnable(GL_DEPTH_TEST);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Trang 59glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW);
Trang 62#include <stdlib.h>
#include <stdio.h>
static int slices = 16;
static int stacks = 16;
/* GLUT callback Handlers */
struct Color
{
GLubyte red, green, blue;
};
inline int round(const float x ) { return INT(x+0.5);}
Color FillColor; // blue = 0,255,0
Color boundary_color; // green = 0,255,0
// gets the color of the pixel at (x,y)
void getPixel(const int x,const int y , Color &c){
GLubyte color[3] = {0,0,0};
Trang 63glReadPixels(x,400-y,1,1,GL_RGB, GL_UNSIGNED_BYTE, &color); c.red = color[0];
Trang 69glVertex2i(-x+XCenter, -y+YCenter); glVertex2i(-x+XCenter, y+YCenter);
}glEnd();
Trang 71int x0 = 150;
int y0 = 150;
circle(x0,y0,50,boundary_color);
Trang 73printf("pos x = %d pos y = %d\n",x,y);
Trang 74static int slices = 16;
static int stacks = 16;
Trang 75void Axes(void)
{
int i;
glColor3f (1.0, 1.0, 1.0); for(i=-100 ; i<=100 ; i++) {
glVertex2s(i,0);
Trang 76static void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glBegin(GL_POINTS);
Trang 78int board[3][3]; /* amount of color for each square */
/* Clear color value for every square on the board */void init(void)
/* The nine squares are drawn In selection mode, each
* square is given two names: one for the row and the
* other for the column on the grid The color of each
* square is determined by its position on the grid, and
* the value in the board[][] array
Trang 79GLuint ii, jj, names, *ptr;
printf ("hits = %d\n", hits);
Trang 80printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++; printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++; printf (" names are ");
for (j = 0; j < names; j++) { /* for each name */ printf ("%d ", *ptr);
if (j == 0) /* set row and column */
/* pickSquares() sets up selection mode, name stack,
* and projection matrix for picking Then the
* objects are drawn
Trang 81GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) glRenderMode (GL_SELECT);
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
drawSquares (GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
Trang 84glOrtho(-70.0,70.0,-70.0,70.0,-1.0,1.0);}
void Elipse(int x0,int y0,int a,int b){
}
Trang 86Bài 21 vẽ đường thẳng cơ bản
// A Simple OpenGL Project
Trang 87// Author: Michael Hall
Trang 88// A Simple OpenGL Project
// Author: Michael Hall
Trang 89#include <gl/gl.h>
#include <gl/glut.h>
void Draw() {
glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3f(0.2, 0.2, 0.0);glVertex3f(0.8, 0.2, 0.0);glVertex3f(0.6, 0.4, 0.0);glVertex3f(0.4, 0.4, 0.0);glVertex3f(0.4, 0.6, 0.0);glVertex3f(0.6, 0.6, 0.0);glVertex3f(0.8, 0.8, 0.0);glVertex3f(0.2, 0.8, 0.0);glEnd();
glFlush();
}
void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);