Fixed some transforms, move to 3d and quaternion math in progress...
[ne.git] / src / mods / sandbox / run.cpp
blobc150f9bbbd1779be28d904427a6538aabf502d23
1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
18 #include "backend/video.h"
19 #include "backend/input.h"
20 #include "backend/texture.h"
21 #include "backend/shapes.h"
23 #include "phys/camera.h"
24 #include "phys/world.h"
25 #include "phys/generic_objects.h"
26 #include "phys/player.h"
27 #include "phys/conversion.h"
29 #include "mods/mod.h"
31 #include "base/time.h"
33 #include "planet.h"
35 #include <GL/gl.h>
36 #include <GL/glu.h>
37 #include <vector>
39 using namespace SandBox;
41 int sandbox_run(void *);
43 /* see mods/list.h for what this is for... */
44 struct mod sandbox_mod = {
45 sandbox_run,
46 "Sandbox Demo",
47 "A demo for testing features"
50 int gl_init();
52 int sandbox_run(void *)
54 Camera cam;
56 //int width = v_info()->width;
57 //int height = v_info()->height;
59 gl_init();
61 int *screen_pos;
62 double *gl_pos;
64 World *w = World::Instance();
66 std::vector<Object*> objects;
68 Texture home_tex("data/material/moon.jpg");
70 Planet home(&home_tex, 128, 0.02);
71 home.setStatic(true);
73 Player player(5);
74 //Sphere player(5, 1);
75 float player_speed = 500;
76 player.setPosition(0.0, 128.0 + 8, 0.0);
77 objects.push_back(&player);
79 //cam.follow(&player);
80 cam.setOffset(Vec3f(0.0, 10.0, 10.0));
81 cam.setSpeed(0.5);
83 cam.setPosition(Vec3f(100.0, 0.0, 400.0));
84 //cam.setPosition(Vec3f(0.0, 128.0, 100.0));
85 //cam.lookAt(player.getPositionV());
87 Time::Stamp delta = 0;
88 Time::Stamp frame_time = Time::Now();
90 int done = 0;
91 while (!done)
93 frame_time = Time::Now();
95 glClearColor(0,0,0,0);
96 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
98 glMatrixMode(GL_MODELVIEW);
99 glLoadIdentity();
101 cam.apply();
104 Vec3f pos = player.getPositionV();
105 glColor3f(1.0, 0.0, 0.0);
106 player.getUp().draw(pos, 10);
107 player.getForward().draw(pos, 10);
108 glColor3f(0.0, 1.0, 0.0);
109 pos.draw(pos, 30);
112 for (size_t i=0; i<objects.size(); i++) {
113 objects[i]->clearGravity();
114 home.applyGravity(objects[i]);
115 objects[i]->update();
118 cam.update();
120 i_pump();
122 keysym_t *key = i_keystate();
124 if (key[K_ESCAPE]) {
125 //done = 1;
126 exit(0);
129 if (key[K_LEFT]) {
130 //player.getBody()->addRelForce(player_speed, 0.0, 0.0);
131 player.getBody()->addTorque(0.0, player_speed * 0.1, 0.0);
133 if (key[K_RIGHT]) {
134 //player.getBody()->addRelForce(-player_speed, 0.0, 0.0);
135 player.getBody()->addTorque(0.0, -player_speed * 0.1, 0.0);
137 if (key[K_UP]) {
138 player.getBody()->addRelForce(0.0, 0.0, player_speed);
140 if (key[K_DOWN]) {
141 player.getBody()->addRelForce(0.0, 0.0, -player_speed);
144 w->step();
146 screen_pos = i_mpos();
147 gl_pos = v_unproject(screen_pos[0],screen_pos[1],1);
149 glEnable(GL_LIGHTING);
150 glEnable(GL_LIGHT0);
151 glEnable(GL_LIGHT1);
153 glDisable(GL_LIGHTING);
155 glDisable(GL_TEXTURE_2D);
156 glDisable(GL_DEPTH_TEST);
157 if (0) {
158 glPushMatrix();
159 glTranslatef(gl_pos[0],gl_pos[1],0);
160 glColor3f(1,0,0);
161 glBegin(GL_LINES);
162 glVertex2f(-10,-10);
163 glVertex2f(10,10);
164 glVertex2f(-10,10);
165 glVertex2f(10,-10);
166 glEnd();
167 glPopMatrix();
169 glEnable(GL_DEPTH_TEST);
171 glEnable(GL_TEXTURE_2D);
172 glColor3f(1.0, 1.0, 1.0);
173 home.draw();
175 glEnable(GL_LIGHTING);
176 glDisable(GL_TEXTURE_2D);
177 glColor3f(1.0, 0.0, 0.0);
178 player.draw();
179 static float rot = 0;
180 rot += 0.1;
182 v_flip();
184 delta = Time::Now() - frame_time;
185 if (20 > delta) {
186 Time::Sleep(20 - delta);
190 return 0;
193 int gl_init()
195 //glClearColor(0,0,0,0);
197 glMatrixMode(GL_PROJECTION);
198 glLoadIdentity();
200 double w = v_info()->width, h = v_info()->height;
201 double aspect = w/h;
203 gluPerspective(45,aspect,1,3000);
205 glEnable(GL_DEPTH_TEST);
206 glClearDepth(1);
208 glEnable(GL_CULL_FACE);
209 glFrontFace(GL_CCW);
211 glShadeModel(GL_SMOOTH);
213 //glEnable(GL_TEXTURE_2D);
214 glEnable(GL_BLEND);
215 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
217 return 0;