Sign in to follow this  
Jungletoe

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

Recommended Posts

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

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]

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]

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.

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!

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.

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]

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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628345
    • Total Posts
      2982191
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now