Jump to content

  • Log In with Google      Sign In   
  • Create Account

D.V.D

Member Since 20 Aug 2012
Offline Last Active Jun 18 2013 02:13 PM
-----

Topics I've Started

Mesh Class

11 March 2013 - 05:34 PM

Im trying to make my own mesh class without copying the code from a tutorial and I ran into some trouble. As of now, Im not actually importing any info, I have all the vertices stored in a c++ vector class and I have a render function to render the model. The problem is that once I call the render function, my GLuint VBO gets a value of 0 and apperantly the size of my vertices drops from 5 or whatever value it was before to 0. Im not sure why, I figured it might be something to do with how OpenGL handles buffers since I can't modify these values outside of my class (theyre private). Does anyone have an idea as to why these values get modified and become 0? I call CreateMesh just before GlutMainLoop is called which is were my tutorial creates theyre model. The // after cout functions simply state the values the cout functions display.

Mesh.h

 

#include <vector>
#include <assert.h>
#include "Vector.h"
#include "glew.h"
#include <iostream>
 
using namespace std;
 
struct Vertex {
    Vector3f pos;
 
    Vertex () {};
 
    Vertex (const Vector3f &_pos) {
        pos = _pos;
    }
};
 
struct Mesh {
public:
 
    Mesh ();
    Mesh (vector<Vertex>);
 
    void Render ();
 
private:
 
    GLuint VBO; // Vertex Buffer Object
    vector<Vertex> vertices;
 
};

 

Mesh.cpp

 

#include "Mesh.h"
 
Mesh::Mesh () {}
 
Mesh::Mesh (vector<Vertex> _vertices) {
    vertices.resize(_vertices.size());
    vertices = _vertices;
 
    //cout << vertices.size() << endl; // 5
    //cout << VBO << endl; // 3435973836
 
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
 
    //cout << vertices.size() << endl; // 5
    //cout << VBO << endl; // 1
}
 
void Mesh::Render () {
    //cout << vertices.size() << endl; // 0
    //cout << VBO << endl; // 0
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_POINTS, 0, vertices.size());
    glDisableVertexAttribArray(0);
}

 

Render Function:

 

Mesh CreateMesh () {
    vector<Vertex> Vertices (5, Vector3f());
    Vertices[0].pos = Vector3f(0.0f, 0.0f, 0.0f);
    Vertices[1].pos = Vector3f(0.0f, 0.0f, 0.0f);
 
    return Mesh(Vertices);
}
 
static void RenderScene () {
    // VBO = 0 here too
    glClear(GL_COLOR_BUFFER_BIT);
    Model.Render();
    glutSwapBuffers();
}

Geomtery based lighting

26 February 2013 - 03:54 PM

I had this idea that Im sure has been implemented before, I wanted to make a lighting technique where you have a set of vertices and use those vertices as your light volume. For example, if you had a area light kind of like in the crysis 3 tech demo, you would have a box where one face of the box projects a light texture that won't go past the other side of the mesh. Im pretty sure one of the crysis 3 developer video's had this, but I was then wondering how you would go about defining which faces generate the light and I couldn't think of a way that would be easy to program without a predefined set of shapes. Does anyone know how I would go about doing something like this in OpenGL, C++?


Really weird problem with rigid body simulator

15 January 2013 - 06:34 PM

EDIT 2: Hey guys I made a rigid body dynamics system but it suffers from major issues when theres more than 2 bodies colliding at once. The way it works is that it finds which objects are colliding with eachother and puts them into pairs. Each pair being 2 objects colliding with eachother but this doesn't seem to always work. When the three objects collide, they pass through eachother and the program starts to lag intensively because of calculations it does when objects are moving inside of eachother. I was wondering, since all my objects are boxes with a fixed radius, do I need to push the objects back a bit so that they aren't overlapping, and then do the momentum calculations? Whats the way of solving such a problem?

 

TestStack is a list of all the objects in my engine. TEST_COUNT is the number of objects in my engine. Ispointinbox is the function used to calculate if a point is in a box, I tested it around and it works since no rotation transformations are put on the box. If source code for this function is needed, Ill provide it.

struct colstack {

	int current;
	
	object pair1 [MAX_OBJ_COUNT*MAX_OBJ_COUNT];
	object pair2 [MAX_OBJ_COUNT*MAX_OBJ_COUNT];

	colstack () {
		current = 0;
	}

	void add (object _o1, object _o2) {
		pair1[current] = _o1;
		pair2[current] = _o2;
		current++;
	}

	void clear () {
		current = 0;
	}

};

colstack CollisionStack;

void PhysicsPipeline::update () {
	for ( int i=0; i < TEST_COUNT; i++ ) {
		// Update position of boxes
		//cout << "UPDATE" << endl;
		TestStack[i].updatepos();
	}
	// For every object object in test count
	for ( int i=0; i < TEST_COUNT; i++ ) {
		// Check collision with every other object in collision
		for ( int j=0; j < TEST_COUNT; j++ ) {
			// If objects being checked aren't the same object
			if ( j != i ) {
				// Add to pair of collisions
				CollisionStack.add(TestStack[i], TestStack[j]);
			}
		}
	}
	for ( int i=0; i < CollisionStack.current-1; i++ ) {
		// Check for Collision of every vertice of the coliding boxe
		bool hascollided = false;
		if ( CollisionStack.pair1[i].BoundingBox.ispointinbox(CollisionStack.pair2[i].BoundingBox.NW) == true ) {
			collide(0,1);
			hascollided = true;
		}
		if ( CollisionStack.pair1[i].BoundingBox.ispointinbox(CollisionStack.pair2[i].BoundingBox.NE) == true && hascollided == false ) {
			collide(0,1);
			hascollided = true;
		}
		if ( CollisionStack.pair1[i].BoundingBox.ispointinbox(CollisionStack.pair2[i].BoundingBox.SW) == true && hascollided == false ) {
			collide(0,1);
			hascollided = true;
		}
		if ( CollisionStack.pair1[i].BoundingBox.ispointinbox(CollisionStack.pair2[i].BoundingBox.SE) == true && hascollided == false ) {
			collide(0,1);
			hascollided = true;
		}
	}
	CollisionStack.clear();
}

 

This is the collision function. It works by saying that the object with the most momentum in a collision will lose momentum while the object with the least momentum gains momentum. This is described with the proportion variables. The debug function simply prints a variable.

 

inline void collide (int i, int j) {
	//FIX LATER!!
	//if ( deb == true ) {
	Vector2f m1 = Vector2f(TestStack[i].mass * TestStack[i].vel.x, TestStack[i].mass * TestStack[i].vel.y);
	Vector2f m2 = Vector2f(TestStack[j].mass * TestStack[j].vel.x, TestStack[j].mass * TestStack[j].vel.y);
	
	cout << "m1: " << m1.x << ", " << m1.y << endl;
	cout << "m2: " << m2.x << ", " << m2.y << endl;

	double mag1 = sqrt( (m1.x*m1.x) + (m1.y*m1.y) );
	double mag2 = sqrt( (m2.x*m2.x) + (m2.y*m2.y) );

	debug("mag1",mag1);
	debug("mag2",mag2);
	
	double angle1 = atan(degreetorad(m1.y/m1.x));
	double angle2 = atan(degreetorad(m2.y/m2.x));
	if ( m1.x < 0 ) {
		angle1 -= 180.0f;
	}
	if ( m2.x < 0 ) {
		angle2 -= 180.0f;
	}

	debug("angle1",angle1);
	debug("angle2",angle2);
	
	double proportion1, proportion2;
	
	if ( mag1 > mag2 ) {
		proportion1 = mag2/mag1;
		proportion2 = 1.0f - proportion2;
	}
	if ( mag1 < mag2 ) {
		proportion2 = mag1/mag2;
		proportion1 = 1.0f - proportion2;
	}
	if ( mag1 == mag2 ) {
		proportion1 = 0.50f;
		proportion2 = 0.50f;
	}

	debug("proportion1",proportion1);
	debug("proportion2",proportion2);
	
	double mag1t = mag1*proportion1;
	double mag2t = mag2*proportion2;

	debug("mag1t",mag1t);
	debug("mag2t",mag2t);
	
	mag1 -= mag1t;
	mag2 -= mag2t;
	mag1 += mag2t;
	mag2 += mag1t;
	
	debug("mag1",mag1);
	debug("mag2",mag2);

	debug("a1",degreetorad(angle1));
	debug("a2",degreetorad(angle2));

	debug("Angle1C",cos(degreetorad(angle1)));
	debug("Angle1S",sin(degreetorad(angle1)));
	debug("Angle2C",cos(degreetorad(angle2)));
	debug("Angle2S",sin(degreetorad(angle2)));

	TestStack[i].vel.x = mag1 * cos(degreetorad(angle1)) * -1.0f / TestStack[i].mass;
	TestStack[i].vel.y = mag1 * sin(degreetorad(angle1)) * -1.0f / TestStack[i].mass;
	TestStack[j].vel.x = mag2 * cos(degreetorad(angle2)) * -1.0f / TestStack[j].mass;
	TestStack[j].vel.y = mag2 * sin(degreetorad(angle2)) * -1.0f / TestStack[j].mass;

	cout << "Ivel: " << TestStack[i].vel.x << ", " << TestStack[i].vel.y << endl;
	cout << "Jvel: " << TestStack[j].vel.x << ", " << TestStack[j].vel.y << endl;
	deb = false;
	//}
}

Loading Shader Code from Files

12 November 2012 - 05:33 PM

I was following some online tutorials about opengl and shader code and I encoutered a problem. They compiled their shader code from const char* variables but I wanted to find out how to do the same thing but reading the shaders from files. The problem I am having is this, when I run my program, I get no error messages from visual studio 2010 but in the console, I get this error message:

Error compiling shader type 35633: '0<1> : error c0000: syntax error, unexpected, $undefined at token "<undefined>"

I ran the same shader code before when it was in a string and it worked just fine. Im reading the file with this function:

[source lang="cpp"]const char* readShaderFile(const std::string fileName){ std::ifstream shaderFile( fileName.c_str() ); // find the file size shaderFile.seekg(0,std::ios::end); std::streampos length = shaderFile.tellg(); shaderFile.seekg(0,std::ios::beg); // read whole file into a vector: std::vector<char> buffer(length); shaderFile.read(&buffer[0],length); // convert to GLchar const char* Shader = (std::string( buffer.begin(), buffer.end() )).c_str(); // return the shader string return Shader;}[/source]

and my shader code is here:

[source lang="cpp"]#version 330 in vec2 TexCoord0; in vec3 Normal0; out vec4 FragColor; struct DirectionalLight { vec3 Color; float AmbientIntensity; float DiffuseIntensity; vec3 Direction; }; uniform DirectionalLight gDirectionalLight; //uniform sampler2D gSampler; void main() { vec4 AmbientColor = vec4(gDirectionalLight.Color, 1.0f) * gDirectionalLight.AmbientIntensity; float DiffuseFactor = dot(normalize(Normal0), -gDirectionalLight.Direction); vec4 DiffuseColor; if (DiffuseFactor > 0) { DiffuseColor = vec4(gDirectionalLight.Color, 1.0f) * gDirectionalLight.DiffuseIntensity * DiffuseFactor; } else { DiffuseColor = vec4(0, 0, 0, 0); } //FragColor = texture2D(gSampler, TexCoord0.xy) * (AmbientColor + DiffuseColor); FragColor = vec4(1.0,0.0,0.0,1.0)*(AmbientColor + DiffuseColor); }[/source]

Freeglut won't install on VC 2010 win 7 64bit

06 October 2012 - 06:02 PM

I found this link for installing freeglut already compiled on windows 7 64 bit but when I run through the installation I get a whole bunch of link errors for every glut/gl function called. The error is Link2019: unresolved external symbol (function name...etc). I tried looking everywhere throughout the internet and I just can't find a answer to this problem. I downloaded the first link MSVC freeglut, I created a folder in Common Files called MSVC and put the libs and includes in that folder. I linked them like said in the readme for the downloaded freeglut to the project and VC recognizes the header but none of the functions that come with freeglut work at all. They all give the link error posted above. The reason Im using freeglut and not glut is because im following a tutorial online and they happen to use functions from freeglut that don't seem to be in glut.h. Can anyone help me? I also added a bin folder with the DLL in the common files/MSVC/freeglut folder I created and added that path to the enviornment path. I still get the same errors though. Can anyone help?

EDIT: I ran normal Glut with no problem but it seems that Im having trouble running GLEW as well. I put all the files into their locations with the same Link error displayed for glew too.

PARTNERS