1 /***************************************************************************
2 objectviewer.cpp - Viewing CGraphObj objects
3 -------------------
4 begin : wo sep 06 2006
5 copyright : (C) 2006 by CJP
6 email : cornware-cjp@users.sourceforge.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17 #include <cstdio>
18 #include <cmath>
19 #include <GL/gl.h>
20
21 #include "objectviewer.h"
22 #include "usmacros.h"
23
24 #include "graphobj.h"
25 #include "texture.h"
26
CObjectViewer(CDataManager * world)27 CObjectViewer::CObjectViewer(CDataManager *world)
28 {
29 m_World = world;
30 m_Reflection = NULL;
31 }
32
~CObjectViewer()33 CObjectViewer::~CObjectViewer()
34 {
35 if(m_Reflection != NULL) delete m_Reflection;
36 }
37
setReflection(const CString & filename)38 void CObjectViewer::setReflection(const CString &filename)
39 {
40 if(m_Reflection != NULL) delete m_Reflection;
41
42 CStaticReflection *ref = new CStaticReflection;
43 ref->load(filename, CParamList());
44 m_Reflection = ref;
45 }
46
addObject(const CString & filename,CParamList list,CVector pos,CMatrix ori,bool reflect,CDataObject::eDataType type,int replace)47 void CObjectViewer::addObject(
48 const CString &filename, CParamList list, CVector pos,
49 CMatrix ori, bool reflect, CDataObject::eDataType type, int replace)
50 {
51 if(replace >= 0 && replace < int(m_Objects.size()))
52 {
53 //Re-use object
54
55 reloadData();
56 CGraphObj *obj = (CGraphObj *)
57 (m_World->getObject(m_Objects[replace].type, m_Objects[replace].objectID));
58
59 if(obj != NULL) obj->load(filename, list); //re-use CGraphObj object, if existing
60
61 m_Objects[replace].filename = filename;
62 m_Objects[replace].parameters = list;
63 m_Objects[replace].position = pos;
64 m_Objects[replace].orientation = ori;
65 m_Objects[replace].reflectInGround = reflect;
66 reloadData();
67 }
68 else
69 {
70 //Add new object
71 SObject obj;
72 obj.filename = filename;
73 obj.parameters = list;
74 obj.type = type;
75 obj.position = pos;
76 obj.orientation = ori;
77 obj.reflectInGround = reflect;
78 m_Objects.push_back(obj);
79 reloadData();
80 }
81 }
82
addTexture(const CString & filename)83 void CObjectViewer::addTexture(const CString &filename)
84 {
85 STexture tex;
86 tex.filename = filename;
87 m_Textures.push_back(tex);
88 reloadData();
89 }
90
update()91 void CObjectViewer::update()
92 {
93 //set up viewport
94 CRenderer::update();
95
96 //printf("Updating graphics\n");
97
98 //Clear the screen
99 /*
100 if(m_Settings.m_ZBuffer)
101 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
102 else
103 glClear( GL_COLOR_BUFFER_BIT );
104 */
105 glDisable(GL_FOG);
106
107 //Lighting:
108 GLfloat light_color[] = {1.0, 1.0, 1.0, 1.0};
109 GLfloat specular_color[] = {3.0, 3.0, 3.0, 1.0};
110 GLfloat ambient_color[] = {0.0, 0.0, 0.0, 1.0};
111 GLfloat light_direction[] = {0.0, 0.0, 1.0, 0.0};
112
113 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);
114 glLightfv(GL_LIGHT0, GL_SPECULAR, specular_color);
115 glLightfv(GL_LIGHT0, GL_POSITION, light_direction);
116 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient_color);
117
118
119 //Set the camera
120 const CMatrix &cammat = m_Camera->getOrientation();
121 glLoadMatrixf(cammat.transpose().gl_mtr());
122 const CVector &camera = m_Camera->getPosition();
123 glTranslatef (-camera.x, -camera.y, -camera.z);
124
125 glColor4f(1,1,1,1);
126
127 glPushMatrix();
128 glScalef(1.0,-1.0,1.0);
129 glCullFace(GL_FRONT);
130
131 //The reflection
132 viewObjects(true);
133
134 glCullFace(GL_BACK); //back to default
135 glPopMatrix();
136
137 //The object itself:
138 viewObjects(false);
139 }
140
viewObjects(bool isReflection)141 void CObjectViewer::viewObjects(bool isReflection)
142 {
143 /*
144 if(!isReflection)
145 {
146 glDisable(GL_TEXTURE_2D);
147 glDisable(GL_LIGHTING);
148
149 glBegin(GL_LINE_LOOP);
150 glVertex3f(-30,0,-30);
151 glVertex3f( 30,0,-30);
152 glVertex3f( 30,0, 30);
153 glVertex3f(-30,0, 30);
154 glEnd();
155 glBegin(GL_LINES);
156 glVertex3f(0, 0,0);
157 glVertex3f(0,18,0);
158 glEnd();
159
160 glEnable(GL_TEXTURE_2D);
161 glEnable(GL_LIGHTING);
162 }
163 */
164
165 for(unsigned int i=0; i < m_Objects.size(); i++)
166 {
167 CGraphObj *obj = (CGraphObj *)
168 (m_World->getObject(m_Objects[i].type, m_Objects[i].objectID));
169
170 if(obj == NULL) continue; //error
171
172 if(isReflection && !(m_Objects[i].reflectInGround))
173 continue; //not in reflection
174
175 glPushMatrix();
176 glTranslatef(m_Objects[i].position.x, m_Objects[i].position.y, m_Objects[i].position.z);
177 glMultMatrixf(m_Objects[i].orientation.gl_mtr());
178
179 if(m_Settings.m_Transparency == SGraphicSettings::off)
180 {
181 obj->draw(&m_Settings, NULL, 1, 0.0, true);
182 }
183 else
184 {
185 obj->draw(&m_Settings, m_Reflection, 1, 0.0, true);
186 }
187
188 glPopMatrix();
189 }
190 }
191
updateScreenSize()192 void CObjectViewer::updateScreenSize()
193 {
194 ; //Don't update screen size: it is filled in from the outside
195 }
196
reloadData()197 void CObjectViewer::reloadData()
198 {
199 for(unsigned int i=0; i < m_Textures.size(); i++)
200 {
201 m_Textures[i].textureID = m_World->loadObject(
202 m_Textures[i].filename, CParamList(), CDataObject::eMaterial);
203 }
204
205 for(unsigned int i=0; i < m_Objects.size(); i++)
206 {
207 m_Objects[i].objectID = m_World->loadObject(
208 m_Objects[i].filename, m_Objects[i].parameters, m_Objects[i].type);
209 }
210 }
211