I want to create a line at (0,0)(1,0), then create (0,0)(2,0), then 3, 4,... up to 10, then again back (9,0)....(1,0). Is there any way how to do this?
void DrawLine(int a) {
glBegin(GL_LINES);{
glVertex3f(a,0,0);
glVertex3f(0,0,0);
glVertex3f(0,a,0);
glVertex3f(0,0,0);
glVertex3f(-a,0,0);
glVertex3f(0,0,0);
glVertex3f(0,-a,0);
glVertex3f(0,0,0);
}glEnd();
}
Related
I have no idea why when i run this code and use the menu i do not see any of the shapes on the screen. When i replace the glCallLists line with a glu[insertshape] and the same parameters it works perfectly but the exact same line of code but this time called through a display list and it doesn't work.
Ive even used print statements to check and the code is definitely being run
Can anyone help?
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <cstdio>
GLUquadricObj* ptrSphere;
GLUquadricObj* ptrCylinder;
GLUquadricObj* ptrDisk;
GLUquadricObj* ptrPartDisk;
GLuint sphereListID;
GLuint cylinderListID;
GLuint diskListID;
GLuint partialDiskListID;
GLuint menuID;
GLuint currentListID;
void drawSphere()
{
gluSphere(ptrSphere, 2, 25, 25);
printf("sphere");
}
void drawCylinder()
{
gluCylinder(ptrCylinder, 3, 3, 4, 25, 25);
printf("cylinder");
}
void drawDisk()
{
gluDisk(ptrDisk, 1.5, 3, 25, 25);
printf("disk");
}
void drawPartDisk()
{
gluPartialDisk(ptrPartDisk, 1.5, 3, 25, 25, 0, 90);
printf("part disk");
}
void initQuadrics()
{
ptrSphere = gluNewQuadric();
gluQuadricDrawStyle(ptrSphere, GLU_LINE);
ptrCylinder = gluNewQuadric();
gluQuadricDrawStyle(ptrCylinder, GLU_LINE);
ptrDisk = gluNewQuadric();
gluQuadricDrawStyle(ptrDisk, GLU_LINE);
ptrPartDisk = gluNewQuadric();
gluQuadricDrawStyle(ptrPartDisk, GLU_LINE);
}
void initLists()
{
sphereListID = glGenLists(0);
cylinderListID = glGenLists(0);
diskListID = glGenLists(0);
partialDiskListID = glGenLists(0);
glNewList(sphereListID, GL_COMPILE);
drawSphere();
glEndList();
glNewList(cylinderListID, GL_COMPILE);
drawCylinder();
glEndList();
glNewList(diskListID, GL_COMPILE);
drawDisk();
glEndList();
glNewList(partialDiskListID, GL_COMPILE);
drawPartDisk();
glEndList();
currentListID = sphereListID;
}
void myMenu(int value)
{
switch(value)
{
case(1):
currentListID = sphereListID;
break;
case(2):
currentListID = cylinderListID;
break;
case(3):
currentListID = diskListID;
break;
case(4):
currentListID = partialDiskListID;
break;
}
glutPostRedisplay();
}
void initMenu()
{
menuID = glutCreateMenu(myMenu);
glutSetMenu(menuID);
glutAddMenuEntry("Sphere", 1);
glutAddMenuEntry("Cylinder", 2);
glutAddMenuEntry("Disk", 3);
glutAddMenuEntry("Partial Disk", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void init()
{
initQuadrics();
initLists();
initMenu();
}
void display()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(currentListID);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL - First window demo");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}
I'm not sure whether this is the only issue in OPs program but it's definitely one:
void initLists()
{
sphereListID = glGenLists(0);
cylinderListID = glGenLists(0);
diskListID = glGenLists(0);
partialDiskListID = glGenLists(0);
The doc. of glGenLists() is quite explicit about this:
glGenLists has one argument, range. It returns an integer n such that range contiguous empty display lists, named n, n + 1 , ... , n + range - 1 , are created. If range is 0, if there is no group of range contiguous names available, or if any error is generated, no display lists are generated, and 0 is returned.
(Emphasize mine.)
So, I would recommend:
Use glGenLists(1) instead of glGenLists(0) (or even use a single glGenLists(4) to generate all list IDs at once).
Check that the return value of glGenLists() is not 0.
Before we switched to OpenGL 3+, we used display lists to improve the performance in our Visual Simulation applications. That worked amazingly well (and we even had difficulties to gain the same performance in OpenGL 3+ using buffers and shaders).
Using display lists still works well with recent professional (expensive) graphics cards. On (much cheaper) consumer graphics cards, we noticed an unsatisfying performance drop with our legacy code while we achieve comparable performance when we use OpenGL 3+ functions only.
I wanna select point on object by click
It successfully realized
this->camera()->convertClickToLine(point, orig, dir);
bool found;
selectedPoint = this->camera()->pointUnderPixel(point, found);
if (selectedName() >= 0) {
glColor3f(0.9f, 0.2f, 0.1f);
glBegin(GL_POINTS);
glVertex3f(selectedPoint.x, selectedPoint.y, selectedPoint.z);
glEnd();
}
example of selectable object:
glBegin(GL_TRIANGLES);
glColor3f(0.5,0,0);
glVertex3f(xmin,ymin,zmin);
glVertex3f(xmin + (xmax-xmin)/2,ymin+(ymax-ymin)/2, zmin+(zmax-zmin)/2);
glVertex3f(xmax,ymin,zmin);
glEnd();
But if i start using QPainter, selectedPoint change coords to smth wrong
QPainter painter(this);
painter.setPen(Qt::black);
painter.setFont(QFont("Helvetica", 8));
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.drawText(textPosX + 10, textPosY, text);
painter.setBrush(QBrush(Qt::black, Qt::SolidPattern));
painter.drawEllipse(QPoint(textPosX, textPosY), 2, 2);
painter.end();
what should i do?
my steps:
I draw box and object by OpenGl
I draw objects names by Qpainter
I draw point on object by click (doesn't work due to previous item, if i comment item 2, all work fine)
solved
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
/QPainter/
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
I'm trying to make a simple button in OpenGL/LWJGL,
I can render my 2D QUAD correctly, but when i implement the texture, only about 3/4 parts of the whole quad gets textured, like this: https://dl.dropboxusercontent.com/u/60223805/glerror1.png
and if i remove the texture coords i get this: https://dl.dropboxusercontent.com/u/60223805/glerror2.png
none.bind();
co.Enable2D_GUI();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(co.width/2-200, co.height/2);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(co.width/2+none.getTextureWidth(),co.height/2);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(co.width/2+none.getTextureWidth(), co.height/2+none.getTextureHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(co.width/2-200, co.height/2+none.getTextureHeight());
GL11.glEnd();
co.Disable2D_GUI();
where none is an Texture (from slick-util library) and the functions Enable2D_GUI and Disable2D_GUI just enables and disable ortho and stuff.
What can be wrong? I'm very new to OpenGL so im sorry if my question is a bit nooby
This is my Enable2D_GUI and Disable2D_GUI functions:
public void Enable2D_GUI() {
GL11.glMatrixMode (GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity ();
GL11.glOrtho (0, width, height, 0, 1, -1);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode (GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
}
public void Disable2D_GUI() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
Now when I test it with a 3D QUAD it doesnt work either, same result. This is my OpenGL init code:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(
45.0f,
(float)width/(float)height,
0.5f,
50.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
You're misusing the glPushMatrix and glPopMatrix and also not adding or performing state switches in the correct order.
In you init code you need to change it to the following.
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, (float) width / (float) height, 0.5f, 50.0f);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL_SMOOTH);
GL11.glClearColor(0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
You don't have to perform state switches after the glLoadMatrix and glLoadIdentity though it would be better doing so. 1 The code is more readable. 2 Some things gets reset after calling glLoadIdentity though just the stuff about and within the Matrix itself.
Then you also need to fix your Enable2D_GUI and Disable2D_GUI to the following.
Enable2D_GUI
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0f, 1f, -1f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_DEPTH_TEST);
Disable2D_GUI
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
Aleast of what I know you can not use the glPushMatrix and glPopMatrix in between glMatrixMode and glLoadIdentity calls. I could be mistaking Then I also added the glLoadIdentity calls instead to reset the Matrices.
This question already exists:
Closed 10 years ago.
Possible Duplicate:
Open GL :- Filling portion of quadrilateral
I am new to open Gl development and developing a game in which I want to show energy level of player. For which I want to fill a portion of quadrilateral.
Plz help me in creating q quadrilateral whose some portion is filled?
I've made a few simple health bars in OpenGL. The first one is what you asked for, the others were for fun :)
All code is in old OpenGL for sake of exposition. The functions take health as a number between 0 and 1 and draw a square of height 1.0 at y = 0, centered on the Y axis. The screenshots show health at 0.3, 0.5 and 1.0.
1) Rectangle's height is proportional to health:
void drawHealth(float health) {
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(1, health);
glVertex2f(0, health);
glEnd();
}
2) Rectangle's colour is proportional to health:
void drawHealth2(float health) {
glBegin(GL_QUADS);
glColor3f(health, 0, 0);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(1, 1);
glVertex2f(0, 1);
glEnd();
}
3) This version creates a sort of gradient. It looks nice when animated, the picture doesn't quite do it justice.
void drawHealth3(float health) {
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glVertex2f(1, 0);
glColor3f(health, 0, 0);
glVertex2f(1, health);
glVertex2f(0, health);
glVertex2f(0, health);
glVertex2f(1, health);
glColor3f(0, 0, 0);
glVertex2f(1, 1);
glVertex2f(0, 1);
glEnd();
}
4) This version draws discrete bars.
void drawHealth4(float health) {
const int numDiv = 15;
const float sep = 0.04;
const float barHeight = 1.0/(float)numDiv;
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
for(float i = 0; i < health; i += (sep + barHeight)) {
glVertex2f(0, i);
glVertex2f(1, i);
glVertex2f(1, i + barHeight);
glVertex2f(0, i + barHeight);
}
glEnd();
}
glColor4f(r,g,b,a);
glBegin(GL_QUADS);
glVertex2d(-R,-1.0);
glVertex2d(R,-1.0);
glVertex2d(R,1.0);
glVertex2d(-R,1.0);
glEnd();
Here is an example to draw a quad of what color you want, jsut replace the parameters of glVertex2d with the endpoints of your quad. To make a energy level bar simply draw one background quad, then draw a quad inside it to represent the filled portion. Use the coordinates to control the size of the inside quad.
You can create two quadrilateral instead of one of dynamic length .
First initialize two quadrilateral (say A and B)of length x and l-x .
id strength increases, you only have to increase the length of A and decrease the length of B by same amount.
I think this is sufficient to get the idea.
I think texture mapping is a really easy task. Actually, I implemented it many times but failed in this time and don't know why? And I can guarantee that the route to load the texture is right. Any other reasons for my confusion?
Here is my code:
GLuint mytexture;
// two functions below come from NeHe's tut. I think it works well.
AUX_RGBImageRec *LoadBMP(CHAR *Filename)
{
FILE *File=NULL;
if (!Filename)
{
return NULL;
}
File=fopen(Filename,"r");
if (File)
{
fclose(File);
return auxDIBImageLoadA(Filename);
}
return NULL;
}
int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if (TextureImage[0]=LoadBMP("NeHe.bmp"))
{
Status=TRUE;
glGenTextures(1, &mytexture);
glBindTexture(GL_TEXTURE_2D, mytexture);
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;
}
//next is my Init() code:
bool DemoInit( void )
{
if (!LoadGLTextures())
{
return FALSE;
}
glEnable(GL_TEXTURE_2D);
........//other init is ok
}
bool DemoRender()
{
...///render other things
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mytexture);
glColor3f(0,0,1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(200, 0);
glTexCoord2f(1, 1); glVertex2f(200, 200);
glTexCoord2f(0, 1); glVertex2f(0, 200);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
Pretty clear, ha? However, the final result only has a blue rectangle without the texture. Anybody could give me a hint?
Assuming TextureImage[0]->data is correctly populated:
However, the final result only has a blue rectangle without the texture.
You're using the default GL_MODULATE texture environment. Either switch glColor3f(0,0,1) to glColor3f(1,1,1) or use GL_DECAL.
You might also try a glPixelStorei(GL_UNPACK_ALIGNMENT, 1) before your glTexImage2D() since you're using GL_RGB for format.
The problem is I set the GL_LINE mode before I load the texture and I failed to notice that. So after I set the GL_FILL mode, everything is fine!!!