Browse Source

Faster (and less WTF-ish) computation of current pixel size for line drawing.

vrld 12 years ago
parent
commit
13dd8d3199
2 changed files with 15 additions and 9 deletions
  1. 12 8
      src/modules/graphics/opengl/Graphics.cpp
  2. 3 1
      src/modules/graphics/opengl/Graphics.h

+ 12 - 8
src/modules/graphics/opengl/Graphics.cpp

@@ -202,6 +202,9 @@ bool Graphics::setMode(int width, int height, WindowFlags *flags)
 
 
 	// Restore the display state.
 	// Restore the display state.
 	restoreState(tempState);
 	restoreState(tempState);
+	pixel_size_stack.clear();
+	pixel_size_stack.reserve(5);
+	pixel_size_stack.push_back(1);
 
 
 	// Get the maximum number of matrices
 	// Get the maximum number of matrices
 	// subtract a few to give the engine some room.
 	// subtract a few to give the engine some room.
@@ -241,6 +244,9 @@ void Graphics::reset()
 	Canvas::bindDefaultCanvas();
 	Canvas::bindDefaultCanvas();
 	Shader::detach();
 	Shader::detach();
 	restoreState(s);
 	restoreState(s);
+	pixel_size_stack.clear();
+	pixel_size_stack.reserve(5);
+	pixel_size_stack.push_back(1);
 }
 }
 
 
 void Graphics::clear()
 void Graphics::clear()
@@ -1090,19 +1096,12 @@ void Graphics::polyline(const float *coords, size_t count)
 	bool looping = (coords[0] == coords[count-2]) && (coords[1] == coords[count-1]);
 	bool looping = (coords[0] == coords[count-2]) && (coords[1] == coords[count-1]);
 
 
 	float halfwidth       = lineWidth/2.f;
 	float halfwidth       = lineWidth/2.f;
-	float pixel_size      = 1.f;
+	float pixel_size      = pixel_size_stack.back();
 	float overdraw_factor = .0f;
 	float overdraw_factor = .0f;
 
 
 	if (lineStyle == LINE_SMOOTH)
 	if (lineStyle == LINE_SMOOTH)
 	{
 	{
 		overdraw = new Vector[2*count+2];
 		overdraw = new Vector[2*count+2];
-		// TODO: is there a better way to get the pixel size at the current scale?
-		GLfloat m[16];
-		glGetFloatv(GL_MODELVIEW_MATRIX, m);
-		float det  = m[0]*m[5]*m[10] + m[4]*m[9]*m[2] + m[8]*m[1]*m[6];
-		det       -= m[2]*m[5]*m[8]  + m[6]*m[9]*m[0] + m[10]*m[1]*m[4];
-		pixel_size = 1.f / sqrtf(det);
-
 		overdraw_factor = pixel_size / halfwidth;
 		overdraw_factor = pixel_size / halfwidth;
 		halfwidth = std::max(.0f, halfwidth - .25f*pixel_size);
 		halfwidth = std::max(.0f, halfwidth - .25f*pixel_size);
 	}
 	}
@@ -1338,6 +1337,7 @@ void Graphics::push()
 		throw Exception("Maximum stack depth reached. (More pushes than pops?)");
 		throw Exception("Maximum stack depth reached. (More pushes than pops?)");
 	glPushMatrix();
 	glPushMatrix();
 	++userMatrices;
 	++userMatrices;
+	pixel_size_stack.push_back(pixel_size_stack.back());
 }
 }
 
 
 void Graphics::pop()
 void Graphics::pop()
@@ -1346,6 +1346,7 @@ void Graphics::pop()
 		throw Exception("Minimum stack depth reached. (More pops than pushes?)");
 		throw Exception("Minimum stack depth reached. (More pops than pushes?)");
 	glPopMatrix();
 	glPopMatrix();
 	--userMatrices;
 	--userMatrices;
+	pixel_size_stack.pop_back();
 }
 }
 
 
 void Graphics::rotate(float r)
 void Graphics::rotate(float r)
@@ -1356,6 +1357,7 @@ void Graphics::rotate(float r)
 void Graphics::scale(float x, float y)
 void Graphics::scale(float x, float y)
 {
 {
 	glScalef(x, y, 1);
 	glScalef(x, y, 1);
+	pixel_size_stack.back() *= 2. / double(x + y);
 }
 }
 
 
 void Graphics::translate(float x, float y)
 void Graphics::translate(float x, float y)
@@ -1373,6 +1375,8 @@ void Graphics::shear(float kx, float ky)
 void Graphics::origin()
 void Graphics::origin()
 {
 {
 	glLoadIdentity();
 	glLoadIdentity();
+	pixel_size_stack.clear();
+	pixel_size_stack.push_back(1);
 }
 }
 
 
 bool Graphics::hasFocus() const
 bool Graphics::hasFocus() const

+ 3 - 1
src/modules/graphics/opengl/Graphics.h

@@ -23,7 +23,8 @@
 
 
 // STD
 // STD
 #include <iostream>
 #include <iostream>
-#include <cmath>
+#include <stack>
+#include <vector>
 
 
 // OpenGL
 // OpenGL
 #include "OpenGL.h"
 #include "OpenGL.h"
@@ -546,6 +547,7 @@ private:
 	Font *currentFont;
 	Font *currentFont;
 	love::window::Window *currentWindow;
 	love::window::Window *currentWindow;
 
 
+	std::vector<double> pixel_size_stack; // stores current size of a pixel (needed for line drawing)
 	LineStyle lineStyle;
 	LineStyle lineStyle;
 	float lineWidth;
 	float lineWidth;
 	GLint matrixLimit;
 	GLint matrixLimit;