- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我还是opengl的新手,我用这个教程作为引用
我现在正在尝试绘制一组由 4 个三角形组成的正方形(这是我的项目任务),这是我的代码
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
using namespace std;
void changeViewPort(int w, int h)
{
glViewport(0, 0, w, h);
}
void draw() {
glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 255.0);
glVertex3f(0.0, 0.0, 0.0); // top triangle
glVertex3f(-1.0,1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glColor3f(50.0, 50.0, 255.0);
glVertex3f(0.0, 0.0, 0.0); // right triangle
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glColor3f(75.0, 75.0, 255.0);
glVertex3f(0.0, 0.0, 0.0);//bottom triangle
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(50.0, 50.0, 255.0);
glVertex3f(0.0, 0.0, 0.0); //left triangle
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
glutSwapBuffers();
}
int main(int argc, char* argv[]) {
// Initialize GLUT
glutInit(&argc, argv);
// Set up some memory buffers for our display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Set the window size
glutInitWindowSize(350, 300);
// Create the window with the title "Hello,GL"
glutCreateWindow("Hello, GL");
// Bind the two functions (above) to respond when necessary
glutReshapeFunc(changeViewPort);
glutDisplayFunc(render);
//glutDisplayFunc(draw);
// Very important! This initializes the entry points in the OpenGL driver so we can
// call all the functions in the API.
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW error");
return 1;
}
glutMainLoop();
return 0;
}
请注意,在我的绘制函数中,我只是试图创建 4 个具有不同蓝色阴影的不同三角形。然而,每当执行此代码时,只有顶部三角形是蓝色的,而其余的则保持白色
有谁知道为什么这行不通?我什至尝试创建绘图函数的 for 循环版本,但它也没有用
void draw() {
double x = 1.0;
double y = 1.0;
double coords[8] = { -x,y,x,y,x,-y,-x,-y };
for (int i = 0; i < 7; i=i+2) { // i = [0,2,4,6]
switch (i) {
case 0:
glColor3f(0, 0, 255);
break;
case 2:
glColor3f(50, 50, 255);
break;
case 4:
glColor3f(75, 75, 255);
break;
case 6:
glColor3f(50, 50, 255);
break;
}
if (i == 6) { //4th and 1st coordinate
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(coords[i], coords[i + 1], 0.0);
glVertex3f(coords[0], coords[1], 0.0);
glEnd();
}
else {
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(coords[i], coords[i + 1], 0.0);
glVertex3f(coords[i + 2], coords[i + 3], 0.0);
glEnd();
}
}
}
最佳答案
glColor3f
将 [0, 1] 范围内的颜色分量作为输入,而不是当前使用的 [0, 255]。
关于c++ - glColor不会改变opengl中其他三角形的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710555/
我是 OpenGL 的新手,所以也许答案很明显。我目前正在尝试在 C++ 中使用 GL_TRIANGLE_FAN 制作一个蓝色圆圈。我的问题是,当我使用 glColor4f 设置颜色时,它会将我所有其
我有以下头文件: #ifndef CLASSES_H #define CLASSES_H class Mouse // Handles clicking of the mouse { private:
基本上我在 opengl es 1.1 中有一个功能齐全的 ogl 程序(2D 游戏),除了一个问题外一切正常:glcolor 我正在尝试将我的一些纹理设置为半透明,我想按如下方式进行; gl.glC
我的代码某处有问题,但对于我绘制的任意数量的图元,尽管调用了 glClearColor 然后使用 glColor3f 选择颜色,出现的颜色是完全随机的...... 所以在我的渲染类中,我循环遍历所有对
我正在尝试绘制一个洛伦兹吸引子,整个吸引子的颜色都会发生变化。我已经编写了以下用于计算吸引点的 for 循环。 float x = 1, y = 1, z = 1; glBegin(G
我的纹理本身有一个透明的背景,我想按原样渲染它。 glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glEnableClientState(GL_VERTEX_
我有一个特别烦人的问题。在某些情况下,对 glColor 的调用似乎被忽略,导致对象以不正确的颜色显示。 可以找到显示此问题的 Qt 项目 here . 当您运行该程序时,您在屏幕上看到的只是两个从某
有什么方法可以发送这样的结构: struct COLOR { float r, g, b, a; }; 作为一个参数直接进入 glColor*() 函数?会使代码更好。 我可以创建自己的函数并
这听起来可能很愚蠢……但我一直在想,如果我向 glColor*f 函数发送一个大于 1.0f 的值有关系吗? 较旧的 gfx 卡/驱动程序是否以不同方式处理这些问题? OpenGL 是直接使用我给它的
我是一名优秀的程序员,十分优秀!