-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelerview.cpp
More file actions
140 lines (124 loc) · 3.54 KB
/
modelerview.cpp
File metadata and controls
140 lines (124 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "modelerview.h"
#include "camera.h"
#include "bitmap.h"
#include "meta_drawing.h"
#include <FL/Fl.H>
#include <FL/Fl_Gl_Window.h>
#include <FL/gl.h>
#include <GL/glu.h>
#include <cstdio>
static const int kMouseRotationButton = FL_LEFT_MOUSE;
static const int kMouseTranslationButton = FL_MIDDLE_MOUSE;
static const int kMouseZoomButton = FL_RIGHT_MOUSE;
int initTexture() {
unsigned char* texture;
int texWidth;
int texHeight;
//load image from file
if((texture = readBMP("texture3.bmp", texWidth, texHeight))==NULL) {
printf("Can't load bitmap!\n");
return 0;
}
printf("load success\n");
printf("textrue size %d, %d\n", texWidth, texHeight);
//Tell openGL to ignore padding at ends of rows
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE/*GL_REPLACE*/);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight,
0, GL_BGR_EXT, GL_UNSIGNED_BYTE,texture);
//Typical Texture Generation Using Data From Bitmapt
glEnable(GL_TEXTURE_2D);
return true;
}
ModelerView::ModelerView(int x, int y, int w, int h, char *label)
: Fl_Gl_Window(x,y,w,h,label)
{
m_camera = new Camera();
}
ModelerView::~ModelerView()
{
delete m_camera;
}
int ModelerView::handle(int event)
{
unsigned eventCoordX = Fl::event_x();
unsigned eventCoordY = Fl::event_y();
unsigned eventButton = Fl::event_button();
unsigned eventState = Fl::event_state();
switch(event)
{
case FL_PUSH:
{
switch(eventButton)
{
case kMouseRotationButton:
m_camera->clickMouse(kActionRotate, eventCoordX, eventCoordY );
break;
case kMouseTranslationButton:
m_camera->clickMouse(kActionTranslate, eventCoordX, eventCoordY );
break;
case kMouseZoomButton:
m_camera->clickMouse(kActionZoom, eventCoordX, eventCoordY );
break;
}
// printf("push %d %d\n", eventCoordX, eventCoordY);
}
break;
case FL_DRAG:
{
m_camera->dragMouse(eventCoordX, eventCoordY);
//printf("drag %d %d\n", eventCoordX, eventCoordY);
}
break;
case FL_RELEASE:
{
switch(eventButton)
{
case kMouseRotationButton:
case kMouseTranslationButton:
case kMouseZoomButton:
m_camera->releaseMouse(eventCoordX, eventCoordY );
break;
}
// printf("release %d %d\n", eventCoordX, eventCoordY);
}
break;
default:
return 0;
}
redraw();
return 1;
}
static GLfloat lightPosition0[] = { 4, 2, -4, 0 };
static GLfloat lightDiffuse0[] = { 1,1,1,1 };
static GLfloat lightPosition1[] = { -2, 1, 5, 0 };
static GLfloat lightDiffuse1[] = { 2, 2, 2, 2 };
void ModelerView::draw()
{
if (!valid())
{
glShadeModel( GL_SMOOTH );
glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHT1 );
glEnable( GL_NORMALIZE );
initTexture();
}
glViewport( 0, 0, w(), h() );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0,float(w())/float(h()),1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_camera->applyViewingTransform();
glLightfv( GL_LIGHT0, GL_POSITION, lightPosition0 );
glLightfv( GL_LIGHT0, GL_DIFFUSE, lightDiffuse0 );
glLightfv( GL_LIGHT1, GL_POSITION, lightPosition1 );
glLightfv( GL_LIGHT1, GL_DIFFUSE, lightDiffuse1 );
}