[FIXED] Isometric View + Texturing? (OpenGL)

Started by
6 comments, last by Jungletoe 12 years, 4 months ago
[size=7]FIXED, THANKS!
[size="5"](SOURCE INCLUDED @ LAST POST)



INTRODUCTION


I've begun work on a 3D OpenGL isometric game engine. Lately I've attempted two things:
1) Texturing a Cube with a .BMP
2) Setting up an Isometric View


PROBLEM

I found the code for the isometric view on GameDev.net and the code for the texturing here. When using the two snippets separately, they work. When using them together, they don't and look like like this:
126fc2b8333787aa41fa9cc8c499e61b.PNG



As you can see, it is textured properly, yet it's not isometric. I think this is due to the isometric code messing up the view.

Here is the isometric code that I put in my init() function:


void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); //RGB + Alpha
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640 / 480, 1.0, 500.0); //sets camera params
glMatrixMode(GL_MODELVIEW);
SDL_WM_SetCaption( "OpenGL Game", NULL );
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

// ISOMETRIC VIEW CODE
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0f, 100.0f, -100.0f, 100.0f, -500.0f, 500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(35.264f, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glScalef(1.0f, 1.0f, -1.0f);

Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);
delete image;
}



When I take out the isometric code part it works perfectly, but isn't isometric (this is just some extra coloring stuff I did too, it won't affect the texturing):

5a6d95b23256a1fc58d2ad9186b772ec.PNG



CONCLUSION


I need to figure out how to make my objects show up isometrically while also allowing me to use textures. Do any of you know any isometric code that would work to set up an isometric camera? I've tried a couple of codes from different people without success.
Advertisement
[font="Arial"]You shouldn't be transforming your geometry like that to achieve an isometric effect. To achieve an isometric effect you only need to set your camera in the correct position and use an isometric projection. [/font]
[font="Arial"]
[/font]
[font="Arial"]// Set projection transform [/font]
[font="Arial"]glMatrixMode(GL_PROJECTION);[/font]
[font="Arial"]glLoadIdentity(); [/font]
[font="Arial"]void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);
[/font]
[font="Arial"]// Set view transform [/font]
[font="Arial"]glMatrixMode(GL_MODELVIEW); [/font]
[font="Arial"][size=2]glLoadIdentity(); [/font]
[font="Arial"][font="Arial"]gluLookAt[/font](GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ);[/font]
[font="Arial"]
[/font]
[font="Arial"]// When drawing an object or a set of objects [/font]
[font=Arial]glMatrixMode(GL_MODELVIEW); [/font]
[font="Arial"]glPushMatrix();[/font]
[font="Arial"]
[/font]
[font=Arial]-- Use transforms here to set an object or set of objects in the correct position in world space [/font]
[font=Arial]-- Draw geometry for such here [/font]
[font="Arial"]
[/font]
[font="Arial"]glPopMatrix();[/font]
[font="Arial"]
[/font]
[font="Arial"]
[/font]
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
What are the values inside glLookAt(); ?

When I use your code with the supposed inputs:


// Set projection transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);

// Set view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0, 0.0);



It gives me this:

e2c5295bf98f2e65040ce3e6d04145e0.PNG



EDIT:
Hell, I'll just post all my source code...

TEXTURED ATTEMPT: (gives me a blank screen)


//*******************************************************************************************************************//
//********************************************* HEADERS ******************************************************//
//*******************************************************************************************************************//

// specific headers
#include "SDL.h"
#include "SDL_opengl.h"
#include "GL/glut.h"
#include "imageloader.h"
#include <iostream>
#include <stdio.h>


const int triangle = 1;
float angle = 0.0;
GLuint _textureId; //The id of the texture

//Makes the image into a texture, and returns the id of the texture
GLuint 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 drawCube(float size)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
//front face
glColor3f(1.0, 1.0, 1.0);
glVertex3f(size/2, size/2, size/2); //glTexCoord2f(0.0f, 0.0f);
glVertex3f(-size/2, size/2, size/2); //glTexCoord2f(1.0f, 0.0f);
glVertex3f(-size/2, -size/2, size/2); //glTexCoord2f(1.0f, 1.0f);
glVertex3f(size/2, -size/2, size/2); //glTexCoord2f(0.0f, 1.0f);

//left face
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-size/2, size/2, size/2);
glVertex3f(-size/2, size/2, -size/2);
glVertex3f(-size/2, -size/2, -size/2);
glVertex3f(-size/2, -size/2, size/2);

//back face
glColor3f(1.0, 0.0, 1.0);
glVertex3f(size/2, size/2, -size/2);
glVertex3f(-size/2, size/2, -size/2);
glVertex3f(-size/2, -size/2, -size/2);
glVertex3f(size/2, -size/2, -size/2);

//right face
glColor3f(0.5, 0.5, 0.5);
glVertex3f(size/2, size/2, -size/2);
glVertex3f(size/2, size/2, size/2);
glVertex3f(size/2, -size/2, size/2);
glVertex3f(size/2, -size/2, -size/2);

//top face
glColor3f(1.0, 0.5, 1.0);
glVertex3f(size/2, size/2, size/2);
glVertex3f(-size/2, size/2, size/2);
glVertex3f(-size/2, size/2, -size/2);
glVertex3f(size/2, size/2, -size/2);

//bottom face
glColor3f(1.0, 1.0, 1.0);
glVertex3f(size/2, -size/2, size/2);
glVertex3f(-size/2, -size/2, size/2);
glVertex3f(-size/2, -size/2, -size/2);
glVertex3f(size/2, -size/2, -size/2);
glEnd();
}

void init()
{
// Set projection transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);

// Set view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0, 0.0);

Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);
delete image;
}

void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glTranslatef(0.0, 0.0, -5.0);
//glRotatef(angle, 1.0, 1.0, 1.0);
drawCube(10.0);
}


int main( int agrc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen=SDL_SetVideoMode(600,400,32, SDL_SWSURFACE|SDL_OPENGL );
bool running = true;
Uint32 start;
SDL_Event event;
init();

while(running)
{
start=SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
}
}
display();
SDL_GL_SwapBuffers();
angle+=0.1;
if(angle > 360)
angle -= 360;
if(1000/30>(SDL_GetTicks()-start))
SDL_Delay(1000/30>(SDL_GetTicks()-start));
}

SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}




NON-TEXTURED ATTEMPT: (gives me picture above)


/* ************************************************************************************************ */
/* ****************************** INCLUDES *********************************** */
/* ************************************************************************************************ */


#include "SDL.h"
#include "SDL_opengl.h"
#include "stdio.h"
#include <string>
#include "CubeTypes.h"

const int triangle = 1;
float currentX = 0.0;
float currentY = 0.0;
float currentZ = 0.0;

/* ************************************************************************************************ */
/* ********************* INIT *********************************** */
/* ************************************************************************************************ */
void init()
{
/*
glClearColor(0.0, 0.0, 0.0, 1.0); //RGB + Alpha
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640 / 480, 1.0, 500.0); //sets camera params
glMatrixMode(GL_MODELVIEW);
SDL_WM_SetCaption( "OpenGL Game", NULL );
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.5);
*/

// Set projection transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);

// Set view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0, 0.0);
}

/* ************************************************************************************************ */
/* ********************* DISPLAY *********************************** */
/* ************************************************************************************************ */

void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glTranslatef(0.0, 0.0, -5.0);
//glRotatef( 0.0 , 1.0, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0f, 100.0f, -100.0f, 100.0f, -500.0f, 500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(35.264f, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glScalef(1.0f, 1.0f, -1.0f);

//current cube position
//drawSand(currentX, currentY, currentZ);

//drawDirt(1.0, 0.0, 0.0);
drawSand(0.0, 0.0, 0.0);
//drawSand(1.0, 0.0, 1.0);
//drawSand(1.0, 1.0, 1.0);
//drawDirt(2.0, 0.0, 1.0);
//drawDirt(2.0, 0.0, 0.0);

}

/* ************************************************************************************************ */
/* ********************* MAIN *********************************** */
/* ************************************************************************************************ */

int main( int agrc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen=SDL_SetVideoMode(600,400,32, SDL_SWSURFACE|SDL_OPENGL );
bool running = true;
Uint32 start;
SDL_Event event;
init();

while(running)
{
start=SDL_GetTicks();
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT){
running = false;
}

if (event.type == SDL_KEYDOWN){
SDLKey keyPressed = event.key.keysym.sym;

switch (keyPressed)
{
case SDLK_ESCAPE:
running = false;
break;
}
}
}
display();
SDL_GL_SwapBuffers();
if(1000/30>(SDL_GetTicks()-start))
SDL_Delay(1000/30>(SDL_GetTicks()-start));
}

SDL_Quit();
return 0;
}
From the looks of things, you're using glLookAt properly, the first 3 values are the world space position of the camera eye, the next three values are the world space position of where the eye is looking, and the last 3 values are an up vector to determine the roll of the camera.
You need to give openGL texture coordinates for each of the vertices as well as position.

http://www.opengl.or.../glTexCoord.xml

I noticed that you had glTexCoord2f commented out. Your texture binding looks fine, also:


[color="#000000"]glTexParameteri[color="#666600"]([color="#000000"]GL_TEXTURE_2D[color="#666600"],[color="#000000"] GL_TEXTURE_MIN_FILTER[color="#666600"],[color="#000000"] GL_NEAREST[color="#666600"]);[color="#000000"]
glTexParameteri[color="#666600"]([color="#000000"]GL_TEXTURE_2D[color="#666600"],[color="#000000"] GL_TEXTURE_MAG_FILTER[color="#666600"],[color="#000000"] GL_NEAREST[color="#666600"]);[color="#000000"]
glTexParameteri[color="#666600"]([color="#000000"]GL_TEXTURE_2D[color="#666600"],[color="#000000"] GL_TEXTURE_MIN_FILTER[color="#666600"],[color="#000000"] GL_LINEAR[color="#666600"]);[color="#000000"]
glTexParameteri[color="#666600"]([color="#000000"]GL_TEXTURE_2D[color="#666600"],[color="#000000"] GL_TEXTURE_MAG_FILTER[color="#666600"],[color="#000000"] GL_LINEAR[color="#666600"]);

You can move that code to the loadTexture function if you want, you don't have to set it before drawing if it's already set for the texture you're binding.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
I changed the code and got the same result as the "Textured-Attempt" picture above. Here's the code again:





//*******************************************************************************************************************//
//********************************************* HEADERS ******************************************************//
//*******************************************************************************************************************//

// specific headers
#include "SDL.h"
#include "SDL_opengl.h"
#include "GL/glut.h"
#include "imageloader.h"
#include <iostream>
#include <stdio.h>


const int triangle = 1;
float angle = 0.0;
GLuint _textureId; //The id of the texture

//Makes the image into a texture, and returns the id of the texture
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//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 drawCube(float size)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-.5f, -.5f, .5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( .5f, -.5f, .5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( .5f, .5f, .5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-.5f, .5f, .5f); // Top Left Of The Texture and Quad

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-.5f, -.5f, -.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-.5f, .5f, -.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( .5f, .5f, -.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( .5f, -.5f, -.5f); // Bottom Left Of The Texture and Quad

// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-.5f, .5f, -.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-.5f, .5f, .5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( .5f, .5f, .5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( .5f, .5f, -.5f); // Top Right Of The Texture and Quad

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-.5f, -.5f, -.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( .5f, -.5f, -.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( .5f, -.5f, .5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-.5f, -.5f, .5f); // Bottom Right Of The Texture and Quad

// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( .5f, -.5f, -.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( .5f, .5f, -.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( .5f, .5f, .5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( .5f, -.5f, .5f); // Bottom Left Of The Texture and Quad

// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-.5f, -.5f, -.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-.5f, -.5f, .5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-.5f, .5f, .5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-.5f, .5f, -.5f); // Top Left Of The Texture and Quad
glEnd();
}

void init()
{
// Set projection transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);

// Set view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0, 0.0);

Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);
delete image;
}

void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glTranslatef(0.0, 0.0, -5.0);
//glRotatef(angle, 1.0, 1.0, 1.0);
drawCube(10.0);
}


int main( int agrc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen=SDL_SetVideoMode(600,400,32, SDL_SWSURFACE|SDL_OPENGL );
bool running = true;
Uint32 start;
SDL_Event event;
init();

while(running)
{
start=SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
}
}
display();
SDL_GL_SwapBuffers();
angle+=0.1;
if(angle > 360)
angle -= 360;
if(1000/30>(SDL_GetTicks()-start))
SDL_Delay(1000/30>(SDL_GetTicks()-start));
}

SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}



And here is the pic of what it does:
c1c7330db89b83b52740904fc5d7859f.PNG

I've literally followed every single tutorial for isometric views on the internet and all give the exact same code you gave, so I'm very curious as to why this wont work. I've also tried commenting out various chunks/line of the code to see if it would work but to no success.

Any more ideas? You've been great so far!
Aren't you setting the modelview matrix to identity every time you render a new frame? It sure seems so to me.



Aren't you setting the modelview matrix to identity every time you render a new frame? It sure seems so to me.


THANK YOU SO MUCH!

I commented out glLoadIdentity and it now gives me this:
577d5308d831646dd4fdc9398672d4f4.PNG


I'm still kind of curious as to why it only prints that pyramid-looking shape though... My code hasn't changed from my above post besides commenting out glLoadIdentity.
[size="7"]
[size="7"]FIXED
[size="7"]

For the sake of helping the community, here is all my source code along with the product. Thanks so much!




//*******************************************************************************************************************//
//********************************************* HEADERS ******************************************************//
//*******************************************************************************************************************//

// specific headers
#include "SDL.h"
#include "SDL_opengl.h"
#include "GL/glut.h"
#include "imageloader.h"
#include <iostream>
#include <stdio.h>


const int triangle = 1;
float angle = 0.0;
GLuint _textureId; //The id of the texture

//Makes the image into a texture, and returns the id of the texture
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//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 drawCube(float size)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-.5f, -.5f, .5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( .5f, -.5f, .5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( .5f, .5f, .5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-.5f, .5f, .5f); // Top Left Of The Texture and Quad

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-.5f, -.5f, -.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-.5f, .5f, -.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( .5f, .5f, -.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( .5f, -.5f, -.5f); // Bottom Left Of The Texture and Quad

// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-.5f, .5f, -.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-.5f, .5f, .5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( .5f, .5f, .5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( .5f, .5f, -.5f); // Top Right Of The Texture and Quad

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-.5f, -.5f, -.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( .5f, -.5f, -.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( .5f, -.5f, .5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-.5f, -.5f, .5f); // Bottom Right Of The Texture and Quad

// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( .5f, -.5f, -.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( .5f, .5f, -.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( .5f, .5f, .5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( .5f, -.5f, .5f); // Bottom Left Of The Texture and Quad

// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-.5f, -.5f, -.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-.5f, -.5f, .5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-.5f, .5f, .5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-.5f, .5f, -.5f); // Top Left Of The Texture and Quad
glEnd();
}

void init()
{
// Set projection transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 10.0f);

// Set view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);
delete image;
}

void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

//glTranslatef(0.0, 0.0, -5.0);
//glRotatef(angle, 1.0, 1.0, 1.0);
drawCube(10.0);
}


int main( int agrc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen=SDL_SetVideoMode(600,400,32, SDL_SWSURFACE|SDL_OPENGL );
bool running = true;
Uint32 start;
SDL_Event event;
init();

while(running)
{
start=SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
}
}
display();
SDL_GL_SwapBuffers();
angle+=0.1;
if(angle > 360)
angle -= 360;
if(1000/30>(SDL_GetTicks()-start))
SDL_Delay(1000/30>(SDL_GetTicks()-start));
}

SDL_FreeSurface(screen);
SDL_Quit();
return 0;}



RESULT:
037930c9734cca2eaceba962291cb70e.PNG


WARNING: You need the required "vtr.bmp" to make this code work. You can use any size as long as it's a power of two.


What I did wrong:
I changed glOrtho and I got it work now. The values I used previously were wrong.

This topic is closed to new replies.

Advertisement