Browse files

Lighting working

  • Loading branch information...
1 parent 76529f3 commit 00912acdc8f70691aa64f221ff06c8da1b249c68 @asivitz committed Jul 13, 2012
Showing with 88 additions and 36 deletions.
  1. +64 −26 ext/Platform.cpp
  2. +5 −1 ext/Platform.h
  3. +1 −1 ext/Renderer.cpp
  4. +18 −8 main.rb
View
90 ext/Platform.cpp
@@ -15,6 +15,25 @@
using namespace Rice;
using namespace std;
+void checkGLError()
+{
+ for (int e = glGetError(); e != GL_NO_ERROR; e = glGetError())
+ {
+ cout << "******************GLERROR****************";
+ switch (e)
+ {
+ case GL_INVALID_ENUM: cout << "OpenGL: Invalid enum" << endl; break;
+ case GL_INVALID_VALUE: cout << "OpenGL: Invalid value" << endl; break;
+ case GL_INVALID_OPERATION: cout << "OpenGL: Invalid operation" << endl; break;
+ case GL_STACK_OVERFLOW: cout << "OpenGL: Stack overflow" << endl; break;
+ case GL_STACK_UNDERFLOW: cout << "OpenGL: Stack underflow" << endl; break;
+ case GL_OUT_OF_MEMORY: cout << "OpenGL: Out of memory" << endl; break;
+ default: cout << "OpenGL: Unknown Error" << endl;
+ }
+ }
+}
+
+
Platform::Platform(Object self) : Rice::Director(self)
{
window = new sf::Window(sf::VideoMode(600, 600), "OpenGL");
@@ -23,9 +42,8 @@ Platform::Platform(Object self) : Rice::Director(self)
renderer = new Renderer();
- lightMap = new sf::RenderTexture();
- if (!lightMap->create(600, 600, false))
- cout << "ERROR: Couldn't create lightMap" << endl;
+ lightFramebuffer = lightImage = 0;
+ createLightFramebuffer();
}
Platform::~Platform()
@@ -47,6 +65,43 @@ void Platform::setPhysics(Physics * phys)
cout << "Couldn't create shader" << endl;
}
+const int lightWidth = 600;
+const int lightHeight = 600;
+
+void Platform::createLightFramebuffer()
+{
+ if (!lightFramebuffer)
+ {
+ // [EAGLContext setCurrentContext:context];
+
+ // Create default framebuffer object.
+ glGenFramebuffers(1, &lightFramebuffer);
+ glBindFramebuffer(GL_FRAMEBUFFER, lightFramebuffer);
+
+ glGenTextures(1, &lightImage);
+ glBindTexture(GL_TEXTURE_2D, lightImage);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, lightWidth, lightHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, lightImage, 0);
+
+ if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+ cout << "Failed to make complete framebuffer object " << glCheckFramebufferStatus(GL_FRAMEBUFFER) << endl;
+
+ checkGLError();
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ }
+}
+
+void Platform::switchToLightFramebuffer()
+{
+ glBindFramebuffer(GL_FRAMEBUFFER, lightFramebuffer);
+ glViewport(0, 0, lightWidth, lightHeight);
+}
+
void Platform::draw()
{
sf::Event event;
@@ -65,22 +120,23 @@ void Platform::draw()
}
}
- lightMap->setActive(true);
+ switchToLightFramebuffer();
glViewport(0, 0, 600, 600);
renderer->drawLights();
- lightMap->display();
+ glBindFramebuffer(GL_FRAMEBUFFER,0);
+ //lightMap->display();
//lightMap->getTexture().copyToImage().saveToFile("out.png");
- bool draw_debug = true;
+ bool draw_debug = false;
window->setActive(true);
glViewport(0, 0, 600, 600);
renderer->draw();
const float lightModel[]=
{60, 0, 0, 0,
- 0, 60, 0, 0,
+ 0, -60, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -89,7 +145,7 @@ void Platform::draw()
{
ShaderProgram * prog = renderer->program;
glEnable(GL_BLEND);
- lightMap->getTexture().bind();
+ glBindTexture(GL_TEXTURE_2D, lightImage);
glUniform1i(prog->locationOfTex(), 0);
//glUniform4f(prog->locationOfUniform("color"), 1.0, 1.0, 1.0, 1.0);
//glUniformMatrix4fv(prog->locationOfUniform("viewMat"), 1, GL_FALSE, renderer->viewMatrix);
@@ -153,24 +209,6 @@ void Platform::addLightCommand(int texid, Array a)
}
}
-void checkGLError()
-{
- for (int e = glGetError(); e != GL_NO_ERROR; e = glGetError())
- {
- cout << "******************GLERROR****************";
- switch (e)
- {
- case GL_INVALID_ENUM: cout << "OpenGL: Invalid enum" << endl; break;
- case GL_INVALID_VALUE: cout << "OpenGL: Invalid value" << endl; break;
- case GL_INVALID_OPERATION: cout << "OpenGL: Invalid operation" << endl; break;
- case GL_STACK_OVERFLOW: cout << "OpenGL: Stack overflow" << endl; break;
- case GL_STACK_UNDERFLOW: cout << "OpenGL: Stack underflow" << endl; break;
- case GL_OUT_OF_MEMORY: cout << "OpenGL: Out of memory" << endl; break;
- default: cout << "OpenGL: Unknown Error" << endl;
- }
- }
-}
-
int Platform::loadImage(string fileName)
{
sf::Image image;
View
6 ext/Platform.h
@@ -18,7 +18,9 @@ class Platform : public Rice::Director
Physics * physics;
sf::Window * window;
- sf::RenderTexture * lightMap;
+
+ GLuint lightFramebuffer;
+ GLuint lightImage;
public:
Platform(Object self);
@@ -32,6 +34,8 @@ class Platform : public Rice::Director
bool isWindowOpen();
int loadImage(string fileName);
void setPhysics(Physics * phys);
+ void switchToLightFramebuffer();
+ void createLightFramebuffer();
};
#endif
View
2 ext/Renderer.cpp
@@ -124,7 +124,7 @@ void Renderer::draw()
void Renderer::drawLights()
{
- glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
+ glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
View
26 main.rb
@@ -9,6 +9,16 @@
# maps physics body ids to game objects
$body_map = {}
$drawables = []
+$tex_map = {}
+
+def load_image name
+ texid = $tex_map[name]
+ if not texid
+ texid = $platform.loadImage name
+ $tex_map[name] = texid
+ end
+ texid
+end
MAX_VEL = 10.0
MOVE_IMP = 4.0
@@ -18,7 +28,7 @@ class Player < WorldObj
attr_accessor :num_standing_on_solid
def initialize pos
- @texid = $platform.loadImage "2player\ shooter/images/redGuyLeft.png"
+ @texid = load_image "2player\ shooter/images/redGuyLeft.png"
#@texid = $platform.loadImage "images/triangle.png"
@body = $physics.addPlayer(pos, 2.0)
$body_map[@body.id] = self
@@ -125,20 +135,20 @@ class Grenade < WorldObj
attr_reader :body
def initialize pos
- @texid = $platform.loadImage "images/light.png"
+ @texid = load_image "images/triangle.png"
+ @light_texid = load_image "images/light.png"
@body = $physics.addGrenade(pos,1.0)
$body_map[@body.id] = self
end
- def mat
+ def draw
m = Matrix.identity(4)
pos = @body.pos
m = m.translate(pos[0],pos[1],0)
- m = m.scale(18,18,1)
- end
-
- def draw
- $platform.addLightCommand(@texid, self.mat.flatten)
+ mat = m.scale(3,3,1)
+ $platform.addDrawCommand(@texid, mat.flatten)
+ light_mat = m.scale(25,25,1)
+ $platform.addLightCommand(@light_texid, light_mat.flatten)
end
end

0 comments on commit 00912ac

Please sign in to comment.