I am trying to change a C++ project, which is currently drawing some lines when I click on the view port. This functionality is perfectly fine, but what I am trying to change is when I click on "UP" or "Down" keys the color for next lines to change. Currently if I click on those keys the color changes for all the lines including the old ones (already drawn).
Please give me an idea of what to do. Here is some of the code:
void drawPrimitive() {
Vertex *temp;
// Set the primitive color
glColor3fv(primitiveColor);
// Set the point size in case we are drawing a point
if (type == POINT)
glPointSize(pointSize);
// Display results depending on the mode
glBegin(mode);
for(temp = head; temp != NULL; temp = temp->np)
{
if (smoothShading)
glColor3f(temp->r, temp->g, temp->b);
glVertex2f(temp->x, temp->y);
}
glEnd(); }
void mouse(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
float pointX, pointY;
pointX = (float)x/window_width * world_width;
pointY = (float)(window_height - y)/window_height * world_height;
// Add a vertex to the list of vertices...
addVertex(&head, &tail, pointX, pointY, 0.0f, primitiveColor[0], primitiveColor[1], primitiveColor[2]);
// automatically calls the display function
glutPostRedisplay();
}
else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
{
deleteVertex(&head, &tail);
glutPostRedisplay();
} }
void special(int key, int x, int y) {
switch (key)
{
// change primitive color
case GLUT_KEY_UP :
changePrimitiveColor(1);
break;
case GLUT_KEY_DOWN :
changePrimitiveColor(-1);
break;
}
glutPostRedisplay(); }
void changePrimitiveColor(int step) {
primitiveColorId += step;
if (primitiveColorId < 0)
primitiveColorId = COLOR_COUNT - 1;
if (primitiveColorId >= COLOR_COUNT)
primitiveColorId = 0;
setColor(primitiveColor, primitiveColorId); }
Your code is sort of unclear; is primitiveColor a global variable?
Assuming you're calling drawPrimitive() for all your lines each redraw, the same primitiveColor would be used for all the lines. As soon as you change the color when you press up or down, the redisplay function is called, and all the lines will be redrawn using the same color.
What you might want to do is have a list containing both the primitives and their respective colors. When you iterate through this list, you can set a color for each line.
Keep in mind that OpenGL behaves like a statemachine. If you set a color, everything you draw after it will be drawn with that color. OpenGL will not remember that your other elements had a different color and you want to keep it like that. You have to do the bookkeeping.
So each time your content is drawn, you will have to explicitly state which elements have which color.
Related
I want to draw a triangle like this
I think I have to change these part of my codes
glBegin(GL_LINES);
glVertex2f(point[0][0], point[0][1]);
glVertex2f(point[1][0], point[1][1]);
glEnd();
and my mouse button down codes are like this
if (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT)
{
inputMode = InputMode::DRAGGING; // Dragging starts
point[0][0] = xw; point[0][1] = yw; // Start point
point[1][0] = xw; point[1][1] = yw; // End point
}
How I have to do?
You need some global variables for 3 points and one index that is telling you which point you actually edit ...
float point[3][2];
int ix=0;
the render change to
glBegin(GL_LINE_LOOP); // or GL_TRIANGLE
glVertex2fv(point[0]);
glVertex2fv(point[1]);
glVertex2fv(point[2]);
glEnd();
now I do not code in GLFW but you need to change the onclick events to something like:
static bool q0 = false; // last state of left button
bool q1 = (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT); //actual state of left button
if ((!q0)&&(q1)) // on mouse down
{
if (ix==0) // init all points at first click
{
point[0][0]=xw; point[0][1]=yw;
point[1][0]=xw; point[1][1]=yw;
point[2][0]=xw; point[2][1]=yw;
}
}
if (q1) // mouse drag
{
point[ix][0]=xw; point[ix][1]=yw;
}
if ((q0)&&(!q1)) // mouse up
{
point[ix][0]=xw; point[ix][1]=yw;
ix++;
if (ix==3)
{
ix=0;
// here finalize editation for example
// copy the new triangle to some mesh or whatever...
}
}
q0=q1; // remember state of mouse for next event
This is my standard editation code I use in my editors for more info see:
Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?
I am not sure about the q1 as I do not code in GLFW its possible you could extract the left mouse button state directly with different expression. the q0 does not need to be static but in such case it should be global... Also its possible the GLFW holds such state too in which case you could extract it similarly to q1 and no global or static is needed for it anymore...
Basically what I'm trying to do is rotate a camera around an object in the center when I hold down "c" and use the arrow keys.
My first question doesn't have much to do with the camera, but with having a key callback recognize 2 keys at the same time.
My function DOES work if I have separate if statements for L/R keys and no "c", but I can't get it to work when I only want the camera to rotate when I'm holding down "c". I have tried using switch(key) and if statements within if statements. Both implementations I've tried are in the code below:
float R = 0.0;
float U = 0.0;
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(key == GLFW_KEY_C && action == GLFW_PRESS) {
switch(key) {
case GLFW_KEY_RIGHT:
R+=0.05;
camera(R, U);
break;
case GLFW_KEY_LEFT:
R-=0.05;
camera(R, U);
break;
case GLFW_KEY_UP:
break;
case GLFW_KEY_DOWN:
break;
default:
break;
}
}
//OR --
if(key == GLFW_KEY_C && action == GLFW_PRESS) {
if(key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
R+=0.05;
camera(R, U);
}
}
}
What am I doing wrong? Is there something else I can try?
My second question has more to do with the camera. It rotates fine for a half circle around the object with my current code, but then once it reaches a certain point, the camera just pans farther and farther away from the object, rather than rotating.
This is the code for my camera function:
GLfloat ox = 10.0;
GLfloat oy = 10.0;
static void camera(float RL, float UD) {
ox+=cos(glm::radians(RL));
oy+=sin(glm::radians(RL));
gViewMatrix = glm::lookAt(glm::vec3(ox, oy, 10.0f), // eye
glm::vec3(0.0, 0.0, 0.0), // center
glm::vec3(0.0, 1.0, 0.0));
}
Your 'C' detecting code won't work because you're only matching when key equals C and an arrow. It can't be both. You'll have to keep another global variable, isCPressed. When C is pressed you set it to true, when C is released you set it to false, and then when an arrow is pressed you check if(isCPressed).
as for the camera code, your algorithm orbits the point 0,0,10 while looking at 0,0,0. That doesn't seem to be what you want, you should have something like oxdist, oydist, 0 for the eye position to orbit 0,0,0 at a distance of 'dist'
Alright so I have this tic tac toe game I'm making with SDL and C++. I'm trying to implement AI into the game. I don't have a problem setting up the AI, but I have a problem making it where you can take turns. My problem is that when I make my move, I can just move as many times as I want before the AI moves. I want it so I can make my move, and I can't make my move again until the AI makes a move. No matter what I do it seems the turn taking doesn't work properly.
This is the class header
class Buttons
{
private:
SDL_Rect squares[8];
public:
Buttons();
void handle_input();
void load_Squares(SDL_Rect sqRects, SDL_Texture* squarTexs);
void show_Squares();
void AI_move();
int grid[9];
bool moveMade = true;
};
Here I check for mouse input, and depending on the location during the left button press, it sets the according grid value to equal 1, meaning it becomes displayed as a circle on the screen. I also make sure that the AI has made a move before it allows me to click.
void Buttons::handle_input()
{
double mouseX = 0, mouseY = 0;
if((event.type == SDL_MOUSEBUTTONDOWN))
{
//If left mouse button was clicked and AI has made a move
if(event.button.button == SDL_BUTTON_LEFT && moveMade == true)
{
//Get mouse location
mouseX = event.button.x;
mouseY = event.button.y;
//If mouse location is in particular square, set according grid value to 1
if((mouseX >= 0) && (mouseX < SCREEN_WIDTH / 3) && (mouseY >= 0) && (mouseY < SCREEN_HEIGHT / 3) && (grid[0] == 0))
{
grid[0] = 1;
moveMade = false;
}
//Basically does this for all other 9 grids
Here is my AI function, where I check to make sure the moveMade variable = false. Every time I make a move in the input function above, it sets moveMade to false, which means it should access this function, and only until it finishes this AI_move function should I be able to make a move again, because moveMade is set back equal to true.
void Buttons::AI_move()
{
if(moveMade == false)
{
AI_block(&moveMade);
AI_complete(&moveMade);
AI_rand(&moveMade);
moveMade = true;
}
}
Last is my show function, where I show a Circle(player) if the grid array value = 1, and I show the X(AI) if the grid value = 2.
void Buttons::show_Squares()
{
switch(grid[0])
{
case 1:
load_Squares(squares[0], circleTexture); break;
case 2:
load_Squares(squares[0], xTexture); break;
}
switch(grid[1])
{
//Does this all the way to grid[8]
}
Alright so my problem doesn't have to do with the AI dealing accordingly, as I haven't even set up my defense and offense functions. My problem is that I can make another move before the AI moves. Sorry if this is way too long, but if I could get any feedback on this that would be great.
Have you tried putting breakpoints at various points such as if(event.button.button == SDL_BUTTON_LEFT && moveMade == true) and then following the program through the see if moveMade ever actually gets changed to false?
You should also look at changing show_Squares() into a loop as there is a lot of repeated code using incremented indexes. Something like this:
void Buttons::show_Squares()
{
size_t array_size = sizeof(squares) / sizeof(int); //gets the number of elements in the array
for(size_t i = 0; i < array_size; i++)
{
switch(grid[i])
{
case 1:
load_Squares(squares[i], circleTexture); break;
case 2:
load_Squares(squares[i], xTexture); break;
}
}
}
Hi so I'm trying to make it so a little UFO bitmap (drawing/painting already taken care of) can be dragged around the screen. I can't seem to make the UFO position update and then redraw repeatedly from the MouseButtonDown() function (simplified code for mouse event handler). Any suggestions on detecting the dragging and redrawing accordingly? Code is below for relevant functions:
void MouseButtonDown(int x, int y, BOOL bLeft)
{
if (bLeft)
{
while(_bMouseMoving == true && _bMouseDragRelease == false) {
_iSaucerX = x - (_pSaucer->GetWidth() / 2);
_iSaucerY = y - (_pSaucer->GetHeight() / 2);
InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
}
// Set the saucer position to the mouse position
_iSaucerX = x - (_pSaucer->GetWidth() / 2);
_iSaucerY = y - (_pSaucer->GetHeight() / 2);
}
else
{
// Stop the saucer
_iSpeedX = 0;
_iSpeedY = 0;
}
}
void MouseButtonUp(int x, int y, BOOL bLeft)
{
_bMouseDragRelease = true;
}
void MouseMove(int x, int y)
{
_bMouseMoving = true;
}
To clarify what chris said, you're only going to get the WM_xBUTTONDOWN message once, and you'll need to use that to toggle a dragging state that you can query when you recieve a WM_MOUSEMOVE message.
When you get the mouse move message during a dragging state, you'll want to invalidate the rectangle surrounding where the ufo was, and the rectangle surrounding where it is.
Invalidating a rectangle causes WM_PAINT messages, where you redraw whatever was behind the ufo, and the ufo in it's new place.
Or you could cheat and make the UFO a cursor when you're dragging :)
How can I get the x,y coordinate of a mouse click, to see if it is over my menu button drawn by directx? Currently, my codebase has the following mouse-related class that doesn't seem to be able to give me this..I'm not sure how this might work.
InputMouse::InputMouse() :
m_LastX(-1),
m_LastY(-1)
{
m_MouseActionEvent.clear();
}
InputMouse::~InputMouse()
{
}
void InputMouse::PostUpdate()
{
m_CurrentAction.clear();
}
bool InputMouse::IsEventTriggered(int eventNumber)
{
for (unsigned int i = 0; i < m_CurrentAction.size(); i++)
{
if (m_MouseActionEvent.size() > 0 && m_MouseActionEvent[m_CurrentAction[i]] == eventNumber)
{
return true;
}
}
return false;
}
void InputMouse::AddInputEvent(int action, int eventNumber)
{
m_MouseActionEvent[action] = eventNumber;
}
void InputMouse::SetMouseMouse(int x, int y)
{
if (m_LastX != -1)
{
if (x > m_LastX)
{
m_CurrentAction.push_back(MOUSE_RIGHT);
}
else if (x < m_LastX)
{
m_CurrentAction.push_back(MOUSE_LEFT);
}
if (y > m_LastY)
{
m_CurrentAction.push_back(MOUSE_UP);
}
else if (y < m_LastY)
{
m_CurrentAction.push_back(MOUSE_DOWN);
}
}
m_LastX = x;
m_LastY = y;
}
DirectX or not, GetCursorPos is going to retrieve the position of the mouse in screen co-ordinates. ScreenToClient will map the screen relative point to a point relative to the client area of your window/directX surface.
If your menu buttons are 2D, this should be as simple as remembering the screen co-ordinates used for your buttons.
If you're trying to determine if a click lands on a 3D object that's been rendered, then the technique you are looking for is called Picking.
A simple Google for "directx picking" comes up with some good results:
http://www.mvps.org/directx/articles/rayproj.htm
http://www.gamedev.net/community/forums/topic.asp?topic_id=316274
Basically, the technique involves converting the mouse click into a ray into the scene. For your menu items, a simple bounding box will probably suffice for determining a 'hit'.
Once an object is drawn, the system has no knowledge of which pixels on the screen it changed, nor do those pixels know which object or objects changed it (nor if those objects even still exist). Therefore, if you need to know where something is on-screen, you have to track it yourself. For buttons and other GUI elements this usually means keeping your GUI in memory along with the rectangles that define the boundaries of each element. Then you can compare your mouse position to the boundary of each element to see which one it is pointing at.