Shader Hintergrundfarbe ignorierenn
-
die.krone schrieb:
glTexImage2D(GL_TEXTURE_2D, 0, 4, SIZE, SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureL);
Was stellt die 4 als drittes Argument dar? Dort sollte GL_RGBA stehen.
Und wie sehen drawCubes() und drawBackground() aus?
-
Das stellt doch GL_RGBA dar. Man kann dort entweder GL_RGBA eintragen oder eben 4, siehe: http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml
Meine initialisierung:
void init() { //Calculations for Left eye/camera frustum L_l = -(Near * ((w / 2 - IOD / 2) / z)); // Left clipping pane L_r = (Near * ((w / 2.0 + IOD / 2.0) / z)); // Right clipping pane L_b = -(Near * ((h / 2.0) / z)); // Bottom clipping pane L_t = (Near * ((h / 2.0) / z)); // Top clipping pane //Calculations for Right eye/camera frustum R_l = -(Near * ((w / 2.0 + IOD / 2.0) / z)); // Left clipping pane R_r = (Near * ((w / 2.0 - IOD / 2.0) / z)); // Right clipping pane R_b = -(Near * ((h / 2.0) / z)); // Bottom clipping pane R_t = (Near * ((h / 2.0) / z)); // Top clipping pane /** * TEXTURES */ glEnable(GL_TEXTURE_2D); /* --- Texture Setup --- */ glGenTextures(2, textures); // Platz f. NUM_TEXTURES Texturobjekte anfordern... for (int i = 0; i < 2; i++) { /* Aktuelles Texturobjekt selektieren */ glBindTexture(GL_TEXTURE_2D, textures[i]); /* Image laden.. */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // scale linearly when image bigger than texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // scale linearly when image smalled than texture teximage = (GLuint *) read_texture(tex[i].filename, &tex[i].width, &tex[i].height, &texcomps); /* ...und Pixeldaten an das Texturobjekt uebergeben. */ gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tex[0].width, tex[0].height, GL_RGBA, GL_UNSIGNED_BYTE, teximage); } /* Per Default berechnete Pixelfarbe durch Texelfarbe ersetzen */ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); /** Init textures to render into*/ glGenTextures(1, &leftTexture); glBindTexture(GL_TEXTURE_2D, leftTexture); glTexImage2D(GL_TEXTURE_2D, 0, 4, SIZE, SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glGenTextures(1, &rightTexture); glBindTexture(GL_TEXTURE_2D, rightTexture); glTexImage2D(GL_TEXTURE_2D, 0, 4, SIZE, SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, leftTexture); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, rightTexture); setShaders(); texLeftSampler = glGetUniformLocation(program, "left"); glUniform1i(texLeftSampler, 1); // 0 für Texture-Unit 0 cout << "ERR texleft: " << glGetError() << endl; texRightSampler = glGetUniformLocation(program, "right"); glUniform1i(texRightSampler, 2); // 1 für Texture-Unit 1 cout << "ERR texright: " << glGetError() << endl; glUseProgram(0); glDisable(GL_BLEND); }
Die Methode, welche das hintere Quad zeichnet:
void drawBackground() { //Draw Background glPushMatrix(); glBindTexture(GL_TEXTURE_2D, textures[0]); glTranslatef(0.0, 0.0, -5.0); //glRotatef(rotation_y, 0.0, 1.0, 0.0); glScalef(7.0, 7.0, 1.0); glBegin(GL_QUADS); // begin drawing a cube glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad glEnd(); glPopMatrix(); }
Methoden für das Zeichnen der Würfel:
void drawCube() { glPushMatrix(); glRotatef(rotation_y, 0.0, 1.0, 0.0); glBindTexture(GL_TEXTURE_2D, textures[1]); glBegin(GL_QUADS); // begin drawing a cube // Front Face (note that the texture's corners have to match the quad's corners) glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad // Back Face glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad // Top Face glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad // Bottom Face glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad // Right face glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad // Left Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad glEnd(); // done with the polygon. glPopMatrix(); } void drawCubes() { glPushMatrix(); glTranslatef(-1.5, -1.0, -2.0); drawCube(); glTranslatef(3.0, 3.0, 0.0); drawCube(); glPopMatrix(); }
Und zuletzt nochmal das Rendern in die Textur bzw. die Display-Methode:
void renderToTexture(int eye) { if (eye == 0) { //LEFT EYE glActiveTexture(GL_TEXTURE0); /* Define a view-port adapted to the texture */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); //frustum for left eye glFrustum(L_l, L_r, L_b, L_t, Near, Far); glViewport(0, 0, SIZE, SIZE); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(-IOD / 2, 0.0, 5.0, /* eye is at (0,0,5) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ /* Render to buffer */ glClearColor(background_color, background_color, background_color, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glScalef(zoom, zoom, zoom); drawCubes(); drawBackground(); glFlush(); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, leftTexture); /* Copy buffer to texture */ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, SIZE, SIZE); } else if (eye == 1) { //RIGHT EYE glActiveTexture(GL_TEXTURE0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //frustum for right eye glFrustum(R_l, R_r, R_b, R_t, Near, Far); glViewport(0, 0, SIZE, SIZE); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(IOD / 2, 0.0, 5.0, /* eye is at (0,0,5) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ /* Render to buffer */ glClearColor(background_color, background_color, background_color, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glScalef(zoom, zoom, zoom); drawCubes(); drawBackground(); glFlush(); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, rightTexture); /* Copy buffer to texture */ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, SIZE, SIZE); } // glActiveTexture(GL_TEXTURE0); } void display() { /** render into two textures */ renderToTexture(0); renderToTexture(1); glViewport(0,0,SIZE,SIZE); glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glDisable(GL_DEPTH_TEST); /** draw rendered textures */ glUseProgram(program); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, leftTexture); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, rightTexture); glBegin(GL_QUADS); glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 1.0f); glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1.0f); glVertex3i(-1, -1, -1); glMultiTexCoord2f(GL_TEXTURE2, 1.0f, 1.0f); glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 1.0f); glVertex3i(1, -1, -1); glMultiTexCoord2f(GL_TEXTURE2, 1.0f, 0.0f); glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 0.0f); glVertex3i(1, 1, -1); glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f); glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f); glVertex3i(-1, 1, -1); glEnd(); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glUseProgram(0); glEnable(GL_DEPTH_TEST); glFlush(); glutSwapBuffers(); glutPostRedisplay(); }
Viel mehr gibt es auch in meinem Programm nicht..
-
Versuch trotzdem als erstes mal anstelle von 4 GL_RGBA anzugeben bei glTexImage2D.
Danach schauen wir mal weiter
-
Okay gemacht. Wie schon gedacht. Gleicher Effekt. Hat nichts verändert.
-
Du verwendest ja auch den immidiate-mode, da werden doch die shader garnicht berücksichtigt.
Im Pinrzip kannst du die shader komplett weglassen - und solltest immernoch dsa gleiche Ergebniss erhalten.
-
Nee also wenn ich den Shader komplett weglasse, dann bekomme ich eine einheitliche Farbe übers ganze Bild.
was muss ich denn anders machen?
-
ogl schrieb:
Du verwendest ja auch den immidiate-mode, da werden doch die shader garnicht berücksichtigt.
Im Pinrzip kannst du die shader komplett weglassen - und solltest immernoch dsa gleiche Ergebniss erhalten.Das stimmt so nicht afaik. Er wird schon berücksichtig. Habe das auch schon selbst gemacht. Aber es ist ein böser Mix.
@krone
Denke mal drüber nach komplett nach OpenGL 3.x zu migrieren und die fixed Pipeline hinter dir zu lassen. Da gehören Shader dann auch zum Core.
Ob das jetzt dein Problem behebt mag ich allerdings nicht zu sagen.
-
Du meinst dann also statt dem glBegin usw. einfach glutSolidCube und so zu verwenden oder gibts noch mehr was ich in dem alten Stil mache?
-
die.krone schrieb:
Du meinst dann also statt dem glBegin usw. einfach glutSolidCube und so zu verwenden oder gibts noch mehr was ich in dem alten Stil mache?
Nein, es gibt einen gravierenden Unterschied zwischen OpenGL 3.x und OpenGL 1.x was du hier verwendest, bzw OpenGL 2.x.
OpenGL 1.x verwendet eben den von dir benutzen Immediate Mode mit glBegin/glEnd.
OpenGL 2.x verwendet clientseitige Vertex Arrays.Beide Versionen haben gemeinsam, dass sie eine sogenannte Fixed Pipeline haben, wo die Matrizen etc von OpenGL angeboten werden.
OpenGL 3.x verwendet dagegen Vertex Buffer Objects, das sind Speicherblöcke im VRAM. Die Geometrie wird an die GraKa gesendet und dann mittels glDrawBuffers dargestellt. Auch gibt es hier keine Matrizen mehr die angeboten werden von OpenGL, man muss die Transformation selbst im Vertex Shader übernehmen.
Man braucht dazu auch DirectX10 fähige Hardware, da nur diese die sog. "Programmable" Pipeline anbieten.Das ist jetzt nur ein großer Abriss, aber so läuft es im Endeffekt. Falls du keine DX10 Karte hast, bzw keine die OpenGL 3.x unterstützt, dann solltest du zumindest über Vertex Arrays mit OpenGL 2.x nachdenken.
Aber wie gesagt, ich denke nicht dass Dein Problem daher kommt^^. Aber kann schon sein. Zur not mal auf OpenGL.org im Forum fragen.
-
Schade, dann krieg ich das wohl nicht gelöst.
-
Ich glaub es kaum..aber ich hab das Problem gelöst.
Eigentlich ist die Lösung simpel.
Man muss nur beim displayMode noch das Attribut GLUT_ALPHA eintragen. Ein reines GLUT_RGBA hat wohl nicht gelangt.Danke trotzdem für die Hilfe!
-
die.krone schrieb:
Ich glaub es kaum..aber ich hab das Problem gelöst.
Eigentlich ist die Lösung simpel.
Man muss nur beim displayMode noch das Attribut GLUT_ALPHA eintragen. Ein reines GLUT_RGBA hat wohl nicht gelangt.Danke trotzdem für die Hilfe!
Dang... daran habe ich nicht gedacht xD. Naja, so lernt man dazu.
-
Tja man lernt nie aus, gell
Nochmal kurz:
Ich hab jetzt folgenden Effekt. Krieg ich noch weg, dass die Farbe beim Übergang zu weiß wechselt?
-
Eine bitte:
Kein Imageshack mehr pls. Die nerven total mit ihrem undefined image wenn mans größer sehen will :(.
http://abload.de oder http://pic-upload.deDa ichs nicht richtig erkenne, rate ich mal und sag, probier GL_NEAREST als Texturfilter.
-
Sorry. War der erste Anbieter den ich gefunden hab.
Texturfilter war es leider nicht.
Ich meine auch dass das gelb zu blau wird, wenn der Hintergrund weiß ist. Nicht dass wir aneinander vorbeireden.
-
Also ich hab es rausgefunden.
Wenn ich die if-Abfrage im Shader rauslösche und einfach nur:uniform sampler2D left; uniform sampler2D right; void main(void) { vec4 cleft = texture2D(left, gl_TexCoord[1].st).rgba; vec4 cright = texture2D(right, gl_TexCoord[2].st).rgba; vec3 col = vec3(0.0,0.0,0.0), coeff = vec3(0.15, 0.15, 0.7); // falls sowohl in Textur1 als auch in Textur2 der Hintergrund sichtbar ist: if (cleft.a == 0.0 && cright.a == 0.0) { // ignorieren discard; } else { col.r = cleft.r; col.g = cleft.g; col.b = dot(cright.rgb, coeff); } gl_FragColor = vec4(col,1.0); }
schreibe, dann gibt es keine unterschiedlichen Farben.
Das Resultat ist folgendes http://www.abload.de/image.php?img=colcodegsu3.png
Allerdings ist das gelb und blau sehr matt. Hat jemand eine Idee wie ich das gelb und blau dunkler/greller bekomme?