Hello, i practise with a project.
I wrote a program simple camera && moving system.
While im rotating camera with right mouse button ( something like 45 degree) if i move with “w” button. i see screen / ground texture shakes.
How i can fix it?
Note: I couldt add files and link of program. Forum doesnt let me? ( When i edited message i added links)
I draw on draw function of class.
Edit:
www.dosya.tc/server14/Nr2x5o/Oyun.rar.html
Here is the link of exe and project. wait 30 seconds at bottom side of website.
Direct link:
www.dosya.tc/en2.php?a=server14/Nr2x5o/Oyun.rar&b=cb4930b24f4beaa7fbd7b72b63881693
Main.cpp
#include "header.h"
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400,200);
glutCreateWindow("Program");
sistem.initRender();
glutDisplayFunc(drawScene);
glutKeyboardFunc(getKey);
glutKeyboardUpFunc(releaseKey);
glutReshapeFunc(resizescreen);
glutMouseFunc(getMouse);
glutMotionFunc(cordMouse);
glutTimerFunc(25, update, 0);
glutMainLoop();
}
Header.h:
#include <iostream>
#include <string>
#include <math.h>
#include <stdio.h>
#include "imageloader.h"
#include <GL/gl.h>
#include <stdlib.h>
#define GLUT_DISABLE_ATEXIT_HACK
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
#define PI 3.14159265
class System{
private:
int ender;
int keyStates[255];
int mouseStates[3][3];
float mx,my;
GLuint tex;
GLuint ktex;
GLuint gtex;
GLuint htex;
GLuint dtex;
GLuint btex;
double rot;
double rot2;
double roty;
int savex, savey;
double movez, movex;
public:
System(){
ender=1;
savex=0;
savey=0;
movez=0.0;
movex=0.0;
rot=0;
}
~System(){
ender=0;
}
void detectKey(int a, int r);
void detectKey(int a, int b, int r);
void draw();
void rotater();
void initRender();
void gsaves();
void drawaxis();
void skybox();
void drawsky();
GLuint loadTexture(Image* image);
void starttexture(GLuint textu);
void getmcord(int x, int y);
void drawcube(float x, float y, float z, float corx, float corz);
};
System sistem;
GLuint System::loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}
void System::drawsky(){
glColor3f(1,1,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, btex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-30,-10,30);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-30,-10,-30);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-30,20,-30);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-30,20,30);
glEnd();
glBindTexture(GL_TEXTURE_2D, dtex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(30,-10,-30);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(30,-10,+30);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(30,20,+30);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(30,20,-30);
glEnd();
glBindTexture(GL_TEXTURE_2D, ktex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-30,-10,-30);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(30,-10,-30);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(30,20,-30);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-30,20,-30);
glEnd();
glBindTexture(GL_TEXTURE_2D, gtex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-30,-10,30);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(30,-10,30);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(30,20,30);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-30,20,30);
glEnd();
glBindTexture(GL_TEXTURE_2D, htex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-30,20,-30);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(30,20,-30);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(30,20,30);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-30,20,30);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void System::skybox(){
Image* image = loadBMP("guney.bmp");
gtex= loadTexture(image);
delete image;
image = loadBMP("hava.bmp");
htex= loadTexture(image);
delete image;
image = loadBMP("kuzey.bmp");
ktex= loadTexture(image);
delete image;
image = loadBMP("dogu.bmp");
dtex= loadTexture(image);
delete image;
image = loadBMP("bati.bmp");
btex= loadTexture(image);
delete image;
}
void System::drawaxis(){
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(-20,0,0);
glVertex3f(20,0,0);
glColor3f(0,1,0);
glVertex3f(0,20,0);
glVertex3f(0,-20,0);
glColor3f(0,0,1);
glVertex3f(0,0,20);
glVertex3f(0,0,-20);
glEnd();
}
void System::gsaves(){
savex=mx;
savey=my;
}
void System::rotater(){
glRotatef(rot,0,1,0);
if(mouseStates[2][0]==1 && my!= savey && my!=mouseStates[2][2]){
roty-=(float) (my- mouseStates[2][2]) / glutGet(GLUT_WINDOW_HEIGHT) ;
if(roty>6) roty=6;
if(roty<-6) roty=-6;
}
if(mouseStates[2][0]==1 && mx!= savex && mx!=mouseStates[2][1]){
rot += (mx-mouseStates[2][1]) * 90 / glutGet(GLUT_WINDOW_WIDTH)/2;
if(rot>360)rot-=360;
if(rot<0)rot= 360+rot;
//a+=cos(rot * PI / 180);
//b+=sin(rot * PI / 180);
}
if(mouseStates[0][0]==1 && mx!= savex && mx!=mouseStates[0][1]){
rot2 += (mx-mouseStates[0][1]) * 90 / glutGet(GLUT_WINDOW_WIDTH)/2;
if(rot2>360)rot2-=360;
if(rot2<0)rot2= 360+rot2;
//a+=cos(rot * PI / 180);
//b+=sin(rot * PI / 180);
}
}
void System::getmcord(int x, int y){
mx=x;
my=y;
}
void System::detectKey(int a, int r){
keyStates[a] = r;
if(a==87 || a==119){
movex -= sin(rot*PI/180)/3;
movez -= cos(rot*PI/180)/3;
}
if(a==65 || a==97){
rot+=5;
}
if(a==68 || a==100){
rot-=5;
}
if(a==83 || a==115){
movex += sin(rot*PI/180)/3;
movez += cos(rot*PI/180)/3;
}
}
void System::detectKey(int a, int b, int r){
mouseStates[a][b] = r;
if(mouseStates[0][0]==0) rot2=rot;
}
void System::starttexture(GLuint textu){
glColor3f(1,1,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textu);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
void System::draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(mouseStates[0][0]==1)
gluLookAt(sin(rot2*PI/180)*6 +movex ,3, cos(rot2*PI/180)*6 +movez , movex, 0, movez, 0, 1, 0);
else
gluLookAt(sin(rot*PI/180)*(10-fabs(roty)/4) +movex ,3-(roty/2), cos(rot*PI/180)*(10-(fabs(roty)/4)) +movez , -sin(rot*PI/180)*6 + movex, roty, -cos(rot*PI/180)*6 +movez, 0, 1, 0);
drawaxis();
drawsky();
glPushMatrix();
glColor3f(0,0,1);
glTranslatef(movex, 0, movez);
rotater();
drawcube(2,1,1, 0,0);
glPopMatrix();
glColor3f(0,1,0);
drawcube(1,1,1,10,0);
glColor3f(1,0,0);
drawcube(1,3,1,0,-10);
starttexture(tex);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-30,-0.8,-30);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(30,-0.8,-30);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(30,-0.8,30);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-30,-0.8,30);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void System::drawcube(float x, float y, float z, float corx, float corz){
glPushMatrix();
glTranslatef(corx,0,corz);
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
glVertex3f(-x/2,-y/2,-z/2);
glVertex3f(-x/2,-y/2,z/2);
glVertex3f(x/2,-y/2,z/2);
glVertex3f(x/2,-y/2,-z/2);
glNormal3f(0,1,0);
glVertex3f(-x/2,y/2,-z/2);
glVertex3f(-x/2,y/2,z/2);
glVertex3f(x/2,y/2,z/2);
glVertex3f(x/2,y/2,-z/2);
glNormal3f(0,0,-1);
glVertex3f(-x/2,y/2,-z/2);
glVertex3f(-x/2,-y/2,-z/2);
glVertex3f(x/2,-y/2,-z/2);
glVertex3f(x/2,y/2,-z/2);
glNormal3f(-1,0,0);
glVertex3f(-x/2,y/2,-z/2);
glVertex3f(-x/2,-y/2,-z/2);
glVertex3f(-x/2,-y/2,z/2);
glVertex3f(-x/2,y/2,z/2);
glNormal3f(1,0,0);
glVertex3f(x/2,y/2,-z/2);
glVertex3f(x/2,-y/2,-z/2);
glVertex3f(x/2,-y/2,z/2);
glVertex3f(x/2,y/2,z/2);
glNormal3f(0,0,1);
glVertex3f(-x/2,y/2,z/2);
glVertex3f(-x/2,-y/2,z/2);
glVertex3f(x/2,-y/2,z/2);
glVertex3f(x/2,y/2,z/2);
glEnd();
glPopMatrix();
}
void System::initRender(){
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
skybox();
Image* image = loadBMP("cimen.bmp");
tex= loadTexture(image);
delete image;
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
}
void getKey(unsigned char key, int x, int y){
sistem.detectKey(key, 1);
}
void releaseKey(unsigned char key, int x, int y){
sistem.detectKey(key, 0);
switch(key){
case 27://ascii table escape
exit(0);
case 127:
system("cls");
}
}
void getMouse(int button, int state, int x, int y){
if (button == GLUT_LEFT_BUTTON){
if(state == GLUT_DOWN) sistem.detectKey(0, 0, 1);
if(state == GLUT_UP) sistem.detectKey(0,0,0);
sistem.detectKey(0,1,x);
sistem.detectKey(0,2,y);
}
if (button == GLUT_MIDDLE_BUTTON){
if(state == GLUT_DOWN) sistem.detectKey(1, 0, 1);
if(state == GLUT_UP) sistem.detectKey(1,0,0);
sistem.detectKey(1,1,x);
sistem.detectKey(1,2,y);
}
if (button == GLUT_RIGHT_BUTTON){
if(state == GLUT_DOWN) sistem.detectKey(2, 0, 1);
if(state == GLUT_UP){
sistem.detectKey(2,0,0);
sistem.gsaves();
}
sistem.detectKey(2,1,x);
sistem.detectKey(2,2,y);
}
}
void cordMouse(int x, int y){
sistem.getmcord(x,y);
}
void resizescreen(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h, 1.0, 200.0);
}
void drawScene(){
sistem.draw();
}
void update(int value){
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}