Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


AlanSmithee

Member Since 19 May 2011
Offline Last Active Today, 05:37 AM

Topics I've Started

Problem with perspective in opengl

23 November 2014 - 01:21 PM

Hello.

 

I am experiencing a perspective problem when rendering a model in OpenGL.

I am pretty sure there is some problem with my MVP matrix, but I am unable to deduce what it could be.

 

I am using the identity matrix for my model and the projection matrix and view matrix is defined as below:

glm::mat4 _projectionMatrix = glm::perspective(45.0f, _screenDimensions.x / _screenDimensions.y, 0.1f, 100.0f);
glm::mat4 _viewMatrix = glm::lookAt(glm::vec3(4,0,0), glm::vec3(0,0,0), glm::vec3(0,1,0));

they are multiplied and sent to the vertex shader:

auto mvpMatrix =  _projectionMatrix * _viewMatrix * glm::mat4(1.0f);
glUniformMatrix4fv(mvpUniformHandle, 1, GL_FALSE, glm::value_ptr(mvpMatrix));

and used in the shaders:

// Vertex shader
#version 330
 
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 TexCoord;
 
uniform mat4 MVP;
 
out vec2 TexCoord0;
 
void main()
{
    gl_Position = MVP * vec4(Position, 1);
    TexCoord0 = TexCoord;
}
 
// Fragment shader

#version 330
 
precision highp float;
 
in vec2 TexCoord0;
 
uniform sampler2D TextureSampler;
 
out vec4 FragColor;
 
void main()
{
    FragColor = texture2D(TextureSampler, TexCoord0.xy);
}

And the result I am getting can be seen in attached file "wrong.png"

 

However, when searching for answers I found a post at SO where someone was trying to do something with a raycastand I noticed that the asker had defined his/her projection matrix with the inverse of the aspect ratio, so I tried doing that:

glm::mat4 _projectionMatrix = glm::perspective(45.0f, -(_screenDimensions.x / _screenDimensions.y), 0.1f, 100.0f);

and the results actually seem to be correct! (see attached file "right.png")

 

I also added files "wrong2.png" and "right2.png" which is the model with some rotation / scaling / translation

 

But all this makes absolutely 0 sense to me, and makes me think that there is something else that is wrong with my code.

Why would passing a negative aspect ratio make it look as it should (if it even is as it should).

 

I am fairly certain there is a  problem in either the MVP matrix or how it is used in the shader.

I have used the model data in a webgl application and it rendered just fine there so the vertecies position and texture coord data is fine.

 

Here is the model loading / passing code for completion sake

// Vertecies, position / texture coord
Vertex vertices[] =
                       {
                           // Front face
                           Vertex(glm::vec3(-1.0, -1.0,  1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3( 1.0, -1.0,  1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0,  1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3(-1.0,  1.0,  1.0), glm::vec2(0.0, 1.0)),
 
                           // Back face
                           Vertex(glm::vec3(-1.0, -1.0, -1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3(-1.0,  1.0, -1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3( 1.0,  1.0, -1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0, -1.0), glm::vec2(0.0, 0.0)),
 
                           // Top face
                           Vertex(glm::vec3(-1.0,  1.0, -1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3(-1.0,  1.0,  1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0,  1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0, -1.0), glm::vec2(1.0, 1.0)),
 
                           // Bottom face
                           Vertex(glm::vec3(-1.0, -1.0, -1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0, -1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0,  1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3(-1.0, -1.0,  1.0), glm::vec2(1.0, 0.0)),
 
                           // Right face
                           Vertex(glm::vec3( 1.0, -1.0, -1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3( 1.0,  1.0, -1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3( 1.0,  1.0,  1.0), glm::vec2(0.0, 1.0)),
                           Vertex(glm::vec3( 1.0, -1.0,  1.0), glm::vec2(0.0, 0.0)),
 
                           // Left face
                           Vertex(glm::vec3(-1.0, -1.0, -1.0), glm::vec2(0.0, 0.0)),
                           Vertex(glm::vec3(-1.0, -1.0,  1.0), glm::vec2(1.0, 0.0)),
                           Vertex(glm::vec3(-1.0,  1.0,  1.0), glm::vec2(1.0, 1.0)),
                           Vertex(glm::vec3(-1.0,  1.0, -1.0), glm::vec2(0.0, 1.0))
                       };
 
        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 

        unsigned int Indices[] =
                     {
                         0,  1,  2,    0,  2,  3, // Front face
                         4,  5,  6,    4,  6,  7, // Back face
                         8,  9, 10,    8, 10, 11, // Top face
                        12, 13, 14,   12, 14, 15, // Bottom face
                        16, 17, 18,   16, 18, 19, // Right face
                        20, 21, 22,   20, 22, 23  // Left face
                     };
 
        glGenBuffers(1, &IBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
 
// Draw function

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glUniformMatrix4fv(mvpUniform, 1, GL_FALSE, glm::value_ptr(mvpMatrix));
 
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
 
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
 
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
 

        glActiveTexture(_textureUnit);
        glBindTexture(_textureTarget, _textureId);

 
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
        glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
 
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);


Anyone have any suggestion as to what could be wrong?

 

Thanks in advance for your help!


building luabind for gcc 4.8.1

15 January 2014 - 08:25 PM

Hi.

 

I hope this is the right forum for this.

 

I am having some major problems getting luabind to even compile with mingw.

I have searched and followed every tutorial / stackoverflow thread / other documentation available and it doesn't help.

 

Steps taken:

 

1. Downloaded boost and build bjam with cmd "bootstrap.bat mingw" (tried versions 1.5.5, 1.5.4 and 1.3.4 same results) 

2. Downloaded lua precompiled binaries (version 4.1.0 since that is supposidly the only one compatible with luabind)

3. Added environment variables BOOST_ROOT and LUA_PATH to their respective value

4. Added bjam path to environment variables

5. Downloaded luabind (tried all versions since 2006) and ran this in cmd "bjam stage toolset=gcc"

 

Output:

"g++"  -ftemplate-depth-128 -O0 -fno-inline -Wall -g  -DLUABIND_BUILDING  -I
"." -I"C:\boost_1_54_0" -I"C:\mingw_dev_lib\lua41\include" -c -o "bin\gcc-mingw-
4.7.1\debug\src\overload_rep.o" "src\overload_rep.cpp"
 
...failed gcc.compile.c++ bin\gcc-mingw-4.7.1\debug\src\overload_rep.o...
gcc.compile.c++ bin\gcc-mingw-4.7.1\debug\src\stack_content_by_name.o
In file included from ./luabind/wrapper_base.hpp:31:0,
                 from ./luabind/back_reference.hpp:27,
                 from ./luabind/class.hpp:97,
                 from ./luabind/luabind.hpp:28,
                 from src\stack_content_by_name.cpp:25:
./luabind/detail/call_member.hpp:318:1: error: missing binary operator before to
ken "("
In file included from ./luabind/back_reference.hpp:27:0,
                 from ./luabind/class.hpp:97,
                 from ./luabind/luabind.hpp:28,
                 from src\stack_content_by_name.cpp:25:
./luabind/wrapper_base.hpp:90:1: error: missing binary operator before token "("
 
In file included from ./luabind/detail/constructor.hpp:39:0,
                 from ./luabind/class.hpp:98,
                 from ./luabind/luabind.hpp:28,
                 from src\stack_content_by_name.cpp:25:
./luabind/wrapper_base.hpp:90:1: error: missing binary operator before token "("
 
In file included from ./luabind/detail/constructor.hpp:41:0,
                 from ./luabind/class.hpp:98,
                 from ./luabind/luabind.hpp:28,
                 from src\stack_content_by_name.cpp:25:
./luabind/detail/signature_match.hpp:151:1: error: missing binary operator befor
e token "("
./luabind/detail/signature_match.hpp:227:1: error: missing binary operator befor
e token "("
In file included from ./luabind/detail/constructor.hpp:42:0,
                 from ./luabind/class.hpp:98,
                 from ./luabind/luabind.hpp:28,
                 from src\stack_content_by_name.cpp:25:
./luabind/detail/call_member.hpp:318:1: error: missing binary operator before to
ken "("
In file included from ./luabind/detail/constructor.hpp:43:0,
                 from ./luabind/class.hpp:98,
                 from ./luabind/luabind.hpp:28,
                 from src\stack_content_by_name.cpp:25:
./luabind/wrapper_base.hpp:90:1: error: missing binary operator before token "("

And it goes on and on with similar messages from different files...

 

If i run without toolset=gcc it compiles just fine with mcvs.. the .lib and .bin files are there, just like they should be..

 

I have looked at :

http://stackoverflow.com/questions/9631762/error-missing-binary-operator-before-token

and

https://svn.boost.org/trac/boost/ticket/6631

which seems to be related, but it doesn't help my situation.

 

Someone please help smile.png

 

Thanks in advance / AS


2d logical to view conversion

13 January 2014 - 09:22 AM

Hi.

 

I wanted to get some input from you guys on what method to use when converting logical positions and dimensions to their view counterparts and vice versa in a 2D game.

 

To give you some context, the game is a simple top down 2d game. The game is competitive and multiplayer. All game related positions and dimensions (x, y, widht, height etc) are in a logical cartesian space and when rendering it is scaled to view space. The other way around is also true, for example, when getting input from the mouse, the click position is converted from view to logical space.

 

Two psuedo code use cases might look like:

render(
  camera.to_view(entity.x),
  camera.to_view(entity.y),
  camera.to_view(entity.w),
  camera.to_view(entity.h)
);
 
get_mouse_pos(
  camera.to_logical(mouse.x),
  camera.to_logical(mouse.y)
);

So, I can think of two ways to do this.

 

 

1. Using a scale that depends on the level and screen width

scale.x = screenWidth / levelWidth;
scale.y = screenHeight / levelHeight;

This will make it so that the same number of logical units will be shown no mather what resolution the screen is. The logical positions and dimensions will simply be scaled to match the screen resolution.

 

pros

  •  
  • gameplay will be the same on all screen resolutions (the same amount of logical units are always shown)
  •  

cons

  •  
  • might look wierd on a lot of resolutions because of scaling
  •  

 

2. Using a fixed scale

scale = 10;

This will make it so that (screenWidth / scale and screenHeight / scale) number of logical units will be shown. The logical positions and dimensions are always scaled the same; by [scale] amount.

 

pros

  • easier to make it look good, since scaling is always the same on all resolutions

cons

  • will affect gameplay, since players using a screen with larger resolutions will see more of the level 

 

PS: I say "screen" but you can think of it as a viewport, in case the game is no in fullscreen, everything still applies.

PSS: "positions and dimensions" are really points and vectors.. lol

 

So, I would love to get some feedback on what you think will work the best, thanks in advance! / AS


Problems with glm::ortho

09 November 2012 - 08:10 AM

Hi!

I am currently trying to learn openGL (and 3d programming in general) using this tutorial:
http://www.opengl-tu...ners-tutorials/

I have finished the first 3 tutorials and I think that I have a basic grasp of what's going on.

Now, my problem is that when I try to follow the authors advice to experiment with glm::ortho as an alternative to glm::perspective I cannot, for the life of me, get anything to show in the cameras "field of view".

The problematic code:
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
	//glm::mat4 Projection = glm::perspective(45.f, 4.0f / 3.0f, 0.1f, 100.0f);
	glm::mat4 Projection = glm::ortho(
		0.0f,
		static_cast<float>(SCREEN_WIDTH),
		static_cast<float>(SCREEN_HEIGHT),
		0.0f,
		0.0f,
		100.0f
	);
	// Camera matrix
	glm::mat4 View = glm::lookAt(
		glm::vec3(0,0,5), // Camera is at (0,0,5), in World Space
		glm::vec3(0,0,0), // and looks at the origin
		glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
	);
	// Model matrix : an identity matrix (model will be at the origin)
	glm::mat4 Model = glm::mat4(1.0f);
	// Our ModelViewProjection : multiplication of our 3 matrices
	glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
	// An array of 3 vectors which represents 3 vertices
	static const GLfloat g_vertex_buffer_data[] =
	{
		1.0f, 1.0f, 0.0f,
		1.0f, 2.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
	};

SCREEN_WIDTH and SCREEN_HEIGHT are int constants 1024, 768 respectively.

If I use a projection matrix generated with glm::perspective (the on commented out at line 2) everything works as it should.
As I move the object around and change the projection matrix parameters, it behaves as you think it should.

But when I use glm::ortho I cannot get the triangle to show, no mather what coordinates I use for the triangles vertices or what values I pass as parameters to glm::ortho. (And I've tried a lot of them Posted Image)

If I understand it correctly you would use ortho when you want to display objects without depth, kind of like how you would in normal 2d space, (which makes me a bit confused about the zNear and zFar parameters, but still, if I'm not misstaken, the triangle should be visible given the projeciton matrix im producing with glm::ortho and the triangles position)

I've searched both gamedev and other resources on the web but I can't find that I'm doing anything wrong or different compared to working examples.

So, the only conclusion I can come to is that I'm missunderstanding the use of glm::ortho or 3d space in general, or that I made some noob mistake, as my grasp of 3d programming is close to nonexisting.

So please, if you have the time, enlighten me as to what I am doing wrong!

Whole main.cpp if needed:
// Include standard headers
#include <iostream>
// Include GLEW
#define GLEW_STATIC
#include "glew.h"
// Include GLFW
#include "glfw.h"
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
// Include Shaders
#include "common/shader.hpp"
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SCREEN_BPP = 32;
int main( void )
{
	// Initialise GLFW
	if (!glfwInit())
	{
		std::cout << stderr << "Failed to initialize GLFW." << std::endl;
		return -1;
	}
	glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	// Open a window and create its OpenGL context
	if (!glfwOpenWindow(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 0, SCREEN_BPP, 0, GLFW_WINDOW))
	{
		std::cout << stderr << "Failed to open GLFW window." << std::endl;
		glfwTerminate();
		return -1;
	}
	// Initialize GLEW
	if (glewInit() != GLEW_OK)
	{
		std::cout << stderr << "Failed to initialize GLEW." << std::endl;
		return -1;
	}
	glfwSetWindowTitle("OpenGL 3 Training");
	// Ensure we can capture the escape key being pressed below
	glfwEnable(GLFW_STICKY_KEYS);
	// Black background
	glClearColor(0.f, 0.f, 1.f, 1.f);
	GLuint VertexArrayID;
	glGenVertexArrays(1, &VertexArrayID);
	glBindVertexArray(VertexArrayID);
	// Create and compile our GLSL program from the shaders
	GLuint programID = LoadShaders("SimpleTransformShader.vertexshader", "SimpleFragmentShader.fragmentshader");
	// Get a handle for our "MVP" uniform
	GLuint MatrixID = glGetUniformLocation(programID, "MVP");
	// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
	//glm::mat4 Projection = glm::perspective(45.f, 4.0f / 3.0f, 0.1f, 100.0f);
	glm::mat4 Projection = glm::ortho(
		0.0f,
		static_cast<float>(SCREEN_WIDTH),
		static_cast<float>(SCREEN_HEIGHT),
		0.0f,
		0.0f,
		100.0f
	);
	// Camera matrix
	glm::mat4 View = glm::lookAt(
		glm::vec3(0,0,5), // Camera is at (0,0,5), in World Space
		glm::vec3(0,0,0), // and looks at the origin
		glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
	);
	// Model matrix : an identity matrix (model will be at the origin)
	glm::mat4 Model = glm::mat4(1.0f);
	// Our ModelViewProjection : multiplication of our 3 matrices
	glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
	// An array of 3 vectors which represents 3 vertices
	static const GLfloat g_vertex_buffer_data[] =
	{
		1.0f, 1.0f, 0.0f,
		1.0f, 2.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
	};
	static const GLushort g_element_buffer_data[] = { 0, 1, 2 };
	// This will identify our vertex buffer
	GLuint vertexbuffer;
	// Generate 1 buffer, put the resulting identifier in vertexbuffer
	glGenBuffers(1, &vertexbuffer);
	// The following commands will talk about our 'vertexbuffer' buffer
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	// Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
	while (true)
	{
		// Check if the ESC key was pressed or the window was closed
		if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS || !glfwGetWindowParam(GLFW_OPENED))
			break;
		// Clear the screen with the clearcolor
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		// User our shader
		glUseProgram(programID);
		// Send our transformation to the currently bound shader, in the "MVP" uniform
		glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
		// First attribute buffer : vertices
		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
		glVertexAttribPointer(
			0,		  // attribute 0. No particular reason for 0, but must match the layout in the shader.
			3,		  // size
			GL_FLOAT,   // type
			GL_FALSE,   // normalized?
			0,		  // stride
			(void*)0	// array buffer offset
		);
		// Draw the triangle !
		glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
		glDisableVertexAttribArray(0);
		// Swap buffers
		glfwSwapBuffers();
	}
	// Cleanup VBO and shader
	glDeleteBuffers(1, &vertexbuffer);
	glDeleteProgram(programID);
	glDeleteVertexArrays(1, &VertexArrayID);
	// Close OpenGL window and terminate GLFW
	glfwTerminate();
	return 0;
}

Thanks in advance / AS.

Affecting the actual player as an alternative to affecting the playable character

26 October 2012 - 07:44 AM

Hi.

Just some quick things that I'm pondering over that I would be happy to get some feedback / thoughts on.

What do you think about having in-game conditions that directly affect the player rather then the players character to increase the skillcap in a PvP or PvE enviroment? (Im thinking from a MMORPG perspective, but that's just my POI)

For example there would be spells that would cause:
Rearrangment of skills on hotbar for a small period of time,
Inversing controls (move left by pressing right)for a small period of time,
Add a small delay to input ations (user hit key, takes 0.2 sec for action to ocour) for a small period of time,

Instead of spells that would cause:
Slow on character for small period of time,
Blindness for small period of time,
DOT..

Ofcourse, messing with controls is not something that you would generally want to do as it makes the player feel less in power and 'cheated on' by the system, but with balance, could this be something of use to increase the skillcap and add actual physical (reaction) and mental (observation, tactic) aspects to a challanging enviroment?

Thanks for your input. / AS

PARTNERS