Advertisement Jump to content
  • Advertisement

thyrgle

Member
  • Content Count

    68
  • Joined

  • Last visited

Community Reputation

116 Neutral

About thyrgle

  • Rank
    Member
  1. Thanks for the response, I tried changing my -(void)draw method to this: glTranslatef(horiVec, 0, vertVec); [newTerrain updateTerrain]; gluLookAt(0, 0, 0, rotX, rotY, 0, 0, 1, 0); I disregarded the eyex and eyez since nothing is moving, and I placed the gluLookAt after I had translated the stuff, but it still isn't working, I just see a blank screen. If I do this: glTranslatef(horiVec, 0, vertVec); [newTerrain updateTerrain]; glLoadIdentity(); gluLookAt(0, 0, 0, rotX, rotY, 0, 0, 1, 0); The background shows, but continually flashes around and the thing doesn't rotate right. Any ideas?
  2. So I was using the two following methods to draw: - (void)drawRect:(NSRect)rect { //In combination draw this is where the drawing happens. [super drawRect:rect]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport( 0,0,[self frame].size.width,[self frame].size.height ); glMatrixMode(GL_MODELVIEW); [self draw]; glFlush(); } - (void)draw { glTranslatef(horiVec, 0, vertVec); [newTerrain updateTerrain]; } It worked fine I could move my character around with the arrow keys (vertVec is kind of a misnomer, it is a 3D game and the person is moving forward not actually up, also quick side note: the game is being updated with a NSTimer every 0.017 secs). Ok, but now I need to add mouse rotation so I changed the code to this: - (void)drawRect:(NSRect)rect { //In combination draw this is where the drawing happens. [super drawRect:rect]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport( 0,0,[self frame].size.width,[self frame].size.height ); glMatrixMode(GL_MODELVIEW); [self draw]; glFlush(); } - (void)draw { glTranslatef(horiVec, 0, vertVec); eyex += horiVec; eyez += vertVec; gluLookAt(eyex, 0, eyez, rotX, rotY, 0, 1, 1, 0); [newTerrain updateTerrain]; } To make some notes: horiVec and vertVec alternate between 0.1, 0.0, and -0.1, for movement. To get rotX and rotY I use the following method which is invoked through an NSTimer every 0.017 sec: -(void)getMouse:(id)sender { NSPoint tempMouseLoc; tempMouseLoc = [NSEvent mouseLocation]; //get current mouse position rotX = mouseLoc.y - tempMouseLoc.y; rotY = mouseLoc.x - tempMouseLoc.x; mouseLoc = tempMouseLoc; } Basically, rotX and rotY are the vector components of the delta of the mouse. I am having a little problem understanding gluLookAt (and that might be part of the problem) but basically what I am aiming for is a way to handle the camera for an FPS style game. But nothing shows up on the screen with the updated draw method. Any ideas why? If you need any more code just ask. Thanks
  3. Ok, thank you! I put: gluPerspective(45.0, //The camera angle [self frame].size.width/[self frame].size.height, //The width-to-height ratio 1.0, //The near z clipping coordinate 200.0); In my prepareOpenGL method and now I have depth. Thanks!
  4. Thank you for the response but, I only see a white screen (for some reason I have trouble flushing the screen unless it is with glFlush()). Also, I have the vertVec in the z part of glTranlatef because I want the triangle to move forward and backward, not up and down. Sorry, maybe I should change vertVec because it is a little bit of a misnomer.
  5. So I have the following class: @implementation GameView -(void)mainTimerFired:(id) sender { //Move the square based off arrow key states. if (upPressed == YES) { vertVec = 0.01; } if (downPressed == YES) { vertVec = -0.01; } if (rightPressed == YES) { horiVec = 0.01; } if (leftPressed == YES) { horiVec = -0.01; } [ self setNeedsDisplay:YES]; } //Needed, if you don't include it the keys wont work. - (BOOL)acceptsFirstResponder { return YES; } - (void) prepareOpenGL { upPressed = NO; downPressed = NO; leftPressed = NO; rightPressed = NO; vertVec = 0; horiVec = 0; rotY = 0; rotX = 0; glEnable(GL_DEPTH_TEST); glClearDepth(1.0f); mainTimer=[ [ NSTimer scheduledTimerWithTimeInterval:0.017 target:self selector:@selector(mainTimerFired:) userInfo:nil repeats:YES ] retain ] ; return; } -(void) keyUp:(NSEvent *)theEvent { //Stop movement when the key is released. switch( [theEvent keyCode] ) { case 126: //Up arrow upPressed = NO; vertVec = 0.0; break; case 125: //Down arrow downPressed = NO; vertVec = 0.0; break; } switch( [theEvent keyCode] ) { case 124: //Right arrow rightPressed = NO; horiVec = 0.0; break; case 123: //Left arrow leftPressed = NO; horiVec = 0.0; break; } } //Key events. -(void) keyDown:(NSEvent *)theEvent { //Tell the game loop that I want to move the square. switch( [theEvent keyCode] ) { case 126: //Up arrow upPressed = YES; break; case 125: //Down arrow downPressed = YES; break; } switch( [theEvent keyCode] ) { case 124: //Right arrow rightPressed = YES; break; case 123: //Left arrow leftPressed = YES; break; } } - (void)drawRect:(NSRect)rect { //In combination draw this is where the drawing happens. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport( 0,0,[self frame].size.width,[self frame].size.height ); glMatrixMode(GL_MODELVIEW); [self draw]; glFlush(); } - (void)draw { glViewport( 0, 0, [self frame].size.width, [self frame].size.height ); glTranslatef(horiVec, 0, vertVec); glBegin(GL_TRIANGLES); { glVertex3f( 0.0, 1.0, -1.0); glVertex3f( -1.0, -1.0, -1.0); glVertex3f( 1.0, -1.0 ,-1.0); } glEnd(); } @end A brief explanation of my code: The keyUp:(NSEvent *) and keyDown:(NSEvent *) tell my game loop mainTimerFired:(id) to change horiVec or vertVec. Those are used in the 2 drawing methods to with glTranslatef to move a triangle. So I am using an NSOpenGLView, a triangle does display, I can move it left and right, but when I try moving it forward and backward nothing happens until it suddenly disappears after holding either the up or down key for a while. I can then bring it back by holding the opposite arrow key. Also, I tried getting info about my GL_DEPTH_BITS by doing: int depth; glGetIntegerv(GL_DEPTH_BITS, &depth); NSLog(@"%i bits depth", depth); And I got 24 bits depth. Which is odd because in Interface Builder I set it up to be 32-bit in my NSOpenGLView.
  6. Thanks for the help it is in the center now, but the thing doesn't move correct still: It is really choppy unlike before when I didn't warp the pointer. I have changed my rotateCamera function to this, it doesn't update when I add the glutWarpPointer: void rotateCamera(int x, int y) { rotx = (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2); roty = (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2); glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2); } Any suggestions?
  7. Hi, I have tried your suggestions, but your glutWarpPointer parameters look identical to mine (with different spacing) and it still isn't working. (Oops, nvm. I forgot to divide by 2, but it still isn't in the center for some odd reason using your code). Also, I have tried changing my rotateCamera function to: void rotateCamera(int x, int y) { rotx = (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2); roty = (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2); glutWarpPointer(glutGet(GLUT_WINDOW_X)+glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_Y)+glutGet(GLUT_WINDOW_HEIGHT)/2); } but, it makes the thing look jerky, and it doesn't rotate correctly.
  8. So originally I had in a function that handled rotation: void rotateCamera(int x, int y) { mouseDeltaX = mouseDeltaX - x; //Get the change. mouseDeltaY = mouseDeltaY - y; rotx = (float)mouseDeltaX; //Assign it to a global variable value that would actually rotate everything. roty = (float)mouseDeltaY; mouseDeltaX = x; //Change mouseDeltaX and mouseDeltaY to the current x mouseDeltaY = y; } It was passed to the glutPassiveMotionFunc(rotateCamera). My game is being constantly updated and near the top of the display function I would have: glRotatef(rotx*0.01, 0.0f, 1.0f, 0.0f); glRotatef(roty*0.01, 1.0f, 0.0f, 0.0f); And to make sure everything wasn't being constant rotated I would use a function I passed to glutIdleFunc: void isIdle() { rotx = 0; roty = 0; } This would cause the thing to stop when I was done. I got it to work, but the only problem is I need to keep the mouse in the center of the window or else it would hit the corners of the screen and prevent further rotation. I tried making a function: void handleCamera(int x) {; glutWarpPointer(glutGet(GLUT_WINDOW_X)+glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_Y)+glutGet(GLUT_WINDOW_HEIGHT)); glutTimerFunc(0.5, handleCamera, 0); } Which is passed to a glutTimerFunc initially. First off: no movement of the scene occurs except for the very begging when the mouse is warped. Second: The mouse for some reason isn't in the center of the window. Will you please help me with these problems? Thanks in advanced. Note: Oops, wrong place, this should probably in "For Beginners" sorry. Can someone switch it? I can't seem to find out how.
  9. Is there another way to do this? Instead of changing the variables I just call transform instead?
  10. I am trying to move a cube backwards when I press forward. But, it is not working exactly right, it will translate the cube 1 "unit" forward and no more. My code that handles the key presses as such: void arrowKeys(int key, int x, int y) { if (key == GLUT_KEY_UP) { z_cam = 1; glutPostRedisplay(); } else if (key == GLUT_KEY_DOWN) { z_cam = -1; glutPostRedisplay(); } } And for my drawing function I do this: void draw() { glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -10.0); glPushMatrix(); glTranslatef(x_cam, y_cam, z_cam); glBindBuffer(GL_ARRAY_BUFFER, cubeBuffer);//Draws the cube here... glVertexPointer(3, GL_FLOAT, 3*sizeof(float), 0);//here glDrawArrays(GL_QUADS, 0, 8);//and here glPopMatrix(); z_cam = 0; x_cam = 0; y_cam = 0; glutSwapBuffers(); } Basically what I am doing is checking if an arrow key has been pressed. Then I change a global variable z_cam (and eventually x_cam and y_cam) to a different number according to which way I want the cube to move. But, it only moves once. I believe the problem is in the way I am pushing and popping the matrix, but I do not know what to do about it. Any help would be greatly appreciated.
  11. Ok thanks. I will do that.
  12. So I have this function that is my param for the function glutKeyboardFunc: void keypress(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; case GLUT_KEY_UP: z_cam = -10.0; std::cout << "here"; glutPostRedisplay(); break; } } The escape key works fine but, for the GLUT_KEY_UP I am not able to get it to even print "here" out. Any help to as why this is happening would be appreciated. If you need more code just ask.
  13. Figured it out, did not reset counter variable...
  14. @Erik: I have tried what you suggest. But, I am still getting a translation. Before redisplay: [attachment=1628:Screen shot 2011-03-12 at 9.29.35 PM.png] After redisplay: [attachment=1629:Screen shot 2011-03-12 at 9.29.47 PM.png]
  15. I need to redisplay my OpenGL draw function which is as follows: void drawMolecule() { glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (0, 400, 400, 0, 0, 1); glMatrixMode (GL_MODELVIEW); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); for (int i = 0; i < 100; i++) { getColor(grid); getTransform(grid); glBegin(GL_QUADS); glVertex2i(0, 0); glVertex2i(20, 0); glVertex2i(20, 20); glVertex2i(0, 20); glEnd(); } glutSwapBuffers(); } But, when I do so, everything gets shifted down when I call glutPostRedisplay in another function... How can I stop it from shifting down? I am calling the translations in the getTransform function which is defined as follows: void getTransform(point x) { glTranslatef(21, 0, 0.0); if (counter % 10 == 0 && counter >= 10) { glTranslatef(0, 21, 0); glTranslatef(-210, 0, 0); } counter++; } Any help would be appreciated.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!