Sign in to follow this  
Followers 0
Jungletoe

OpenGL
[FIXED] Isometric View + Texturing? (OpenGL)

7 posts in this topic

[b][size=7]FIXED, THANKS! [/size][/b]
[b][size="5"](SOURCE INCLUDED @ LAST POST)[/size]
[b]
[/b]
[b]
[/b]INTRODUCTION[/b]
[b]
[/b]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


[b]PROBLEM[/b]

I found the code for the isometric view on GameDev.net and the code for the texturing [url="http://www.videotutorialsrock.com/opengl_tutorial/textures/text.php"]here[/url]. When using the two snippets separately, they work. When using them together, they don't and look like like this:
[img]http://www.freemmorpgmaker.com/files/imagehost/pics/126fc2b8333787aa41fa9cc8c499e61b.PNG[/img]



As you can see, it is textured properly, yet it's not isometric. I think this is due to the [url="http://www.gamedev.net/topic/319421-opengl-isometric-ortho/"]isometric code[/url] messing up the view.

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

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;
}
[/code]


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):

[img]http://www.freemmorpgmaker.com/files/imagehost/pics/5a6d95b23256a1fc58d2ad9186b772ec.PNG[/img]



[b]CONCLUSION[/b]
[b]
[/b]
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.
0

Share this post


Link to post
Share on other sites
[font="Arial"][size="2"]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. [/size][/font]
[font="Arial"][size="2"]
[/size][/font]
[font="Arial"][size="2"]// Set projection transform [/size][/font]
[font="Arial"][size="2"]glMatrixMode(GL_PROJECTION);[/size][/font]
[font="Arial"][size="2"]glLoadIdentity(); [/size][/font]
[font="Arial"][size="2"]void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);[/size]
[/font]
[font="Arial"]// Set view transform [/font]
[font="Arial"]glMatrixMode(GL_MODELVIEW); [/font]
[font="Arial"][size=2]glLoadIdentity[/size](); [/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]
1

Share this post


Link to post
Share on other sites
What are the values inside glLookAt(); ?

When I use your code with the supposed inputs:

[code]
// 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);[/code]


It gives me this:

[img]http://www.freemmorpgmaker.com/files/imagehost/pics/e2c5295bf98f2e65040ce3e6d04145e0.PNG[/img]



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

TEXTURED ATTEMPT: (gives me a blank screen)

[code]
//*******************************************************************************************************************//
//********************************************* 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;
}[/code]



NON-TEXTURED ATTEMPT: (gives me picture above)

[code]
/* ************************************************************************************************ */
/* ****************************** 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;
}[/code]
0

Share this post


Link to post
Share on other sites
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.

[url="http://www.opengl.org/sdk/docs/man/xhtml/glTexCoord.xml"]http://www.opengl.or.../glTexCoord.xml[/url]

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


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

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.
1

Share this post


Link to post
Share on other sites
I changed the code and got the same result as the "Textured-Attempt" picture above. Here's the code again:

[code]



//*******************************************************************************************************************//
//********************************************* 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;
}
[/code]


And here is the pic of what it does:
[img]http://www.freemmorpgmaker.com/files/imagehost/pics/c1c7330db89b83b52740904fc5d7859f.PNG[/img]

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!
0

Share this post


Link to post
Share on other sites
Aren't you setting the modelview matrix to identity every time you render a new frame? It sure seems so to me.
1

Share this post


Link to post
Share on other sites
[quote name='Syranide' timestamp='1322734603' post='4889362']
Aren't you setting the modelview matrix to identity every time you render a new frame? It sure seems so to me.
[/quote]

THANK YOU SO MUCH!

I commented out glLoadIdentity and it now gives me this:
[img]http://www.freemmorpgmaker.com/files/imagehost/pics/577d5308d831646dd4fdc9398672d4f4.PNG[/img]


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.
0

Share this post


Link to post
Share on other sites
[size="7"][b]
[/b][/size][size="7"][b]FIXED[/b][/size]
[size="7"][b]
[/b][/size]
[size="2"]For the sake of helping the community, here is all my source code along with the product. Thanks so much![/size]


[code]

//*******************************************************************************************************************//
//********************************************* 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;}[/code]


[b][size="3"]RESULT:[/size][/b]
[size="2"][b][img]http://www.freemmorpgmaker.com/files/imagehost/pics/037930c9734cca2eaceba962291cb70e.PNG[/img][/b][/size]
[size="2"][b]
[/b][/size]
[size="2"][b]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.[/b][/size]
[size="2"][b]
[/b][/size]
[size="2"][b]What I did wrong:[/b][/size]
[size="2"]I changed glOrtho and I got it work now. The values I used previously were wrong.[/size]
[b]
[/b]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0