Wie arbeiten Glortho und Glvertex3f zusammen zusammen?C++

Programme in C++. Entwicklerforum
Anonymous
 Wie arbeiten Glortho und Glvertex3f zusammen zusammen?

Post by Anonymous »

Ich habe einen Code, der ein flaches Rechteck hinter einem rotierenden Würfel zeichnen soll. Bitte erläutern Sie die Mathematik, wie Glortho in Verbindung mit Glvertex3f funktioniert, damit ich das Rechteck hinter dem Würfel erscheint.

Code: Select all

//-lglut -lGLEW -lGL -lGLU -lm

#include  // Include the GLEW header file
#include  // Include the GLUT header file
#include 
#include 
#include 
#include
#include
#include
#include 

GLuint texture;
GLfloat angle = 0.0;

const float zNNear=0.5;
const float zFar=-0.5;

// Rotate X
double rX=0;
// Rotate Y
double rY=0;

bool running = true;
bool inc = false;
bool inc2 = false;
bool inc3 = false;
bool inc4 = false;

bool* keyStates = new bool[256];

float yVal = 0.0f;
float yVal2 = 0.1f;
float yVal3 = 0.0f;
float yVal4 = 0.1f;

GLuint LoadBMP(const char *fileName)
{
FILE *file=fopen(fileName,"rb");
if (!file)
{
printf("Image could not be loaded\n");
return 0;
}

unsigned char header[54];
fread(header,1,54,file);

unsigned int dataPos=*(int*)&(header[0x0A]);
unsigned int imageSize=*(int*)&(header[0x22]);
unsigned int width=*(int*)&(header[0x12]);
unsigned int height=*(int*)&(header[0x16]);

if(imageSize == 0)
{
imageSize = width*height*3;
}
if(dataPos == 0)
{
dataPos = 54;
}

unsigned char *data = new unsigned char[imageSize];
fread(data,1,imageSize,file);
fclose(file);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_BGR,
GL_UNSIGNED_BYTE,data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete(data);

return texture;
}

void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}

void keyOperations(void)
{
if(keyStates['a'])
{
}
}

void drawCube(void)
{
glBindTexture(GL_TEXTURE_2D, texture);
glOrtho(0.0,0.0,0.0,0.0,4.0,4.0);
glBegin(GL_QUADS);

//front
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zNNear);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,-0.5f,zNNear);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,0.5f,zNNear);

//back
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zFar);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,0.5f,zFar);

//top
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,0.5f,zNNear);

//bottom
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,-0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,-0.5f,zNNear);

//right
glTexCoord2f(0.0f,0.0f);glVertex3f(0.5f,-0.5f,-zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(0.5f,-0.5f,zNNear);

//left
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.5f,-0.5f,zFar);
glTexCoord2f(0.5f,0.0f)  ;glVertex3f(-0.5f,0.5f,zFar);
glTexCoord2f(0.5f,0.5f)  ;glVertex3f(-0.5f,0.5f,zNNear);
glTexCoord2f(0.0f,0.5f)  ;glVertex3f(-0.5f,-0.5f,zNNear);

glEnd();
}

void display(void)
{
keyOperations();

glClearColor(0.0f,0.0f,0.0f,1.0f);//set background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,10.0,10.0);
glutPostRedisplay();

if(inc)
yVal -= 0.005f;
else
yVal += 0.005f;

if(yVal1.0f)
inc=true;

if(inc2)
yVal2 -= 0.005f;
else
yVal2 += 0.005f;

if(yVal21.0f)
inc2=true;

if(inc3)
yVal3 -= 0.005f;
else
yVal3 += 0.005f;

if(yVal31.0f)
inc3=true;

if(inc4)
yVal4 -= 0.005f;
else
yVal4 += 0.005f;

if(yVal41.0f)
inc4=true;

glColor3f(yVal, 0.0, 0.0); // Red
//glRectf(-1.0,1.0,1.0,0.5);
glBegin(GL_QUADS);
glVertex3f(0.25,0.25,-1.0);
glVertex3f(0.75,0.25,-1.0);
glVertex3f(0.75,0.75,-1.0);
glVertex3f(0.25,0.75,-1.0);

//glVertex3f(-1.0,1.0,2.0);
//glVertex3f(1.0,0.5,2.0);

glEnd();

//glColor3f(0.0, yVal2, 0.0); // Green
//glRectf(-1.0,0.5,1.0,0.0);

//glColor3f(yVal3, 0.25, 0.0); // Orange
//glRectf(-1.0,0.0,1.0,-0.5);

//glColor3f(0.0, yVal4, 0.25); // Orange
//glRectf(-1.0,-0.5,1.0,-1.0);

glRotatef( rX, 1.0, 0.0, 0.0 );

glRotatef( rY, 0.0, 1.0, 0.0 );

rX+=0.1;
rY+=0.1;

drawCube();

glutSwapBuffers();
}

void reShape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(GLfloat)w/(GLfloat)h, 1.0,100.0);
glMatrixMode(GL_MODELVIEW);
}

void init()
{
glEnable(GL_TEXTURE_2D);
texture=LoadBMP("Me8.bmp");
}

void reshape(int width, int height)
{
glViewport(0,0,(GLsizei)width, (GLsizei)height);

glMatrixMode(GL_PROJECTION); //glMatrixMode
glLoadIdentity();//replace the current matrix with the identity matrix

glMatrixMode(GL_MODELVIEW);
}

void keyPressed(unsigned char key, int x, int y)
{
keyStates[key]=true;
}

void keyUp(unsigned char key, int x, int y)
{
keyStates[key]=false;
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Photo Example With Coloured Background");

init();

glEnable(GL_DEPTH_TEST);

glutDisplayFunc(display);//glutDisplayFunc

glutReshapeFunc(reshape);//sets the reshape callback for the current window.

glutKeyboardFunc(keyPressed);//sets the keyboard callback for the current window

glutKeyboardUpFunc(keyUp);//glutKeyboardUpFunc

glutMainLoop();//enters the GLUT event processing loop.

return 0;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post