//DrawColRect()//
//Draws a coloured rectangular quad
//Assumes you have begun GL_QUADS
void DrawColRect(int x,int y,int width,int height,char r,char g,char b)
{
glColor3ub(r,g,b); //Set the colour
glVertex2d(x,y);
glVertex2d(x+width,y);
glVertex2d(x+width,y+height);
glVertex2d(x,y+height);
}
//BlitTextRect()//
//Blits a textured rectanglular quad
//Assumes you have begun GL_QUADS
//Assumes you have bound the correct texture
void DrawTextRect(int x,int y,int width,int height,int left,int top,int right,int bottom)
{
glTexCoord2f(0.0,1.0);
glVertex2d(x,y);
glTexCoord2f(0.0,1.0);
glVertex2d(x+width,y);
glTexCoord2f(0.0,1.0);
glVertex2d(x+width,y+height);
glTexCoord2f(0.0,1.0);
glVertex2d(x,y+height);
}
//DrawGLScene()//
//Does all the drawing - called by WinMain
int DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //Clear the background
glLoadIdentity(); //Reset the modelview matrix
glEnable2D(); //Enable 2D
glBindTexture(GL_TEXTURE_2D,texture[0]); //Select Our Texture to draw with
glBegin(GL_QUADS); //We're here to draw rectangles!
//Drawing goes here - tiles, sprites, what have you
DrawColRect(32,32,32,32,155,155,30);
DrawTextRect(480,320,32,32,32,32,32,32);
glEnd();
glDisable2D();
return true;
}
The colored rect displays fine. The textured rect shows up too, but it is textureless.
[edited by - Dalton on April 4, 2004 3:20:23 PM]
Texture mapping 2D quads
I'm starting up a 2D engine with OpenGL. I'm using the glEnable2d() and glDisable2D() functions shown here to get into 2D mode.
My problem is with texture mapping. My texture loads successfully, stored at texture[0], a global GLuint (I am using the texture loading process explained by NeHe tutorial 6). The texture doesn't display when I map it to the vertices of a 2D quad. Perhaps it's because I'm using glTexCoord2f in 2D mode? Also, is there some way to map a texture directly from the coordinates of the pixels of the bitmap, instead of working on a 0.0-1.0 range?
Code:
as far as I can tell, you haven''t enabled textures yet. a call to glEnable( GL_TEXTURE_2D ); should do it. then, when you want to draw an untextured something, I believe you just bind texture 0 -- glBindTexture( GL_TEXTURE_2D, 0 );
hopefully that works for you
hopefully that works for you
You need to enable texturing when you draw the textured quad.
If you haven''t already, you might want to purchase the OpenGL programming guide (red book) if you are serious about getting into OpenGL programming. It has a good chapter on texture-mapping.
If you haven''t already, you might want to purchase the OpenGL programming guide (red book) if you are serious about getting into OpenGL programming. It has a good chapter on texture-mapping.
http://fly.cc.fer.hr/~unreal/theredbook/
That is a great link for OpenGL programming.
Another great one (especially for beginners) is
http://nehe.gamedev.net
Texturemapping like you are trying to do is the fastest way to
do 2D stuff in OpenGL, but if you just want to draw pixels, look
into glDrawPixels and related functions. Hope that helps.
It said "Requires Windows 98 or better", so I installed Linux.
That is a great link for OpenGL programming.
Another great one (especially for beginners) is
http://nehe.gamedev.net
Texturemapping like you are trying to do is the fastest way to
do 2D stuff in OpenGL, but if you just want to draw pixels, look
into glDrawPixels and related functions. Hope that helps.
It said "Requires Windows 98 or better", so I installed Linux.
Well, I think I know part of the problem. As a DirectDraw afficiando, I'm used to (x,y)=(0,0) being in the top left of the screen. It seems I'm working in an environment where (x,y)=(0,0) is in the bottom left of the screen. So I was drawing my vertices in the wrong order, counter-clockwise. I fixed the order, but still the texture doesn't show up. Here:
Is something still amiss?
[edited by - Dalton on April 4, 2004 8:30:12 PM]
glTexCoord2f(0.0f,0.0f); glVertex2d(x,y); glTexCoord2f(0.0f,1.0f); glVertex2d(x,y+height); glTexCoord2f(1.0f,1.0f); glVertex2d(x+width,y+height); glTexCoord2f(1.0f,0.0f); glVertex2d(x+width,y);
Is something still amiss?
[edited by - Dalton on April 4, 2004 8:30:12 PM]
The following is a program that I wrote that renders a flame
Please feel free to use my code.
Note: I am not responsible what so ever in what ever damage it
may cause nor am I responsible for any missuse of this code.
flame.c:
Please feel free to use my code.
Note: I am not responsible what so ever in what ever damage it
may cause nor am I responsible for any missuse of this code.
flame.c:
//#include "pow.h"#include <math.h>#include <stdlib.h>#include <stdio.h>#include </usr/include/GL/gl.h>#include </usr/include/GL/glu.h>#include </usr/include/GL/glut.h>#include "texture.h"#define maxpar 80#define PARTICLE 1#define maxsize 90float ParXPos[maxpar],ParYPos[maxpar],ParZPos[maxpar],Yamt[maxpar];float Red[maxpar],Green[maxpar],Blue[maxpar];static float xrot=50,yrot=0,zrot=0;void GLUTCALLBACK renderScene(void) { int i; double tim; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glClearDepth(1.0f); glLoadIdentity (); glBindTexture(GL_TEXTURE_2D, Texture[0]); //glRotatef(zrot, 0.0, 1.0, 0.0); //glRotatef(xrot, 1.0, 0.0, 0.0); zrot+=0.9; if (zrot>360) zrot=0; for (i=0;i //glColor4f(Red,Green,Blue,1);<br> //glColor4f(Red,Green,Blue,1);<br> //printf("%f",Red);<br> glTranslatef (ParXPos, ParYPos, -1.0);<br> glCallList(PARTICLE);<br> }<br> for (i=0;i<maxpar;i++) {<br> ParYPos-=Yamt;<br> //ParXPos+=0.9;<br> if (ParYPos<-630) {<br> ParYPos=270;<br> //ParXPos=rand()%10+-5;<br> }<br> }<br> glutSwapBuffers();<br>glFlush();<br>}<br>void ParInit(void)<br>{<br> int i,h=1,it=0;<br> float x,y,z;<br> for (i=0;i<maxpar;i++) {<br> if (it==0) xrot+=1.5;<br> if (it==1) xrot-=1.5;<br> if (xrot>50) it=1;<br> if (xrot<4) it=0;<br> ParXPos=0;<br> ParYPos=0;<br> ParZPos=rand()%40+-20;<br> Yamt=rand()%4+2;<br> Green=15/(float)(rand()%255+1);<br> Red=15/(float)(rand()%255+1);<br> Blue=15/(float)(rand()%255+1);<br> }<br> glNewList(PARTICLE, GL_COMPILE);<br> glBegin(GL_QUADS);<br> glVertex3f(-maxsize,-maxsize,2);<br> glTexCoord2f(0.0, 1.0); <br> glVertex3f(-maxsize,maxsize,2);<br> glTexCoord2f(0.0, 0.0); <br> glVertex3f(maxsize,maxsize,2);<br> glTexCoord2f(1.0, 0.0); <br> glVertex3f(maxsize,-maxsize,2);<br> glTexCoord2f(1.0, 1.0); <br> glEnd();<br> glEndList();<br>}<br>int main(int argc, char **argv) {<br> glutInit(&argc, argv);<br> glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);<br> glutInitWindowPosition(100,100);<br> glutInitWindowSize(600,600);<br> glutCreateWindow("FIRE DONE BY WYATT");<br> <br> glMatrixMode (GL_PROJECTION); <br> glFrustum (-100.0, 100.0, 100.0, -1000.0, 1.5, 2000.0); <br> glTranslatef (0.0, 0.0, -5.0); /* viewing transformation */<br> glViewport (0, 0, 600, 600); <br> glScalef (1.0, 1.0, 1.0); <br> glMatrixMode (GL_MODELVIEW); <br> TextureInit();<br> BitMapLoad("mask.bmp", 0,0);<br> ParInit(); <br><br> glEnable(GL_BLEND);<br> glBlendFunc(GL_SRC_ALPHA,GL_ONE);<br> <br> glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); <br> glHint(GL_POINT_SMOOTH_HINT,GL_NICEST); <br> <br> glPixelStorei(GL_UNPACK_ALIGNMENT, 1);<br> glDisable(GL_DEPTH_TEST);<br> glDepthFunc(GL_LEQUAL);<br> glShadeModel(GL_FLAT);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);<br> //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br> //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);<br> glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);<br> glEnable(GL_TEXTURE_2D);<br> <br> glutIdleFunc(renderScene);<br> glutDisplayFunc(renderScene);<br> glutMainLoop();<br>} </pre> <br><br>texture.h:<br><pre><br>#include <malloc.h><br>#define maxtex 25<br>unsigned int Texture[maxtex];<br>unsigned char *Image[maxtex+1];<br>void TextureInit()<br>{<br> glGenTextures(maxtex, &Texture[0]);<br>}<br>void BitMapLoad(char *Name, int mipmap)<br>{<br> static int num=-1;<br> FILE *bmp;<br> unsigned r,g,b;<br> int ii, i,temp;<br> unsigned long Size_X=0, Size_Y=0;<br> <br> if ((bmp = fopen(Name, "r"))==NULL) {<br> printf("Could not open %s!!!\n", Name);<br> exit(1);<br> } else num++;<br> <br> <br> fseek(bmp, 18, SEEK_SET);<br> for (i=0;i<4;i++) {<br> temp=fgetc(bmp);<br> Size_X+=(temp<<8*i);<br> }<br> for (i=0;i<4;i++) {<br> temp=fgetc(bmp);<br> Size_Y+=(temp<<8*i);<br> }<br> if ((Image[num]=malloc(((Size_X*Size_Y)*sizeof(int)*3)))==NULL) {<br> printf("Could not allocate memory!");<br> exit(1);<br> }<br><br> fseek(bmp, 54, SEEK_SET);<br> for (ii=0;ii<Size_Y;ii++) {<br> for (temp=0;temp<Size_X%4;temp++) fgetc(bmp);<br> for (i=0;i<Size_X*3;i+=3) { <br> b=fgetc(bmp);<br> g=fgetc(bmp);<br> r=fgetc(bmp);<br> //col=(r+g+b)/3;<br> Image[num][(ii*Size_X*3)+i]=r;<br> Image[num][(ii*Size_X*3)+i+1]=g;<br> Image[num][(ii*Size_X*3)+i+2]=b;<br> }<br> }<br> glBindTexture(GL_TEXTURE_2D, Texture[num]);<br> glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, 0);<br> glPixelStorei(GL_UNPACK_ALIGNMENT, 1);<br> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br> if (mipmap==1) {<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br> gluBuild2DMipmaps(GL_TEXTURE_2D, 3, (unsigned int)Size_X, (unsigned int)Size_Y, <br> GL_RGB, GL_UNSIGNED_BYTE, <br> &Image[num][0]);<br> }<br> if (mipmap==0) {<br> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);<br> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);<br> glTexImage2D(GL_TEXTURE_2D, 0, 3, Size_X, Size_Y, 0, GL_RGB, GL_UNSIGNED_BYTE, &Image[num][0]);<br> }<br> printf("\n%ld, %ld\n", Size_X, Size_Y);<br> fclose (bmp); <br>}<br> </pre> </i>
Shiiiiiiit the fact of the matter is, I am a stupid noob. I was loading the texture before even creating my window, or initializing OpenGL.
You may also want to note that all textures in OpenGL must have
even widths and heights. Example a texture sized: 129, 128 won''t work, however a texture 128, 128 will.
even widths and heights. Example a texture sized: 129, 128 won''t work, however a texture 128, 128 will.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement