TheFearlessHobbit

Members
  • Content count

    11
  • Joined

  • Last visited

Community Reputation

140 Neutral

About TheFearlessHobbit

  • Rank
    Member

Personal Information

  • Interests
    |programmer|
  1. What is wrong with my ray-sphere intersection?

    Hello, thank you both for your replies. I think I managed to fix my problem. I changed this: if (b_squared_minus_4ac > 0) { // Potential calculation error here double x1 = (-b - sqrt(b_squared_minus_4ac)) / (2.0 * a); double x2 = (-b + sqrt(b_squared_minus_4ac)) / (2.0 * a); // None of these cases test true from far away if (x1 >= 0.0 && x2 >= 0.0) return true; if (x1 < 0.0 && x2 >= 0.0) return true; } into this: if (b_squared_minus_4ac == 0) { // One real root return true; } else if (b_squared_minus_4ac > 0) { // Two real roots long double x1 = (-b - sqrt(b_squared_minus_4ac)) / (2.0 * a); long double x2 = (-b + sqrt(b_squared_minus_4ac)) / (2.0 * a); if (x1 >= 0.0 || x2 >= 0.0) return true; if (x1 < 0.0 || x2 >= 0.0) return true; } // No real root return false; Not sure how reasonable this solution is but at least it seems to be working now. I am able to pick up my triangle and move it around even from afar. Thank you once again for all your help guys, enjoy your day.
  2. Hello, I was hoping someone here could help me identify the issue with my 3D mouse picking mechanic. I have a basic scene and I am trying to pick a mesh (a triangle in my case) and move it around with my mouse. So far I have been able to implement that correctly but I can't seem to be able to pick the object from far away. I'd have to move the camera closer to the triangle bounded by a sphere in order for it to intersect with my ray. What I am trying to do is simply make it so that you can pick the object up and move it around even from far away. I will include code that is relevant to the mechanic below: Get the ray direction // Function that takes mouse position on screen and return ray in world coords glm::vec3 PhysicsEngine::GetRayFromMouse() { glm::vec2 ray_nds = glm::vec2(mouseX, mouseY); glm::vec4 ray_clip = glm::vec4(ray_nds.x, ray_nds.y, -1.0f, 1.0f); glm::mat4 invProjMat = glm::inverse(m_Camera.GetProjectionMatrix()); glm::vec4 eyeCoords = invProjMat * ray_clip; eyeCoords = glm::vec4(eyeCoords.x, eyeCoords.y, -1.0f, 0.0f); glm::mat4 invViewMat = glm::inverse(m_Camera.ViewMatrix()); glm::vec4 rayWorld = invViewMat * eyeCoords; glm::vec3 rayDirection = glm::normalize(glm::vec3(rayWorld)); return rayDirection; } Check for ray-sphere collision Here is where I believe I am having the problem. After debugging the code, I noticed that it does pick up an intersection between ray and the sphere even from far away because b^2 - 4ac is greater than 0. However, the real solutions (normally when b^2 - 4ac is > 0 this means we have 2 real solutions) are not returning true for some reason. To be more clear, I have commented the parts where I believe the problem is occurring. // Function that checks for ray-sphere intersection and returns true or false bool PhysicsEngine::ray_sphere(vec3 ray_origin_wor, vec3 ray_direction_wor, float sphere_radius) { // work out components of quadratic vec3 v = glm::vec3(m_Transformation.GetPos().x, m_Transformation.GetPos().y, m_Transformation.GetPos().z) - m_Camera.GetCameraPosition(); double a = glm::dot(ray_direction_wor, ray_direction_wor); double b = 2.0 * glm::dot(v, ray_direction_wor); double c = glm::dot(v, v) - sphere_radius * sphere_radius; double b_squared_minus_4ac = b * b + (-4.0) * a * c; // b^2-4ac is greater than 0 even from far away if (b_squared_minus_4ac > 0) { // Potential calculation error here double x1 = (-b - sqrt(b_squared_minus_4ac)) / (2.0 * a); double x2 = (-b + sqrt(b_squared_minus_4ac)) / (2.0 * a); // None of these cases test true from far away if (x1 >= 0.0 && x2 >= 0.0) return true; if (x1 < 0.0 && x2 >= 0.0) return true; } return false; } I am thankful for any guidance you can give me.
  3. Making camera follow object

    Fixed it, thank you all very much for your help. 
  4. Making camera follow object

    I tried setting up an orthographic projection as you suggested like so: m_Projection = glm::ortho(PlayerXCoord - 800.0f / 2.0f, PlayerXCoord + 800.0f / 2.0f, PlayerYCoord - 600.0f / 2.0f, PlayerYCoord + 600.0f / 2.0f, -1.0f, 1.0f); It seems to be working but my scene is so far away from the camera I can hardly see it. It's a mere pixel.  
  5. Making camera follow object

    Why of course I tried that :)   Ultimately, this is what I tried to do: extern float PlayerXCoord; extern float PlayerYCoord; float PlayerZCoord = -1.0f; glm::vec3 PlayerPosition(PlayerXCoord, PlayerYCoord, PlayerZCoord); CCamera::CCamera() : m_ViewDirection(0.0f, 0.0f, -1.0f), m_Position(0.0f, 0.0f, 0.0f) {} CCamera::~CCamera() {} glm::mat4 CCamera::GetViewToWorldMatrix() const { return glm::lookAt(m_Position, m_Position + PlayerPosition, glm::vec3(0.0f, 1.0f, 0.0f)); }
  6. Making camera follow object

    Hey everybody, I recently just setup my camera scene: Camera Class class Camera { public: Camera(); ~Camera(); glm::mat4 GetViewToWorldMatrix() const; private: glm::vec3 m_Position; glm::vec3 m_ViewDirection; }; Definition: Camera::Camera() : m_ViewDirection(0.0f, 0.0f, -1.0f) // Always look in the negative z {} Camera::~Camera() {} glm::mat4 Camera::GetViewToWorldMatrix() const { return glm::lookAt(m_Position, m_Position + m_ViewDirection , glm::vec3(0.0f, 1.0f, 0.0f)); } Obviously by adding the position vector of the camera with the view direction vector, that will return the lookAt. After that I'm simply multiplying the projection matrix with the ViewToWorld matrix and it's working fine, I can see my whole scene. However, I'm trying to make it so that the camera only follows one object in my scene. How can I achieve this?     Thank you for your help.  :)
  7. Any good A* tutorial?

    Hello, why don't you try picking up a book instead  :) Here's a book I found very helpful when I started learning AI: Programming Game AI by Example (Click) It teaches you a lot of things including dijkstra's algorithm with A*. The author provides plenty of examples for you to follow as well.  If you're not looking to spend money on books, then you could try the articles on this site, some of them are really good even for beginners. :)  
  8. OpenGL Trouble displaying texture with SOIL (newbie)

    Oh, what a silly mistake. Thank you for pointing that out I can see my texture now! I'll try to pay more attention to my arguments from this point on to make sure they are all consistent.  Thank you once again for your help. :)  
  9. Hey guys, back with another newbie question.  :(    I'm trying to create a simple 2D platform game in an attempt to learn and discover more things about OpenGL. Basically, I have created a quad that fits the entire window and now I would like to texture it so that it looks like a background. However, I can't seem to be able to get it to work as I keep getting an access violation error.    Here's some relevant parts of my code I hope you find useful:   Global variables GLuint g_texture = 0; int iWidth, iHeight;     Startup function: { // ... glewinit, shader creation, vertices and color creation etc...   GLfloat Texture[] = { 0.0f, 0.0f,  // Top Left 1.0f, 0.0f,  // Top Right 1.0f, 1.0f,  // Bottom Right 0.0f, 1.0f   // Bottom Left };   //... buffer generations etc...   glGenTextures(1, &g_texture); glBindTexture(GL_TEXTURE_2D, g_texture); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);   unsigned char* image = SOIL_load_image("adventure.png", &iWidth, &iHeight, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iWidth, iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   glGenerateMipmap(GL_TEXTURE_2D);   SOIL_free_image_data(image);   glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); }   Render function   void Render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glUseProgram(program); glBindTexture(GL_TEXTURE_2D, g_texture); glBindVertexArray(vao); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);  glBindVertexArray(0);   glutSwapBuffers(); }   And lastly, here are my shaders:   Vertex Shader   #version 430 core   in layout (location = 0) vec3 VertexPos; in layout (location = 1) vec4 VertexColors; in layout (location = 2) vec2 TextureCoord;   out vec4 outputColors; out vec2 TexCoord;   void main() { gl_Position = vec4(VertexPos, 1.0f); outputColors = VertexColors; TexCoord = vec2(TextureCoord.x, TextureCoord.y); }   Fragment Shader   #version 430 core   // Inputs in vec4 outputColors; in vec2 TexCoord;   // Output Color out vec4 VertexColors;   uniform sampler2D Texture;   void main() { VertexColors = texture(Texture, TexCoord) * outputColors; //VertexColors = outputColors; }   I hope someone could point out where I'm going wrong with this.   Thank you very much for reading my thread. All help is greatly appreciated.  :)
  10. OpenGL Trouble coloring my mesh's vertices (Newbie)

    Problem fixed. (Woo!) For whoever's interested, I simply created another VBO which contains the new color data and inside my fragment shader I created a timer to alternate between both outputs using if-else statements every 1 or 2 seconds. However, I sort of lost my glowing effect that way but at least I got my multi-colored vertices that change overtime. I'll work on getting some glowing effects going on.  Thank you to whoever read my post and tried to help me out.  :)
  11. Hello everyone, I am a new registered user.  I have just started learning OpenGL 2 weeks ago and I come from a WIN32 API background. To cut straight to the chase, I have a Hexagon drawn like this:  GLfloat vertices[] = { -0.25f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   // Red                        0.25f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,   // Green                         0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ,   // Blue                         0.25f, 0.5f, 0.0f,  1.0f, 0.0f, 1.0f,    // Purple                         -0.25f, 0.5f, 0.0f,  1.0f, 1.0f, 1.0f,   // White                         -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f     // Yellow }; Here are my shaders:  Vertex Shader #version 430 core layout (location = 0) in vec3 position;  layout (location = 1) in vec3 color; layout (location = 2) in vec3 newColor; //Er... what to do?! uniform mat4 _resultantMatrix; out vec3 outputColor; void main()  {  vec4 currentPos = vec4(position, 1.0f); gl_Position = _resultantMatrix * currentPos; outputColor = color; }; Fragment Shader #version 430 core in vec3 outputColor; out vec4 Color; uniform float Timer; void main() {    vec3 tempColor = outputColor * abs(sin(Timer));    tempColor.x = (sin(Timer) / 2) + 0.5f; //   tempColor.y = (cos(Timer) / 2) + 0.5f; Not doing what I expect it to do.    Color = vec4(tempColor, 1.0f);  }; The result is a multi-colored hexagon. However, I want my hexagon's vertices to glow, that is essentially what I am going for. I have a uniform float Timer that fades in and out quite smoothly. Though, every time I execute my program, it looks like only 2 or 3 vertices change color, while the others stay the same. I think I know what the issue here is, it has something to do with the fact that I am storing all my colors in the same VBO.    glGenBuffers(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));   // glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(21 * sizeof(GLfloat))); Stores the data in layout 2 but don't know what to do after! glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); // glEnableVertexAttribArray(2); So like in my fragment shader, when I go tempColor.x = blahblahblah; it's changing more than one vertex color. If anyone could help me out I'd greatly appreciate it as I have been stuck for 2 hours trying to figure this thing out. I want all my vertices to have different colors while the program is being executed, and so far only 3 vertices change while the other 3 stays the same.    Thank you very much!