Sign in to follow this  

texture mapping a quad in opengl

This topic is 1953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to texture a quad using opengl and c++.here is the code I am using.
<code>
#include <iostream>
#include <stdlib.h>
#include "glut.h" // OpenGL toolkit
#include "GL/gl.h"
#include "GL/glu.h"
#include <windows.h>
#include <stdio.h>
#include "GLAux.h"
GLuint texture[1];
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if(!Filename)
{
return NULL;
}
File=fopen(Filename,"r");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if(TextureImage[0]=LoadBMP("image1.bmp"))
{
Status=TRUE;
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if(TextureImage[0])
{
if(TextureImage[0]->data)
{
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
return Status;
}
int InitGL(GLvoid)
{
if(!LoadGLTextures())
{
return FALSE;
}
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
return TRUE;
}
GLfloat left = 0.0f;
GLfloat right = 0.0f;
// Initial square position and size
GLfloat x = 50.0f;
GLfloat y = 0.0f;
GLfloat rsize = 5;
// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 5.0f;
GLfloat ystep = 5.0f;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
//////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2f(0.0,0.0);
glTexCoord2f(1.0,0.0);
glVertex2f(50.0,0.0);
glTexCoord2f(1.0,1.0);
glVertex2f(50.0,50.0);
glTexCoord2f(0.0,1.0);
glVertex2f(0.0,50.0);
glEnd();
// Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(-135.0f,75.0f,-85.0f,65.0f);
glRectf(-95.0f,100.0f,-85.0f,75.0f);

glRectf(-125.0f,55.0f,-95.0f,45.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glRectf(135.0f,75.0f,85.0f,65.0f);
glRectf(95.0f,100.0f,85.0f,75.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glRectf(135.0f,-75.0f,85.0f,-65.0f);
glRectf(95.0f,-100.0f,85.0f,-75.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glRectf(-135.0f,-75.0f,-85.0f,-65.0f);
glRectf(-95.0f,-100.0f,-85.0f,-75.0f);
glColor3f(1.0f, 1.0f, 1.0f);
// Draw a filled rectangle with current color
glRectf(x, y, x + rsize, y - rsize);
// Flush drawing commands and swap
glutSwapBuffers();
}
///////////////////////////////////////////////////////////
// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{
// Reverse direction when you reach left or right edge
if(x > windowWidth-rsize || x < -windowWidth)
xstep = -xstep;
// Reverse direction when you reach top or bottom edge
if(y > windowHeight || y < -windowHeight + rsize)
ystep = -ystep;
if(y >= 60 && x >= -135 && x <= -85)
{
ystep = -ystep;
}
if(y >= 60 && x >= -135 && x <= -80)
{
xstep = -xstep;
}

if(y >= 60 && x <= 135 && x >= 80)
{
ystep = -ystep;
}
if(y >= 60 && x <= 135 && x >= 80)
{
xstep = -xstep;
}
if(y <= -60 && x >= -135 && x <= -85)
{
ystep = -ystep;
}
if(y <= -60 && x >= -135 && x <= -85)
{
xstep = -xstep;
}
if(y <= -60 && x <= 135 && x >= 85)
{
ystep = -ystep;
}
if(y <= -60 && x <= 135 && x >= 85)
{
xstep = -xstep;
}
// Actually move the square
x += xstep;
y += ystep;
// Check bounds. This is in case the window is made
// smaller while the rectangle is bouncing and the
// rectangle suddenly finds itself outside the new
// clipping volume
if(x > (windowWidth-rsize + xstep))
x = windowWidth-rsize-1;
else if(x < -(windowWidth + xstep))
x = -windowWidth -1;
if(y > (windowHeight + ystep))
y = windowHeight-1;
else if(y < -(windowHeight - rsize + ystep))
y = -windowHeight + rsize - 1;
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
///////////////////////////////////////////////////////////
// Setup the rendering state
void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w, int h)
{
GLfloat aspectRatio;
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
{
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
}
else
{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void PaddleLeft()
{
// glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0f,0.0f,0.0f);
glRectf(-125.0f-left,55.0f,-95.0f-left,45.0f);
glutSwapBuffers();
left+=1.0f;
}
void PaddleRight()
{

}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
{
exit(0);
break;
}
}
}
void mySpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT:
PaddleLeft();
break;
case GLUT_KEY_RIGHT:
PaddleRight();
break;
}
}
///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(300,100);
glutInitWindowSize(800,600);
glutCreateWindow("Warlords");
glutDisplayFunc(RenderScene);
AUX_RGBImageRec *LoadBMP(char *Filename);
int InitGL(GLvoid);
int LoadGLTextures();
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(keyboard);
glutSpecialFunc(mySpecialKeys);
glutTimerFunc(33, TimerFunction, 1);
SetupRC();
glutMainLoop();

return 0;
}
</code>

Share this post


Link to post
Share on other sites
I have a tutorial on how to do [url="http://lazyfoo.net/tutorials/OpenGL/05_texture_mapping_and_pixel_manipulation/index.php"]texture mapping in OpenGL[/url].

Share this post


Link to post
Share on other sites

This topic is 1953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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