NCBI C++ ToolKit
gltest.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: gltest.cpp 24939 2011-12-29 14:52:50Z wuliangs $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Mike DiCuccio
27  *
28  * File Description:
29  * CGlTestApp -- test of OpenGL framework for rendering images as a CGI app
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <cgi/cgictx.hpp>
34 #include <corelib/ncbifile.hpp>
35 
37 
38 #include <gui/opengl/glutils.hpp>
39 #include <gui/opengl/glcamera.hpp>
40 //#include <GL/glut.h>
41 
42 using namespace ncbi;
43 
44 
45 /////////////////////////////////////////////////////////////////////////////
46 // CGlTestApplication::
47 //
48 
50 {
51 public:
53 
54  virtual void Render(CCgiContext& ctx);
55 
56 private:
57  // camrea set-up
59 };
60 
61 
62 
64 {
65  m_Camera.SetLayout(CGlCamera::ePerspective);
66  m_Camera.SetFieldOfView(45.0f);
67  m_Camera.SetNearPlane(0.1f);
68  m_Camera.SetFarPlane(500.0f);
69 }
70 
71 
73 {
74  // retrieve our CGI rendering params
75  const CCgiRequest& request = ctx.GetRequest();
76 
77  const TCgiEntries& entries = request.GetEntries();
78 
79  TCgiEntries::const_iterator fov_iter = entries.find("fov");
80  TCgiEntries::const_iterator dist_iter = entries.find("dist");
81  TCgiEntries::const_iterator xrot_iter = entries.find("xrot");
82  TCgiEntries::const_iterator yrot_iter = entries.find("yrot");
83  TCgiEntries::const_iterator zrot_iter = entries.find("zrot");
84  //TCgiEntries::const_iterator rad1_iter = entries.find("rad1");
85  //TCgiEntries::const_iterator rad2_iter = entries.find("rad2");
86 
87  float dist = 40.0f;
88  float xrot = 0.0f;
89  float yrot = 0.0f;
90  float zrot = 0.0f;
91  //float rad1 = 2.0f;
92  //float rad2 = 7.0f;
93 
94  if (fov_iter != entries.end()) {
95  m_Camera.SetFieldOfView(NStr::StringToDouble(fov_iter->second));
96  }
97 
98  if (dist_iter != entries.end()) {
99  dist = NStr::StringToDouble(dist_iter->second);
100  }
101 
102  if (xrot_iter != entries.end()) {
103  xrot = NStr::StringToDouble(xrot_iter->second);
104  }
105 
106  if (yrot_iter != entries.end()) {
107  yrot = NStr::StringToDouble(yrot_iter->second);
108  }
109 
110  if (zrot_iter != entries.end()) {
111  zrot = NStr::StringToDouble(zrot_iter->second);
112  }
113 
114  //if (rad1_iter != entries.end()) {
115  // rad1 = NStr::StringToDouble(rad1_iter->second);
116  //}
117 
118  //if (rad2_iter != entries.end()) {
119  // rad2 = NStr::StringToDouble(rad2_iter->second);
120  //}
121 
122  //
123  // do some rendering
124  //
125 
126  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
127  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
128 
129  glMatrixMode(GL_PROJECTION);
130  glLoadIdentity();
131  glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
132  glBegin(GL_QUADS);
133  glColor3f(0.2f, 0.2f, 0.2f);
134  glVertex2f(-1.0f, 1.0f);
135  glVertex2f( 1.0f, 1.0f);
136 
137  glColor3f(0.4f, 0.4f, 0.8f);
138  glVertex2f( 1.0f, -1.0f);
139  glVertex2f(-1.0f, -1.0f);
140  glEnd();
141 
142  m_Camera.MakeCurrent();
143 
144  glMatrixMode(GL_MODELVIEW);
145  glLoadIdentity();
146 
147  //
148  // lighting - done against an identity matrix
149  //
150  static GLfloat light_pos[] = { -20.0f, 20.0f, 5.0f, 1.0f };
151  static GLfloat white_light[] = { 1.0f, 1.0f, 1.0f, 1.0f };
152  glEnable(GL_LIGHTING);
153  glEnable(GL_LIGHT0);
154  glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
155  glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
156  glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
157  glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
158 
159  //
160  // world-view setup
161  //
162  glTranslatef(0.0f, 0.0f, -dist);
163  glRotatef(xrot, 1.0f, 0.0f, 0.0f);
164  glRotatef(yrot, 0.0f, 1.0f, 0.0f);
165  glRotatef(zrot, 0.0f, 0.0f, 1.0f);
166 
167 
168  //
169  // make things a bit faster
170  //
171  glEnable(GL_DEPTH_TEST);
172  glEnable(GL_CULL_FACE);
173  //glCullFace(GL_BACK);
174 
175  //
176  // some interesting solids
177  // we use different material properties for each
178  //
179  glEnable(GL_COLOR_MATERIAL);
180 
181  glColor3f(1.0f, 0.5f, 0.0f);
182  glPushMatrix();
183  glTranslatef(5.0f, 0.0f, 0.0f);
184  //glutSolidTorus(rad1, rad2, 30, 60);
185  glPopMatrix();
186 
187  glColor3f(0.0f, 1.0f, 0.0f);
188  glPushMatrix();
189  glTranslatef(-5.0f, 0.0f, 0.0f);
190  glRotatef(90, 1.0f, 0.0f, 0.0f);
191  //glutSolidTorus(rad1, rad2, 30, 60);
192  glPopMatrix();
193 
194  //
195  // debugging: draw our axes
196  //
197  glDisable(GL_LIGHTING);
198  glBegin(GL_LINES);
199 
200  glColor3f (1.0f, 0.0f, 0.0f);
201  glVertex3f(0.0f, 0.0f, 0.0f);
202  glVertex3f(5.0f, 0.0f, 0.0f);
203 
204  glColor3f (0.0f, 1.0f, 0.0f);
205  glVertex3f(0.0f, 0.0f, 0.0f);
206  glVertex3f(0.0f, 5.0f, 0.0f);
207 
208  glColor3f (0.0f, 0.0f, 1.0f);
209  glVertex3f(0.0f, 0.0f, 0.0f);
210  glVertex3f(0.0f, 0.0f, 5.0f);
211 
212  glEnd();
213 
215 }
216 
217 
218 
219 /////////////////////////////////////////////////////////////////////////////
220 // MAIN
221 //
222 
223 int main(int argc, const char* argv[])
224 {
225  int result = CGlTestApplication().AppMain(argc, argv, 0, eDS_Default, 0);
226  _TRACE("back to normal diags");
227  return result;
228 }
virtual void Render(CCgiContext &ctx)
Definition: gltest.cpp:72
CCgiRequest::
Definition: ncbicgi.hpp:676
const_iterator end() const
Definition: map.hpp:292
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:626
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static double StringToDouble(const CTempStringEx str, TStringToNumFlags flags=0)
Convert string to double.
Definition: ncbistr.cpp:1288
static wxAcceleratorEntry entries[3]
const_iterator find(const key_type &key) const
Definition: map.hpp:293
Try standard log file (app.name + ".log") in /log/, use stderr on failure.
Definition: ncbidiag.hpp:1763
Magic spell ;-) needed for some weird compilers... very empiric.
const TCgiEntries & GetEntries(void) const
Get a set of entries(decoded) received from the client.
Definition: ncbicgi.hpp:1154
int main(int argc, const char *argv[])
Definition: gltest.cpp:223
static void DumpState()
Dump many of the most common OpenGL states.
Definition: glutils.cpp:203
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock, CFileIO, CFileReader, CFileWriter, CFileReaderWriter, CFileException.
CGlCamera m_Camera
Definition: gltest.cpp:58
static CS_CONTEXT * ctx
Definition: ct_dynamic.c:24
#define _TRACE(message)
Definition: ncbidbg.hpp:120
static int result
Definition: cursor5.c:11
const CCgiRequest & GetRequest(void) const
Definition: cgictx.hpp:374
Modified on Tue Jan 16 15:41:12 2018 by modify_doxy.py rev. 546573