• Content count

  • Joined

  • Last visited

Community Reputation

541 Good

About Prot

  • Rank
  1. I am trying to implement Blades of Waving Grass and I am still stuck at the point where I need to achieve texture arrangements like in the figure below (the first one).     So my plan is to draw the first texture, then draw the second and rotate it by 30°, then another and rotate it by 60° etc. Drawing the first texture is not a problem but I have some issues rotating the second one. I would expect it to look something like this (view from above):   Instead the actual result looks something like this:   Vertex and position data: GLfloat vertices[] = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, }; glm::vec3 texturePositions[] = { glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f) }; Camera/View transformations: // Camera/View transformation glm::mat4 view; view = camera.GetViewMatrix(); // Projection glm::mat4 projection; projection = glm::perspective(glm::radians(camera.Zoom), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 1000.0f); // Get the uniform locations GLint modelLoc = glGetUniformLocation(ourShader.Program, "model"); GLint viewLoc = glGetUniformLocation(ourShader.Program, "view"); GLint projectionLoc = glGetUniformLocation(ourShader.Program, "projection"); // Pass the matrices to the shader glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); Drawing the first two textures: //Texture 1 glm::mat4 model; model = glm::translate(model, texturePositions[0]); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glDrawArrays(GL_TRIANGLES, 0, 6); //Texture 2 GLfloat angle = glm::radians(30.0f); model = glm::translate(model, texturePositions[1]); model = glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f)); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glDrawArrays(GL_TRIANGLES, 0, 6); As I am still in the very early learning process so I am kind of clueless here.  1. I think that the order of transformations for the second texture is fine in my code, isn't it?  2. Also how would I determine the right position for the object where the rotation would be applied correctly? (see above point 2)  3. Might it be more efficient to apply the rotation in the objects local space? Is that possible in my case?  4. If I change the rotation axis from y to z the object rotates around its own center just like expected.  5. I also tried to apply the following solutions (taken from here and here) where I moved the object to glm::vec3(-x,-y,-z) rotate it and then moving it back to glm::vec3(x,y,z), unfortunately with no success. Which makes sence as the position   vector is set to (0,0,0) anyway. I also tried to change this position to something else than the origin also with no success. I do not expect anybody to write code for me (though some snippet would be nice) but I would appreciate some hints or somebody pointing me in the right direction. I think that I have a basic fallacy here. Thanks in advance.
  2. Point around point rotation

      Just as an additional information. I am trying to construct a function which draws a Koch Curve according to the number of desired iterations. That would need an angle of 60° or (Pi / 3).   But when I was debugging the rotate_point-function I was using (0,0) as origin and (1,0) as the "rotating" point and an angle of 90° (or Pi / 2), because it's easier to debug.
  3. Point around point rotation

        Thank you guys for all the advice. While it propably is a good idea to know all those values by heart I tried the following: const GLfloat pi = glm::pi<float>(); GLfloat x = point_a.x + (point_b.x - point_a.x) * glm::cos(pi / 2) - (point_b.y - point_a.y) * glm::sin(pi / 2); GLfloat y = point_a.y + (point_b.x - point_a.x) * glm::sin(pi / 2) + (point_b.y - point_a.y) * glm::cos(pi / 2); The results were exactly the same (as expected :D ). An I know I shouldn't recalculate whose values every time, I will not (promise).   So bottom line -4.37113883e-008 is ok I have to live with it :D
  4. Point around point rotation

      Well my problem is the following:   If I try rotating the point (1,0) around the origin (0,0) I get unprecise output values. I call: glm::vec2 point = rotate_point(glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), 90); It returns:   x = -4.37113883e-008 y = 1.00000000   I mean this is ok, but I wish x would be exactly 0 here. I traced back this inaccuracy down to the glm::cos function where I calculate: GLfloat angle_radians = glm::radians(angle); GLfloat costest = glm::cos(angle_radians); Here costest = -4.37113883e-008 but should be exactly 0. I mean if I do this calculation using degree in the windows calculator it returns exactly 0. What is the right way to achieve this accuracy in my code.   Let's be honest this small error doesn't break my code neither does it distort the drawing result, still this can't be the best solution.
  5. Hey guys my goal is it to calculate the new coordinates of a point after rotating it around another point. According to a thread this can be done using the following formula:     I verified that using pen and paper and it turned out well. Now I was going to implement the according function using the GLM library. glm::vec2 rotate_point(glm::vec2 point_a, glm::vec2 point_b, GLfloat angle) { GLfloat angle_radians = glm::radians(angle); GLfloat x = point_a.x + (point_b.x - point_a.x) * glm::cos(angle_radians) - (point_b.y - point_a.y) * glm::sin(angle_radians); GLfloat y = point_a.y + (point_b.x - point_a.x) * glm::sin(angle_radians) + (point_b.y - point_a.y) * glm::cos(angle_radians); return glm::vec2(x, y); } According to the documentation both glm::sin and glm::cos need the angle parameter to be in radians. For that reason I am using glm::radians to convert the angle to radians. So here is where the problem occurs. The values which are returned by my function are not correct and I guess, it is because the cos and sin functions return wrong values (at least in my case). How could I solve this issue? Are there any other functions in the glm-library which I didn't find or is there another, maybe mathematical approach to solve this problem?
  6. Hi folks here is my vertex array I am using: vertices = [ -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, -0.5, 1.0, 0.0, 0.5, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, -1.0, 0.5, 0.0, -1.0, 0.0, 0.0, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, -0.5, 0.0, 0.0, -0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 1.0, -0.5, 0.0, 1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.5, -1.0, 0.0, -0.5, -1.0, 0.0, -1.0, -1.0, 0.0, ]; In a html-select I want to select a rendering mode and use it inmy drawScene-method: var renderModeSelector = document.getElementById("renderModeDropDown"); var selectedRenderModeValue = renderModeSelector.options[renderModeSelector.selectedIndex].value; switch (selectedRenderModeValue) { case "0": gl.drawArrays(gl.POINTS, 0, squareVertexPositionBuffer.numItems); break; case "1": gl.drawArrays(gl.TRIANGLE_FAN, 0, squareVertexPositionBuffer.numItems); break; case "2": gl.drawArrays(gl.LINE_LOOP, 0, squareVertexPositionBuffer.numItems); break; default: console.log("No rendering mode selected."); } Case '0' is supposed to draw the points which works fine   Case '1' should draw a filled rectangle, with the shader below. Unfortunately it is messed up, see also in the picture below. Fragment shader: precision mediump float; varying vec4 vColor; void main(void) { gl_FragColor = vColor; } Vertex-shader: attribute vec3 aVertexPosition; attribute vec4 aVertexColor; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; varying vec4 vColor; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); vColor = aVertexColor; gl_PointSize = 5.0; }   3. And finally in case '2' it is supposed to draw only the outlines, which also does not work as expected, see below.     So I wonder if it is possible to meet my requirements while using one vertexPositionBuffer. Also if it is not what would be another appropriate solution? Should I create another vertexPositionBuffer or can I just change mine "on the fly", before rendering?
  7.   Actually it does not change anything.   I have to take the time to try all the other suggestions.
  8. I do get "GL_INVALID_ENUM" right after glGenVertexArrays(1,&vao);
  9. Nopes, unfortunately this didn't change the behaviour.    I don't even know how to approach debugging shader output, any hints? Also what should be the Item Type of a glsl-shader under Visual Studio 2013? Currently my shader files are set to Custom Build Tool.
  10. But it does seem to compile the Shaders, without printing any faling information to the console, fyi here the Shader_Manager-class: #include "Shader_Manager.h" #include <iostream> #include <fstream> #include <vector> std::map<std::string, GLuint> Shader_Manager::programs; Shader_Manager::Shader_Manager(void) {} Shader_Manager::~Shader_Manager(void) { std::map<std::string, GLuint>::iterator i; for (i = programs.begin(); i != programs.end(); ++i) { GLuint pr = i->second; glDeleteProgram(pr); } programs.clear(); } const GLuint Shader_Manager::GetShader(const std::string & shaderName) { return; } //reads and returns the contents of a file std::string Shader_Manager::ReadShader(const std::string& filename) { std::string shaderCode; std::ifstream file(filename,std::ios::in); if (!file.good()) { std::cout << "Can't read file" << filename.c_str() << std::endl; std::terminate(); } file.seekg(0,std::ios::end); shaderCode.resize((unsigned int)file.tellg()); file.seekg(0,std::ios::beg);[0],shaderCode.size()); file.close(); return shaderCode; } //creates and compiles a shader (vertex or fragment) GLuint Shader_Manager::CreateShader(GLenum shaderType,const std::string& source,const std::string& shaderName) { int compile_result = 0; GLuint shader = glCreateShader(shaderType); const char *shader_code_ptr = source.c_str(); const int shader_code_size = source.size(); glShaderSource(shader,1,&shader_code_ptr,&shader_code_size); glCompileShader(shader); glGetShaderiv(shader,GL_COMPILE_STATUS,&compile_result); //check for errors if (compile_result == GL_FALSE) { int info_log_length = 0; glGetShaderiv(shader,GL_INFO_LOG_LENGTH,&info_log_length); std::vector<char> shader_log(info_log_length); glGetShaderInfoLog(shader,info_log_length,NULL,&shader_log[0]); std::cout << "ERROR compiling shader: " << shaderName.c_str() << std::endl << &shader_log[0] << std::endl; return 0; } return shader; } //uses ReadShader to extract the shader contents and to create both shaders and load them into the program which is returned to be used in rendering loop void Shader_Manager::CreateProgramm(const std::string& shaderName, const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename) { //read the shader files and save the code std::string vertex_shader_code = ReadShader(vertexShaderFilename); std::string fragment_shader_code = ReadShader(fragmentShaderFilename); GLuint vertex_shader = CreateShader(GL_VERTEX_SHADER,vertex_shader_code,"vertex shader"); GLuint fragment_shader = CreateShader(GL_FRAGMENT_SHADER,fragment_shader_code,"fragment shader"); int link_result = 0; //create the program handle, attach the shaders and link it GLuint program = glCreateProgram(); glAttachShader(program,vertex_shader); glAttachShader(program,fragment_shader); glLinkProgram(program); glGetProgramiv(program,GL_LINK_STATUS,&link_result); //check for link errors if (link_result == GL_FALSE) { int info_log_length = 0; glGetProgramiv(program,GL_INFO_LOG_LENGTH,&info_log_length); std::vector<char> program_log(info_log_length); glGetProgramInfoLog(program,info_log_length,NULL,&program_log[0]); std::cout << "Shader Loader: LINK ERROR" << std::endl << &program_log[0] << std::endl; return; } programs[shaderName] = program; } I was assuming that if the shaders are failing to compile I would have seen it in the console.
  11. Hi folks, I am following in2gpu's tutorials on modern OpenGL. The goal is to render a triangle. Everything worked fine so far, except for the color. The authors triangle has three color defined which makes the fragment shader render the triangle in different colors.  Although I strictly followed the tutorial my triangle is colored in a flat white.   The project now contains several classes which are responsible for freeglut and glew initialization. I also have a Vertex- and a Fragmentshader which look like this:   Vertex_Shader.glsl: #version 330 core layout(location = 0) in vec3 in_position; layout(location = 1) in vec4 in_color; out vec4 color; void main(){ color = in_color; gl_Position = vec4(in_position, 1); } Fragment_Shader.glsl: #version 330 core layout(location = 0) out vec4 out_color; in vec4 color; void main(){ out_color = color; } So the first thing to mention here is I am using version 330 while the author uses version 450 and I am not sure whether this is crucial here. Also there might be another source for the problem. I am using Visual Studio 2015 which does not seem to know .glsl-files. I created the shaders by adding a new Item. Here I chose a Pixel Shader File (.hlsl) and renamed it to .glsl. This did raise the following Error:     I am able to build and run the project though, without errors. Also here is the Triangle class itself:   Triangle.cpp: #include "Triangle.h" Triangle::Triangle() { } Triangle::~Triangle() { //is going to be deleted in Models.cpp (inheritance) } void Triangle::Create() { GLuint vao; GLuint vbo; glGenVertexArrays(1,&vao); glBindVertexArray(vao); std::vector<VertexFormat> vertices; vertices.push_back(VertexFormat(glm::vec3(0.25,-0.25,0.0), glm::vec4(1,0,0,1))); vertices.push_back(VertexFormat(glm::vec3(-0.25,-0.25,0.0), glm::vec4(0,1,0,1))); vertices.push_back(VertexFormat(glm::vec3(0.25,0.25,0.0), glm::vec4(0,0,1,1))); glGenBuffers(1,&vbo); glBindBuffer(GL_ARRAY_BUFFER,vbo); glBufferData(GL_ARRAY_BUFFER,sizeof(VertexFormat) * 3, &vertices[0],GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VertexFormat),(void*)0); glEnableVertexAttribArray(1); //you can use offsetof to get the offset of an attribute glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,sizeof(VertexFormat),(void*)(offsetof(VertexFormat,VertexFormat::color))); glBindVertexArray(0); //here we assign the values this->vao = vao; this->vbos.push_back(vbo); } void Triangle::Update() { //Triangle does not have to be updated } void Triangle::Draw() { glUseProgram(program); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES,0,3); } Although it seems that I followed the tutorial all the way, my triangle is still rendered white only. The are of course a lot more classes but I guess I should not post the entire project here. I can always post some additional information if it is needed. At the end it seems to me that something is wrong with the fragment shader. I also described my problem to the author. he could not have a look into my code/project but he suspected that there is something wrong with my attributes.   I am very new to both C++ and OpenGL therefore it is very difficult for me to debug (if it is even possible for shaders).   Glad for any help and thanks in advance!    
  12. I was wondering,   when you play browser games you never seem to witness a page refresh, even if there is data being stored on the server. So how are databases handled by browser games.   In my opinion there are several possible scenarios:   1. Data is being modified during your absence. Let's say in FarmVille your plants will grow while you are not using the application. So is this data being constantly updated on the server or does the application calculate everything that might have happened in a fast forward when you launch it?   2. Let's say you are currently playing in the browser and you completed a mission and get a coin reward or something. Is this data-update instantly written to a database or is it somehow stored in some kind of session tracker (cookies?) and stored on the server once you leave the application?                   2.1 If it is written to the server instantly how is this done without a page refresh? I mean if there is some kind of client/server communication there must be a page refresh right?                 2.2 If the data is send to the server once you leave the application, how to handle a case where the user simply kills the browser process by simply closing it? Can this scenario be catched by code so the data can still be written?
  13. Ok I read it and also did understand it. One thing I have to ask, though. It is pretty clear that in the example posted by phil_t Box contains all variables a Rectangle (XNA) also contains. But what is the Box's velocity supposed to be? Is it the offset between the last and the new position? This I do not fully understand.
  14. Hi,   I would like to ask you for a little help or advise with my class. In short, what is it supposed to do is to draw a texture which works well. Further it is supposed to follow the mouse position.   Also (here is the problem) it is supposed to check for collision with Rectangle objects (argument in Update() -> List<Obstacle> _obstacleList). Once it does collide with a rectangle it is supposed to 'bounce' in the other direction. Right now when bouncing back the mouse position is also changed via Mouse.SetPosition().   My problem is sometimes the collision checks simply do not work. It feels like collision checks fail if I move the mouse to fast. I am also not sure whether my CheckMinkowskiSum method does the calculations right. It would be great if somebody more experienced could have a look at my class. I know it is a mess right now but I guess it is still readable: using System.Collections.Generic; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System; using System.Windows.Forms; using MonoTest.Environment; using ButtonState = Microsoft.Xna.Framework.Input.ButtonState; namespace MonoTest { public class Companion { private Texture2D texture; private Vector2 position; private MouseState oldMouseState; private GraphicsDevice graphics; private List<ITriggerObject> triggerObjList; private int xBounceVelocity = 0; private int yBounceVelocity = 0; private int maxBounceVelocity = 5; private bool isBouncing = false; private int maxBounceFactor = 15; private int currenBounceFactor = 0; public Companion(Texture2D _texture, Vector2 _position, GraphicsDevice _graphics, List<ITriggerObject> _triggerObjList) { this.texture = _texture; this.position = _position; = _graphics; this.triggerObjList = _triggerObjList; Mouse.SetPosition(graphics.Viewport.Width / 2, graphics.Viewport.Height / 2); //Set the mouse to a position where no collision appears! } public void Update(List<Obstacle> _obstacleList) { MouseState mState = Mouse.GetState(); if (!isBouncing) { CheckCollision(_obstacleList, mState); } else { Bounce(); } oldMouseState = mState; } //Called while still bouncing private void Bounce() { if (currenBounceFactor == maxBounceFactor) { isBouncing = false; currenBounceFactor = 0; xBounceVelocity = 0; yBounceVelocity = 0; } else { currenBounceFactor++; position = new Vector2(position.X+xBounceVelocity,position.Y+yBounceVelocity); Mouse.SetPosition((int)position.X + xBounceVelocity, (int)position.Y + yBounceVelocity); } } //Checks for collision with all Rectangles in the level private void CheckCollision(List<Obstacle> _obstacleList, MouseState mState) { position = new Vector2(mState.X,mState.Y); bool collides = false; string collisionFrom = string.Empty; Rectangle collisionRectangle = new Rectangle(); foreach (Obstacle obstacle in _obstacleList) { if (obstacle.BoundRectangle.Intersects(this.BoundingRect)) { collides = true; collisionFrom = CheckMinkowskiSum(this.BoundingRect, obstacle.BoundRectangle); collisionRectangle = obstacle.BoundRectangle; break; } } //If a collision occured mark bouncing = true if (collides) { switch (collisionFrom) { case "bottom": yBounceVelocity = -maxBounceVelocity; isBouncing = true; break; case "top": yBounceVelocity = maxBounceVelocity; isBouncing = true; break; case "left": case "right": if (collisionRectangle.Center.X < this.BoundingRect.Center.X) //Left { xBounceVelocity = maxBounceVelocity; isBouncing = true; } else //right { xBounceVelocity = -maxBounceVelocity; isBouncing = true; } break; } } } //Returns the direction from which the collision took place public string CheckMinkowskiSum(Rectangle pRectangle, Rectangle oRectangle) { // float wy = ((pRectangle.Width + oRectangle.Width) * (pRectangle.Center.Y - oRectangle.Center.Y)); float hx = ((pRectangle.Height + oRectangle.Height) * (pRectangle.Center.X - oRectangle.Center.X)); if (wy > hx) { if (wy > (hx * -1)) { return "top"; } else { return "left"; } } else { if (wy > (-1 * hx)) { return "right"; } else { return "bottom"; } } } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture,position,Color.White); } public Rectangle BoundingRect { get { return new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); } } } }