Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions nb15033王国军/mySecondHomework/OpenGLHomeWork02/RGBpixmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@




#include "RGBpixmap.h"

typedef unsigned short ushort;
typedef unsigned long ulong;
fstream inf;


ushort getShort()
{
char ic;
ushort ip;
inf.get(ic); ip = ic;
inf.get(ic); ip |= ((ushort)ic << 8);
return ip;
}

ulong getLong()
{

ulong ip = 0;
char ic = 0;
unsigned char uc = ic;
inf.get(ic); uc = ic; ip = uc;
inf.get(ic); uc = ic; ip |=((ulong)uc << 8);
inf.get(ic); uc = ic; ip |=((ulong)uc << 16);
inf.get(ic); uc = ic; ip |=((ulong)uc << 24);
return ip;
}

int RGBpixmap:: readBMPFile(string fname, bool hasAlpha)
{
inf.open(fname.c_str(), ios::in|ios::binary);
if(!inf) {
cout << " can't open file: " << fname << endl;
return 0;
}
int k, row, col, numPadBytes, nBytesInRow;


char ch1, ch2;
inf.get(ch1);
inf.get(ch2);
ulong fileSize = getLong();
ushort reserved1 = getShort();
ushort reserved2 = getShort();
ulong offBits = getLong();
ulong headerSize = getLong();
ulong numCols = getLong();
ulong numRows = getLong();
ushort planes = getShort();
ushort bitsPerPixel = getShort();
ulong compression = getLong();
ulong imageSize = getLong();
ulong xPels = getLong();
ulong yPels = getLong();
ulong numLUTentries = getLong();
ulong impColors = getLong();

if(bitsPerPixel != 24)
{
cout << "not a 24 bit/pixelimage, or is compressed!\n";
inf.close();
return 0;
}

nBytesInRow = ((3 * numCols + 3)/4) * 4;
numPadBytes = nBytesInRow - 3 * numCols;
nRows = numRows;
nCols = numCols;
pixel = new mRGB[nRows * nCols];
if (!pixel)
return 0;

long count = 0;
char dum;
for(row = 0; row < nRows; row++)
{
for(col = 0; col < nCols; col++)
{
char r,g,b;
inf.get(b);
inf.get(g);
inf.get(r);
pixel[count].r = r;
pixel[count].g = g;
pixel[count].b = b;
if ( (hasAlpha) && (r==-1) && (g==-1) && (b==-1) )
pixel[count++].a = 0;
else
pixel[count++].a = 255;
}
for(k = 0; k < numPadBytes ; k++)
inf >> dum;
}
inf.close();
return 1;
}

void RGBpixmap :: setTexture(GLuint textureName)
{
glBindTexture(GL_TEXTURE_2D,textureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nCols, nRows,0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
}

163 changes: 163 additions & 0 deletions nb15033王国军/mySecondHomework/OpenGLHomeWork02/RGBpixmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@





#ifndef _RGBPIXMAP
#define _RGBPIXMAP
#if defined(_WIN32)
#include <glut.h>
#else
#include <GLUT/glut.h>
#endif
#include <string>
#include <iostream>
#include <fstream>
#include <assert.h>

using namespace std;


class IntPoint
{
public:
int x,y;
void set(int dx, int dy){x = dx; y = dy;}
void set(IntPoint& p){ x = p.x; y = p.y;}
IntPoint(int xx, int yy){x = xx; y = yy;}
IntPoint(){ x = y = 0;}
};


class Point2
{
public:
float x,y;
void set(float dx, float dy){x = dx; y = dy;}
void set(Point2& p){ x = p.x; y = p.y;}
Point2(float xx, float yy){x = xx; y = yy;}
Point2(){x = y = 0;}
};


class PolyLine
{
public:
int num;
Point2 pt[80];
PolyLine(){num = 0;}
};


class IntRect
{
public:
int left, top, right, bott;
IntRect(){left = top = right = bott = 0;}
IntRect(int l, int t, int r, int b)
{left = l; top = t; right = r; bott = b;}
void set(int l, int t, int r, int b)
{left = l; top = t; right = r; bott = b;}
void set(IntRect& r)
{left = r.left; top = r.top; right = r.right; bott = r.bott;}
};


typedef unsigned char uchar;

class mRGB
{

public: uchar r,g,b,a;
mRGB(){r = g = b = 0; a = 255;}
mRGB(mRGB& p){r = p.r; g = p.g; b = p.b; a = p.a;}
mRGB(uchar rr, uchar gg, uchar bb){r = rr; g = gg; b = bb;
if ( (r==255) && (g==255) && (b==255)) a = 0; else a = 255;}
mRGB(uchar rr, uchar gg, uchar bb, uchar aa){r = rr; g = gg; b = bb; a = aa;}
void set(uchar rr, uchar gg, uchar bb){r = rr; g = gg; b = bb;
if ( (r==255) && (g==255) && (b==255)) a = 0; else a = 255;}
void set(uchar rr, uchar gg, uchar bb, uchar aa){r = rr; g = gg; b = bb; a = aa;}
};


class RGBpixmap
{
private:
mRGB* pixel;

public:
int nRows, nCols;
RGBpixmap() {nRows = nCols = 0; pixel = 0;}
RGBpixmap(int rows, int cols)
{
nRows = rows;
nCols = cols;
pixel = new mRGB[rows*cols];
}

int readBMPFile(string fname, bool hasAlpha);
void setTexture(GLuint textureName);

void freeIt()
{
delete []pixel; nRows = nCols = 0;
}


void copy(IntPoint from, IntPoint to, int x, int y, int width, int height)
{

if(nRows == 0 || nCols == 0) return;
glCopyPixels(x, y, width, height,GL_COLOR);
}

void draw()
{

if(nRows == 0 || nCols == 0) return;

glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glDrawPixels(nCols, nRows,GL_RGBA, GL_UNSIGNED_BYTE,pixel);
}

int read(int x, int y, int wid, int ht)
{
nRows = ht;
nCols = wid;
pixel = new mRGB[nRows *nCols];
if(!pixel) return -1;
glPixelStorei(GL_PACK_ALIGNMENT,1);
glReadPixels(x, y, nCols, nRows, GL_RGBA,GL_UNSIGNED_BYTE,pixel);
return 0;
}

int read(IntRect r)
{
nRows = r.top - r.bott;
nCols = r.right - r.left;
pixel = new mRGB[nRows *nCols];
if(!pixel) return -1;

glPixelStorei(GL_PACK_ALIGNMENT,1);
glReadPixels(r.left,r.bott, nCols, nRows, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
return 0;
}

void setPixel(int x, int y, mRGB color)
{
if(x>=0 && x <nCols && y >=0 && y < nRows)
pixel[nCols * y + x] = color;
}

mRGB getPixel(int x, int y)
{
mRGB bad(255,255,255);
assert(x >= 0 && x < nCols);
assert(y >= 0 && y < nRows);
return pixel[nCols * y + x];
}
};
#endif



Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@





#ifndef __SOLAR_SYSTEM_GLOBALS_H__

const GLfloat PI = 3.1415926535;
const GLfloat TO_RADIANS = PI/180.0;


const GLint INIT_WINDOW_POSITION[2] = { 150, 150 };
const GLfloat ASPECT_RATIO = 1.5;


const GLfloat LIGHT_0_POSITION[] = { 0.0, 0.0, 0.0, 1.0};
const GLfloat LIGHT_1_POSITION[] = { 0.0, 0.0, 1.0, 0.0};
const GLfloat LIGHT_AMBIENT[] = { 0.8, 0.8, 0.8, 1.0};
const GLfloat LIGHT_DIFFUSE[] = { 0.9, 0.9, 0.9, 1.0};
const GLfloat LIGHT_SPECULAR[] = { 1.0, 1.0, 1.0, 1.0};
const GLfloat LIGHT_MODEL_AMBIENT[] = { 0.2, 0.2, 0.2, 1.0};


const char EARTH_BMP_FILENAME[] = "earthmap.bmp";
const char MOON_BMP_FILENAME[] = "moonmap.bmp";
const GLfloat EARTH_RADIUS = 0.54;
const GLfloat MOON_RADIUS = 0.14;
const GLfloat MOON_ORBIT_RADIUS = 0.90;
const GLfloat EARTH_ROTATION = 1.00;
const GLfloat LUNAR_CYCLE = 5.0;


const GLfloat MAXIMUM_VIEWER_DISTANCE = 20;
const GLfloat MINIMUM_VIEWER_DISTANCE = 2;
const GLfloat INITIAL_VIEWER_DISTANCE = 5;
const GLfloat VIEWER_DISTANCE_INCREMENT = 0.1;
const GLfloat INITIAL_VIEWER_AZIMUTH = 0.0;
const GLfloat INITIAL_VIEWER_ZENITH = PI / 2.0;
const GLfloat VIEWER_ANGLE_INCREMENT = PI / 1.0;
const GLfloat LOOK_AT_POSITION[] = { 0.0, 0.0, 1.0 };

const char SUN_BMP_FILENAME[] = "sunmap.bmp";

/*
设置太阳的半径大小为1.5
*/
const GLfloat SUN_RADIUS = 1.5;

/*
设置地球公转半径大小为3.0
*/
const GLfloat EARTH_ORBIT_RADIUS = 3.0;

/*
设置地球自转周期为12
*/
const GLfloat EARTH_ORBIT_DUR = 12;

const GLfloat EARTH_INCLINATION = 7;


#define __SOLAR_SYSTEM_H__
#endif
Binary file not shown.
Loading