// graph3d.cpp --> a little bit of the libary of 3DRender engine
#include <math.h>
#include <stdio.h>
#include <point3.h>
// 1997-1998
struct face_struct {
float kb,kc,kr;
float dist;
Point3 A,B,C,M;
};
Matrix3 I(1,0,0,
0,1,0,
0,0,1);
Point2 Origin;
Point3 u;
Point3 Position,Target;
Point3 o(0,0,0),i(1,0,0),j(0,1,0),k(0,0,1);
Point3 a,b,c;
float teta,fi;
float Zoom,focal,radius;
class Matrix3;
class Point3;
void look(float,float,float);
void camera_rotation(float,float,float);
void init3d(void);
void Real(void);
void Axono(void);
void origin(int,int);
int Xp(Point3);
int Yp(Point3);
float delta(Point3);
void DoProjection(Point3,Point2 &);
int Detect_Face3D(int,int,Point3,Point3,Point3,float&,float&,float&);
//void Get_Info_Face3D(float,float,Point3,Point3,Point3,face_struct&);
int Detect_Face2D(int xp,int yp,Point2 A,Point2 B,Point2 C);
int Detect_Fast_Face3D(int,int,Point3,Point3,Point3,float&,float&,float&);
float delta(Point3 m) {
return(c.x*(m.x-Position.x)+c.y*(m.y-Position.y)+c.z*(m.z-Position.z)+radius-focal);
}
int Xp(Point3 m) {
float r = Zoom*focal/(c.x*(m.x-Position.x)+c.y*(m.y-Position.y)+c.z*(m.z-Position.z)+radius-focal);
return(r*(b.x*(m.x-Position.x)+b.y*(m.y-Position.y)+b.z*(m.z-Position.z))+Origin.x);
}
int Yp(Point3 m) {
float r = Zoom*focal/(c.x*(m.x-Position.x)+c.y*(m.y-Position.y)+c.z*(m.z-Position.z)+radius-focal);
return(r*(a.x*(m.x-Position.x)+a.y*(m.y-Position.y)+a.z*(m.z-Position.z))+Origin.y);
}
void DoProjection(Point3 m,Point2 &p) {
float r= Zoom*focal/(c.x*(m.x-Position.x)+c.y*(m.y-Position.y)+c.z*(m.z-Position.z)+radius-focal);
p.x = (long) r*(b.x*(m.x-Position.x)+b.y*(m.y-Position.y)+b.z*(m.z-Position.z))+Origin.x;
p.y = (long) r*(a.x*(m.x-Position.x)+a.y*(m.y-Position.y)+a.z*(m.z-Position.z))+Origin.y;
}
void Real(void) { focal=10; }
void Axono(void) { focal=1E5; Position=Point3(0,0,0);}
void look(float t,float f,float k) {
teta=t;fi=f;Zoom=k;
a = Point3(cos(t)*cos(f),cos(t)*sin(f),-sin(t));
b = Point3(-sin(f),cos(f),0.0);
c = Point3(-sin(t)*cos(f),-sin(t)*sin(f),-cos(t)); }
void camera_rotation(float fx,float fy,float fz) {
float l1,l2;
l1=cos(fz)*sin(fy);
l2=sin(fz)*sin(fy);
c.x=cos(fz)*cos(fy);
c.y=-sin(fz)*cos(fy);
c.z=-sin(fy);
b.x=-l1*sin(fx)+sin(fz)*cos(fx);
b.y=l2*sin(fx)+cos(fz)*cos(fx);
b.z=-cos(fy)*sin(fx);
a.x=l1*cos(fx)+sin(fz)*sin(fx);
a.y=-l2*cos(fx)+cos(fz)*sin(fx);
a.z=cos(fy)*cos(fx);
}
void origin(long x,long y) { Origin=Point2(x,y);}
void init3d(void) {
Origin=Point2(320,240);
teta=M_PI/3;fi=M_PI/3;
Real();
look(teta,fi,1);
focal=12;radius=100;
Position=Point3(0,0,0);
Zoom = 10;
}
int Test_k(float k) {
if ((k>=0) && (k<=1)) return 1; else return 0;}
int Detect_Face3D(int x,int y,Point3 A,Point3 B,Point3 C,float& kr,float& kb,float& kc) {
Point3 AB,AC,L,V;
float det;
float xp = -(x-Origin.x)/Zoom;
float yp = -(y-Origin.y)/Zoom;
AB=B-A;AC=C-A;
V = xp*b+yp*a-focal*c;
L = Position-radius*c+focal*c-A;
det = V.x*(AB.y*AC.z-AC.y*AB.z)-AB.x*(V.y*AC.z-AC.y*V.z)+
AC.x*(V.y*AB.z-AB.y*V.z);
if (det==0) return 0; else {
kb = (V.x*(L.y*AC.z-AC.y*L.z)-L.x*(V.y*AC.z-AC.y*V.z)+
AC.x*(V.y*L.z-L.y*V.z))/det;
if (!(Test_k(kb))) return 0; else {
kc = (V.x*(AB.y*L.z-L.y*AB.z)-AB.x*(V.y*L.z-L.y*V.z)+
L.x*(V.y*AB.z-AB.y*V.z))/det;
if (!(Test_k(kc))) return 0; else {
if (kb+kc>1) return 0; else {
kr = (L.x*(AB.y*AC.z-AC.y*AB.z)-AB.x*(L.y*AC.z-AC.y*L.z)+
AC.x*(L.y*AB.z-AB.y*L.z))/det;
if (kr<0) return 0; else return 1;
}}}}
}
/*void Get_Info_Face3D(float xp,float yp,Point3 A,Point3 B,Point3 C,face_struct& INFO) {
Point3 AB,AC,L,V;
float det;
AB=B-A;AC=C-A;
V = xp*b+yp*a-focal*c;
L = Position-radius*c+focal*c-A;
det = V.x*(AB.y*AC.z-AC.y*AB.z)-AB.x*(V.y*AC.z-AC.y*V.z)+
AC.x*(V.y*AB.z-AB.y*V.z);
INFO.kb = (V.x*(L.y*AC.z-AC.y*L.z)-L.x*(V.y*AC.z-AC.y*V.z)+
AC.x*(V.y*L.z-L.y*V.z))/det;
INFO.kc = (V.x*(AB.y*L.z-L.y*AB.z)-AB.x*(V.y*L.z-L.y*V.z)+
L.x*(V.y*AB.z-AB.y*V.z))/det;
INFO.kr = (L.x*(AB.y*AC.z-AC.y*AB.z)-AB.x*(L.y*AC.z-AC.y*L.z)+
AC.x*(L.y*AB.z-AB.y*L.z))/det;
INFO.dist = - INFO.kr*norm(V);
INFO.A = A;INFO.B = B;INFO.C = C;
INFO.M = INFO.kb*AB+INFO.kc*AC+A;
}
*/
/*
int Detect_Face2D(int xp,int yp,Point2 A,Point2 B,Point2 C) {
Point2 AB,AC,AP;
float kb,kc,det;
AB.x=B.x-A.x;AB.y=B.y-A.y;
AC.x=C.x-A.x;AC.y=C.y-A.y;
AP.x=xp-A.x;AP.y=yp-A.y;
det=AB.x*AC.y-AC.x*AB.y;
if (det==0) return 0; else {
kb =(AP.x*AC.y-AC.x*AP.y)/det;
if ((kb>1) || (kb<0)) return 0; else {
kc=(AB.x*AP.y-AP.x*AB.y)/det;
if ((kc>1) || (kc<0)) return 0; else {
if (kb+kc>1) return 0; else return 1;
}}}
}
*/
int Detect_Face2D(int xp,int yp,Point2 A,Point2 B,Point2 C) {
Point2 AB,AC,AP;
long kb,kc,det;
AB.x=B.x-A.x;AB.y=B.y-A.y;
AC.x=C.x-A.x;AC.y=C.y-A.y;
AP.x=xp-A.x;AP.y=yp-A.y;
det=AB.x*AC.y-AC.x*AB.y;
if (det==0) return 0; else {
if (det>0) {
kb =(AP.x*AC.y-AC.x*AP.y);
if ((kb>det) || (kb<0)) return 0; else {
kc=(AB.x*AP.y-AP.x*AB.y);
if ((kc>det) || (kc<0)) return 0; else {
if (kb+kc>det) return 0; else return 1;
}}
} else {
kb =(AP.x*AC.y-AC.x*AP.y);
if ((kb<det) || (kb>0)) return 0; else {
kc=(AB.x*AP.y-AP.x*AB.y);
if ((kc<det) || (kc>0)) return 0; else {
if (kb+kc<det) return 0; else return 1;
}}
}
}
}
int Detect_Fast_Face3D(int xp,int yp,Point3 A,Point3 B,Point3 C,float& kr,float& kb,float& kc) {
Point2 a,b,c;
DoProjection(A,a);DoProjection(B,b);DoProjection(C,c);
if (Detect_Face2D(xp,yp,a,b,c)) { Detect_Face3D(xp,yp,A,B,C,kr,kb,kc);return 1;}
else return 0;
};
// mandel.cpp : Defines the entry point for the application.
// date of program 2002
#include "stdafx.h"
#include "resource.h"
#include "amd3dx.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int mx = 640, my = 480+100;
BOOL threednow = FALSE;
DWORD t1,t2;
TCHAR buffer[]= "ثلاثي الأبعاد";;
HGLOBAL Hgl;
LPBYTE m;
HBITMAP hbmp;
HDC hdcmem;
HDC hdc;
BOOL now = FALSE;
void draw(HDC hdc,float x0,float y0 ) {
float cx,cy,x1,y1;
int a,b;
int orb,i,j =0;
float xo,yo;
SelectObject(hdcmem,hbmp);
for (b = 0; b<= my-100; b++)
for (a = 0 ; a<=mx ; a++) {
cx = -2.5+4*(float)a/my;
cy = 1.5-4*(float)b/my;
orb = 0; i = 0;
xo = x0, yo = y0;
do {
x1 = xo*xo-yo*yo+cx;
y1 = 2*xo*yo+cy;
xo = x1;
yo = y1;
i++;
}
while ((i < 32) && (x1*x1+y1*y1 < 4.0) && (fabs(x1) < 2.0) && (fabs(y1) < 2.0));
if (i < 32) orb = i;
SetPixel(hdc,a,b,RGB(8*orb,8*orb,0));
}
}
class complex {
public :
float x,y;
complex() { x = 0;y = 0;}
complex(float x0,float y0) { x= x0; y = y0;};
}
;
void draw_3DNOW(HDC hdc,float x0,float y0 ) {
float cx,cy;
int a,b;
int orb,i,j = 0;
int m;
complex c,X;
complex r(-2.5,1.5),t(4,-4),A;
float S;
float M[2],cm[2]={2,2};
float x =4.0;
m = mx;
_asm {
movq mm4,t // mm4 ={4,4}
movq mm5,r // mm5={-2.5,1.3}
}
for (b = 0; b<= my-100; b++)
for (a = 0 ; a<=mx ; a++) {
// cx = -2.5+4*(float)a/mx;
// cy = 1.5-4*(float)b/my;
c =complex(cx,cy);
orb = 0; i = 0;
X = complex(x0,y0);
M[0] = (float) a;M[1] = (float) b;
_asm {
femms
movq mm6,M
movd mm0,m
pi2fd (mm0,mm0)
pfrcp (mm1,mm0)
punpckldq mm0,mm0
pfrcpit1 (mm0,mm1)
pfrcpit2 (mm0,mm1)
pfmul (mm0,mm6)
pfmul (mm0,mm4)
pfadd (mm0,mm5)
movq mm3,mm0 // mm3 = cy | cx
mov eax,0
begin:
movq mm0,X // mm0 -> y|x
movq mm1,mm0 // mm1 -> y|x
pswapd (mm2,mm0) // mm2 -> x|y
pfmul (mm1,mm1) // mm1 -> y*y | x*x
pfmul (mm2,mm0) // mm2 -> x*y | x*y
pfpnacc (mm1,mm2) // mm1 -> 2*x*y | x*x-y*y
pfadd (mm1,mm3) // mm1 -> 2*x*y+cy | x*x-y*y+cx
movq X,mm1 // A( x',y');
cond1:
pfmul (mm1,mm1)
pfacc (mm1,mm1) // module ...
movd S,mm1 // S = x'*x'+y'*y'
}
i++;
while ((i < 32) && (S < 4.0) && (fabs(X.x) < 2.0) && (fabs(X.y) < 2.0));
end:
if (i < 32) orb = i;
SetPixel(hdc,a,b,RGB(8*orb,8*orb,0));
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MANDEL, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MANDEL);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MANDEL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+2);
wcex.lpszMenuName = (LPCSTR)IDC_MANDEL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle,
WS_CAPTION | WS_OVERLAPPED | WS_MINIMIZEBOX | WS_POPUP | WS_SYSMENU,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
HMENU hmenu;
BITMAP bm;
switch (message)
{ case WM_CREATE:
MoveWindow(hWnd,50,50,mx,my-100,TRUE);
threednow = FALSE;
timeBeginPeriod(1);
hdc = GetDC(hWnd);
//Hgl = GlobalAlloc(GHND,mx*my*3);
//m= (LPVOID ) GlobalLock(Hgl);
m = new BYTE[mx*my*3];
hbmp = CreateBitmap(mx,my,1,2,m);
// SetBitmapBits(hbmp,3*mx*my,m);
//hbmp = CreateCompatibleBitmap(hdc,mx,my);
hdcmem = CreateCompatibleDC(hdc);
SelectObject(hdcmem,hbmp);
GetObject(hbmp,sizeof(bm),&bm);
//m = (LPBYTE) bm.bmBits;
// PlaySound("action3.wav",hInst,SND_LOOP | SND_ASYNC | SND_FILENAME | SND_NOSTOP );
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_3DNOW:
hmenu =GetMenu(hWnd);
threednow = !threednow;
if (threednow)
CheckMenuItem(hmenu,IDM_3DNOW,MF_CHECKED);
else
CheckMenuItem(hmenu,IDM_3DNOW,MF_UNCHECKED);
break;
case IDM_DRAW:
hdc = GetDC(hWnd);
SetTextColor(hdc,RGB(255,255,0));
SetBkMode(hdc,TRANSPARENT);
PatBlt(hdc,0,0,mx,my,PATCOPY);
t1 = timeGetTime();
if (threednow) draw_3DNOW(hdc,0,0); else draw(hdc,0,0);
t2 = timeGetTime();
// sprintf( buffer,"%d", (t2-t1) );
// if (threednow) strcat(buffer,"ms 3DNow!"); else strcat(buffer,"ms FPU");
TextOut(hdc,0,0, (LPCSTR) buffer, strlen(buffer));
DrawTextEx(hdc,(LPSTR) L"DEMO",5,NULL,DT_CENTER,NULL);
// TextOut(hdc,0,0,(LPCSTR "demo",5);
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
#include "stdafx.h"
#define KEY_ESCAPE 27
GLuint texture;
void InitTexture()
{
HBITMAP hBMP; // Handle Of The Bitmap
BITMAP BMP;
//byte Texture IDB_;
hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
GetObject( hBMP, sizeof(BITMAP), &BMP );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, BMP.bmWidth, BMP.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
}
typedef struct {
int width;
int height;
char* title;
float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;
glutWindow win;
float g_rotation = 0;
float g_rotation_speed = 0.2f;
void display()
{
// Clear Screen and Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Define a viewing transformation
gluLookAt( 4,2,0, 0,0,0, 0,1,0);
// Push and pop the current matrix stack.
// This causes that translations and rotations on this matrix wont influence others.
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,0);
glRotatef(2*g_rotation,1,1,1);
// glRotatef(90+2*g_rotation,1,0,0);
//glRotatef(90+2*g_rotation,1,0,0);
// Draw the teapot
// glBlendFunc(GL_ONE,GL_SRC_ALPHA); // Set Blending Mode (Cheap / Quick)
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glutSolidTeapot(1);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-2.5f, -2.5f, 2.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(2.5f, -2.5f, 2.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(2.5f, -2.5f, -2.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-2.5f, -2.5f, -2.5f);
glEnd();
glPopMatrix();
g_rotation += g_rotation_speed;
glutSwapBuffers();
}
void initialize ()
{
// select projection matrix
glMatrixMode(GL_PROJECTION);
// set the viewport
glViewport(0, 0, win.width, win.height);
// set matrix mode
glMatrixMode(GL_PROJECTION);
// reset projection matrix
glLoadIdentity();
GLfloat aspect = (GLfloat) win.width / win.height;
// set up a perspective projection matrix
gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);
// specify which matrix is the current matrix
glMatrixMode(GL_MODELVIEW);
glShadeModel( GL_SMOOTH);
// specify the clear value for the depth buffer
glClearDepth( 1.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
// specify implementation-specific hints
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
GLfloat amb_light[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat diffuse[] = { 0.8, 0.8, 0.8 ,1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 1.0, 1.0, 1.0, 1.0 };
// glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position);
glEnable( GL_LIGHT0 );
GLfloat mdiffuse[] = { 1.0, 1.0, 1.0 ,1.0 };
glMaterialfv( GL_FRONT, GL_AMBIENT, amb_light );
glMaterialfv( GL_FRONT, GL_DIFFUSE, mdiffuse );
glMaterialfv( GL_FRONT, GL_SPECULAR, specular );
glMaterialf( GL_FRONT, GL_SHININESS, 40.0 );
// glEnable( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
glDepthFunc( GL_LEQUAL );
glEnable( GL_DEPTH_TEST );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClearColor(0.0, 0.0, 0.0, 1.0);
InitTexture();
}
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 ";
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); // set window 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;
}
// mmx.cpp :
// C(t) = (1-t)*C1 + t*C2
#include <conio.h>
#include <stdio.h>
typedef struct { char r,g,b,a;} color;
__fastcall void FPUBlendColor(float t,color c1,color c2,color& c) {
float k = 1-t;
c.r = k*c1.r+t*c2.r;
c.g = k*c1.g+t*c2.g;
c.b = k*c1.b+t*c2.b;
}
__fastcall void MMXBlendColor(float t,color c1,color c2,color& c) {
DWORD a,b;
DWORD x = 256;
if (t==0) {c = c1;goto skip;}
if (t==1) {c = c2;goto skip;}
__asm {
fld t
fimul x
fistp a
mov eax,256
sub eax,a
mov b,eax
emms
imul eax,a,010101h //ecx // eax = 0aaa
movd mm2,eax
imul ebx,b,010101h //ebx =0bbb
movd mm3,ebx
pxor mm4,mm4
punpcklbw mm2,mm4 // mm2 =w(0,a,a,a)
punpcklbw mm3,mm4 // mm3 =w(0,b,b,b)
mov eax,dword ptr c2
movd mm0,eax
punpcklbw mm0,mm4 // mm0 =(0,b1,g1,r1)
mov eax,dword ptr c1
movd mm1,eax
punpcklbw mm1,mm4 // mm1 = (0,b2,g2,r2)
pmullw mm0,mm2 // mm0 = (0,a*b1,a*g1,a*r1)
pmullw mm1,mm3 // mm1 = (0,b*b2,b*g2,b*r2)
paddw mm0,mm1 // mm0 = (0,a*b1+b*b2,a*g1+b*g2,a*r1+b*r2)
psrlw mm0,8 // mm0/256
packuswb mm0,mm0 // mm0 =(0,0,0,0,0,r,g,b)
movd eax,mm0
mov ebx,c
mov [ebx],eax
emms
}
skip: ;
}
int main(int argc, char* argv[])
{color c={0,0,0,0};
color c1={255,20,20};
color c2 = {255,200,0};
float t = 0.2;
MMXBlendColor(t,c1,c2,c);
printf("mmx ->%d %d %d\n",c.r,c.g,c.b);
FPUBlendColor(t,c1,c2,c);
printf("fpu ->%d %d %d\n",c.r,c.g,c.b);
getch();
return 0;
}